File size: 6,645 Bytes
09f45d8 85f794d 09f45d8 b2aa446 09f45d8 a5981db 09f45d8 b7b2aea 09f45d8 a5981db 09f45d8 b2aa446 09f45d8 b7b2aea 09f45d8 03efaf9 09f45d8 b2aa446 09f45d8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | 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>;
}
|