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; } 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(); 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(() => { 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 ; }