Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download web/src/apps/AppRail.tsx from fsanyoto/runloopable: direct link, hf CLI and curl.
- Browser
- Download file 6.65 kB
-
https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/apps/AppRail.tsx
- Command line
-
hf download hf://spaces/fsanyoto/runloopable/web/src/apps/AppRail.tsx
-
curl -L -o AppRail.tsx https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/apps/AppRail.tsx
6.65 kB
| import { useEffect, useRef, useState } from "react"; | |
| import { API_V1, CREDENTIALS, UNAUTHORIZED_EVENT, checkTenant, currentTenant, signal } from "../apiContract"; | |
| import { appPathHash } from "../customer-grid/seam"; | |
| import { fetchWithDeadline } from "../net/deadline"; | |
| import PanelToggleIcon from "../shell/PanelToggleIcon"; | |
| import { useNarrowRailFold } from "../shell/railHeader"; | |
| import { AppMark, TemplateMark } from "./templateMarks"; | |
| import "../styles/sdlc81/app-rail.css"; | |
| interface RailApplication { | |
| id: string; | |
| name: string; | |
| deepLink?: string; | |
| templateKind?: string; | |
| templateConnector?: string; | |
| navPlacement?: { order?: number }; | |
| openTarget?: { database?: string }; | |
| compatibleDatabases?: string[]; | |
| capabilities?: Record<string, boolean>; | |
| } | |
| const RAIL_KEY = "aios-app-rail"; | |
| function railApplications(body: unknown): RailApplication[] { | |
| if (!body || typeof body !== "object" || !("applications" in body) | |
| || !Array.isArray(body.applications)) throw new Error("application_list_invalid"); | |
| const seen = new Set<string>(); | |
| return body.applications.filter((row): row is RailApplication => { | |
| if (!row || typeof row !== "object" || typeof row.id !== "string" || !row.id | |
| || typeof row.name !== "string" || !row.name.trim() || seen.has(row.id) | |
| || (row.templateConnector !== "amazon" && row.templateConnector !== "amazon_ads") | |
| || row.capabilities?.read === false) return false; | |
| seen.add(row.id); | |
| return true; | |
| }).sort((a, b) => (a.navPlacement?.order ?? 0) - (b.navPlacement?.order ?? 0) | |
| || a.name.localeCompare(b.name, undefined, { sensitivity: "base" }) || a.id.localeCompare(b.id)); | |
| } | |
| /** An Amazon App's rail row draws its own mark, never the Amazon logo (QI37). */ | |
| function isAmazonApp(app: RailApplication): boolean { | |
| return app.templateConnector === "amazon" || app.templateConnector === "amazon_ads"; | |
| } | |
| /** The same href as the App home tile. */ | |
| function railHref(app: RailApplication): string { | |
| const database = app.openTarget?.database ?? app.compatibleDatabases?.[0] ?? ""; | |
| return app.deepLink ? `#${app.deepLink}` : database | |
| ? appPathHash({ database, application: app.id }) | |
| : `#/app?application=${encodeURIComponent(app.id)}`; | |
| } | |
| /** The left rail inside an Amazon App: every Amazon App the grant-filtered nav returns, the current one selected. */ | |
| export function AppRail({ appId }: { appId: string }) { | |
| const tenant = currentTenant(); | |
| const [revision, setRevision] = useState(0); | |
| const scope = JSON.stringify([tenant, appId, revision]); | |
| const wanted = useRef(scope); | |
| wanted.current = scope; | |
| const [result, setResult] = useState<{ scope: string; applications: RailApplication[] } | null>(null); | |
| const [railShut, setRailShut] = useState<boolean>(() => { | |
| try { | |
| return localStorage.getItem(RAIL_KEY) === "1"; | |
| } catch { | |
| return false; | |
| } | |
| }); | |
| const toggleRail = () => setRailShut(value => { | |
| const next = !value; | |
| try { | |
| localStorage.setItem(RAIL_KEY, next ? "1" : "0"); | |
| } catch { | |
| // Blocked storage still folds for this visit. | |
| } | |
| return next; | |
| }); | |
| const narrow = useNarrowRailFold(); | |
| const [narrowOpen, setNarrowOpen] = useState(false); | |
| useEffect(() => { | |
| if (!narrow) setNarrowOpen(false); | |
| }, [narrow]); | |
| const folded = narrow ? !narrowOpen : railShut; | |
| useEffect(() => { | |
| const controller = new AbortController(); | |
| const isCurrent = () => !controller.signal.aborted && wanted.current === scope | |
| && currentTenant() === tenant; | |
| const clear = () => { | |
| controller.abort(); | |
| setResult(null); | |
| }; | |
| const refresh = () => { | |
| clear(); | |
| setRevision(value => value + 1); | |
| }; | |
| setResult(null); | |
| window.addEventListener(UNAUTHORIZED_EVENT, clear); | |
| window.addEventListener("aios:signed-out", clear); | |
| window.addEventListener("aios:applications-changed", refresh); | |
| if (appId) void fetchWithDeadline(`${API_V1}/nav/applications`, { | |
| method: "GET", credentials: CREDENTIALS, signal: controller.signal, cache: "no-store", | |
| }, async response => { | |
| if (!isCurrent()) throw new Error("application_list_stale"); | |
| if (checkTenant(response)) throw new Error("application_list_scope_changed"); | |
| if (tenant === null && currentTenant() !== null) { | |
| setRevision(value => value + 1); | |
| throw new Error("application_list_scope_bound"); | |
| } | |
| if (response.status === 401) signal(UNAUTHORIZED_EVENT); | |
| if (!response.ok) throw new Error("application_list_failed"); | |
| return railApplications(await response.json()); | |
| }).then(({ body }) => { | |
| if (isCurrent()) setResult({ scope, applications: body }); | |
| }).catch(() => { | |
| if (isCurrent()) setResult(null); | |
| }); | |
| return () => { | |
| controller.abort(); | |
| window.removeEventListener(UNAUTHORIZED_EVENT, clear); | |
| window.removeEventListener("aios:signed-out", clear); | |
| window.removeEventListener("aios:applications-changed", refresh); | |
| }; | |
| }, [scope, appId, tenant]); | |
| const applications = result?.scope === scope ? result.applications : []; | |
| const toggleLabel = folded ? "Expand Apps" : "Minimize Apps"; | |
| return <nav className={"app-rail" + (folded ? " is-collapsed" : "") + (narrow && narrowOpen ? " is-narrow-open" : "")} | |
| aria-label="Apps" data-app-rail={appId}> | |
| <div className="app-rail-top"> | |
| {/* The Table rail's Back control, matched by class: it opens the Apps home. */} | |
| <a className="cg-path-history-button" href="#/app" aria-label="Back to Apps" title="Back to Apps"> | |
| <svg viewBox="0 0 16 16" width="16" height="16" fill="none" aria-hidden="true"> | |
| <path d="M9.8 3.6 5.4 8l4.4 4.4" stroke="currentColor" strokeWidth="1.35" | |
| strokeLinecap="round" strokeLinejoin="round" /> | |
| </svg> | |
| </a> | |
| <span className="app-rail-title">Apps</span> | |
| <button type="button" className="cg-rail-toggle" aria-label={toggleLabel} title={toggleLabel} | |
| aria-expanded={!folded} onClick={narrow ? () => setNarrowOpen(open => !open) : toggleRail}> | |
| <PanelToggleIcon open={!folded} /> | |
| </button> | |
| </div> | |
| <ul className="app-rail-list"> | |
| {applications.map(app => <li key={app.id}> | |
| <a className="app-rail-item" href={railHref(app)} title={app.name} | |
| aria-current={app.id === appId ? "page" : undefined}> | |
| {isAmazonApp(app) | |
| ? <TemplateMark mark={app.templateKind} size={16} /> | |
| : <AppMark mark={app.templateKind} connector={app.templateConnector} size={16} />} | |
| <span className="app-rail-name">{app.name}</span> | |
| </a> | |
| </li>)} | |
| </ul> | |
| </nav>; | |
| } | |