import { useEffect, useRef, useState } from "react"; import { API_V1, CREDENTIALS, TOAST_EVENT, UNAUTHORIZED_EVENT, checkTenant, signal } from "../../apiContract"; import CodeAppFrame from "./CodeAppFrame"; import { checkCodeAppSize } from "./frameDocument"; import ConfirmDelete, { useConfirmDelete } from "./ConfirmDelete"; type CodeApp = { id: string; name: string; html: string; revision: number | string }; type CodeKit = { css: string; version: number | string }; type PageState = | { kind: "loading"; id: string } | { kind: "ready"; id: string; app: CodeApp; kit: CodeKit; started: boolean } | { kind: "error" | "refused"; id: string; message: string }; class ReadRefused extends Error {} async function read(path: string, abort: AbortSignal): Promise { const response = await fetch(`${API_V1}${path}`, { credentials: CREDENTIALS, signal: abort, cache: "no-store" }); if (checkTenant(response)) throw new ReadRefused("You cannot open this App."); if (response.status === 401) { signal(UNAUTHORIZED_EVENT); throw new ReadRefused("You cannot open this App."); } if (response.status === 403 || response.status === 404) throw new ReadRefused("You cannot open this App."); if (!response.ok) throw new Error("This App could not be loaded."); return response.json(); } function isCodeApp(value: unknown, id: string): value is CodeApp { if (!value || typeof value !== "object") return false; const app = value as Partial; return app.id === id && typeof app.name === "string" && typeof app.html === "string" && (typeof app.revision === "number" || typeof app.revision === "string"); } function isCodeKit(value: unknown): value is CodeKit { if (!value || typeof value !== "object") return false; const kit = value as Partial; return typeof kit.css === "string" && (typeof kit.version === "number" || typeof kit.version === "string"); } function Loading() { return
{["48%", "100%", "72%"].map((width) =>
; } export default function CodeAppPage({ applicationId, bootstrapBase }: { applicationId: string; bootstrapBase?: string }) { const [retry, setRetry] = useState(0); const [state, setState] = useState({ kind: "loading", id: applicationId }); const announced = useRef(null); useEffect(() => { if (state.id !== applicationId || state.kind !== "refused" || announced.current === state) return; announced.current = state; window.dispatchEvent(new CustomEvent(TOAST_EVENT, { detail: { text: state.message, tone: "error" } })); }, [state, applicationId]); const { request, confirmDelete, confirmTool } = useConfirmDelete(); useEffect(() => { const controller = new AbortController(); setState({ kind: "loading", id: applicationId }); async function load() { try { const [app, kit] = await Promise.all([ read(`/applications/${encodeURIComponent(applicationId)}/code`, controller.signal), read("/applications/code-kit", controller.signal), ]); if (!isCodeApp(app, applicationId) || !isCodeKit(kit)) throw new Error("Invalid App response"); checkCodeAppSize(app.html); if (!controller.signal.aborted) setState({ kind: "ready", id: applicationId, app, kit, started: false }); } catch (error) { if (controller.signal.aborted) return; setState(error instanceof ReadRefused || error instanceof RangeError ? { kind: "refused", id: applicationId, message: error.message } : { kind: "error", id: applicationId, message: "This App could not be loaded." }); controller.abort(); } } void load(); return () => controller.abort(); }, [applicationId, retry]); const current = state.id === applicationId ? state : { kind: "loading" as const, id: applicationId }; return
{(current.kind === "loading" || (current.kind === "ready" && !current.started)) && } {current.kind === "ready" && setState((value) => value.kind === "ready" && value.id === current.id ? { ...value, started: true } : value)} onRefused={(message) => setState({ kind: "refused", id: current.id, message })} />} {current.kind === "error" &&

{current.message}

}
; }