import { useCallback, useEffect, useRef, useState } from "react"; import { ReactFlowProvider } from "@xyflow/react"; import "@xyflow/react/dist/style.css"; import { AlertCircle, AlertTriangle, Check, PanelLeft, PanelRight, X, } from "lucide-react"; import { api } from "./lib/api"; import { getUserId } from "./lib/userId"; import { theaterAutoOpenEnabled, useApp } from "./lib/store"; import { ensureDemoArchive } from "./lib/demoArchive"; import { useIsDesktop, useMediaQuery } from "./lib/useMediaQuery"; import { TopBar } from "./components/TopBar"; import { FlowCanvas } from "./components/FlowCanvas"; import { ResultsPane } from "./components/ResultsPane"; import { MultiverseBoard } from "./components/MultiverseBoard"; import { EvidenceTheater } from "./components/EvidenceTheater"; import { CircuitPicker } from "./components/CircuitPicker"; import { MobileDrawer } from "./components/MobileDrawer"; import { WelcomeTour, useFirstVisitTour } from "./components/WelcomeTour"; import { LessonCard } from "./components/LessonCard"; import { LearnLab } from "./components/LearnLab"; import { activateScenario } from "./lib/scenarios"; // --------------------------------------------------------------------------- // RESPONSIVE CONTRACT (no horizontal page scroll at ANY width ≥ 768px) // // <768px mobile: both side panes become MobileDrawers; the Run // FAB replaces the toolbar Run button. // 768–1199px compact desktop ("the band"): the left pane stays in // flow (its clamps account for the strip), but the // Evidence pane NEVER takes layout width — it boots // collapsed to its 32px strip and, when expanded, opens // as a right-anchored OVERLAY over the center column // (bandEvidenceOpen: session state, never persisted). // Rationale: LEFT_MIN + RIGHT_MIN + MIN_CANVAS_W + two // resizers = 808px, so hosting both panes in flow is // impossible at 768px and cramped through ~1200px. // ≥1200px full desktop: three columns, both panes resizable, // widths + collapsed preferences persisted. // // Two width guards keep the columns inside the viewport: the drag-time // clamp in each PaneResizer, and the mount+resize clamp effect below — // it MUST run on mount, not just on resize events, because persisted // widths from a wider monitor would otherwise overflow the first paint // into horizontal scroll. The center column owns all remaining width // (flex-1 + min-w-0 down to the board grid, whose auto-fill tracks are // minmax(min(240px,100%),1fr)); the board/theater overlays are // absolutely positioned inside it, so nothing can widen the page. // --------------------------------------------------------------------------- // Default + clamp bounds for the two side panels. The middle canvas flexes. const LEFT_DEFAULT = 320; const LEFT_MIN = 220; const LEFT_MAX = 560; const RIGHT_DEFAULT = 400; const RIGHT_MIN = 300; const RIGHT_MAX = 720; const COLLAPSED_W = 32; // Full-desktop threshold — below this (and ≥768px) is "the band". const WIDE_QUERY = "(min-width: 1200px)"; // Minimum width we ever want to leave for the canvas (React Flow + // its two-row toolbar). The dynamic clamp in the resize handler caps // the side panes so the center column never falls below this, // regardless of viewport size. (The old rationale — the pipeline // shelf's columns overflowing `
` — died with the shelf; the floor // simply keeps the toolbar rows and a useful slice of canvas usable.) const MIN_CANVAS_W = 280; // Reserve the side resizer's footprint when computing the canvas budget. const RESIZER_W = 4; const LS_LEFT = "quda.leftPaneWidth"; const LS_RIGHT = "quda.rightPaneWidth"; const LS_LEFT_COLLAPSED = "quda.leftPaneCollapsed"; const LS_RIGHT_COLLAPSED = "quda.rightPaneCollapsed"; function loadWidth(key: string, def: number, min: number, max: number): number { if (typeof window === "undefined") return def; try { const raw = window.localStorage.getItem(key); if (!raw) return def; const n = Number(raw); if (!Number.isFinite(n)) return def; return Math.min(max, Math.max(min, n)); } catch { return def; } } function loadBool(key: string, def: boolean): boolean { if (typeof window === "undefined") return def; try { const raw = window.localStorage.getItem(key); if (raw == null) return def; return raw === "1"; } catch { return def; } } export default function App() { const setHealth = useApp((s) => s.setHealth); const running = useApp((s) => s.running); const workspaceMode = useApp((s) => s.workspaceMode); const theaterOpen = useApp((s) => s.theaterOpen); const learnLabOpen = useApp((s) => s.learnLabOpen); const [ready, setReady] = useState(false); const isDesktop = useIsDesktop(); const isWide = useMediaQuery(WIDE_QUERY); // 768–1199px compact-desktop band (see RESPONSIVE CONTRACT above). const band = isDesktop && !isWide; // Welcome tour: auto-opens on the first visit (skipped under // ?scenario= boots), re-openable from the TopBar Tour button. const [tourOpen, setTourOpen] = useFirstVisitTour(); // Desktop pane widths — unused on mobile (drawers take over there). const [leftW, setLeftW] = useState(() => loadWidth(LS_LEFT, LEFT_DEFAULT, LEFT_MIN, LEFT_MAX), ); const [rightW, setRightW] = useState(() => loadWidth(LS_RIGHT, RIGHT_DEFAULT, RIGHT_MIN, RIGHT_MAX), ); const [leftCollapsed, setLeftCollapsed] = useState(() => loadBool(LS_LEFT_COLLAPSED, false), ); // Right pane (results) defaults to collapsed on first visit so the // canvas reads cleanly and matches the mobile drawer (also closed by // default). Once the user runs a pipeline, the run-start effect below // expands it; user's preference thereafter is persisted in // localStorage so a power user who likes it permanently expanded // doesn't have to re-expand every visit. const [rightCollapsed, setRightCollapsed] = useState(() => loadBool(LS_RIGHT_COLLAPSED, true), ); // Mobile drawer state — unused on desktop. const [leftDrawerOpen, setLeftDrawerOpen] = useState(false); const [rightDrawerOpen, setRightDrawerOpen] = useState(false); // Band (768–1199px) Evidence overlay — session-only, never // persisted: the band boots collapsed-by-default regardless of the // ≥1200px preference, and expanding here must not rewrite it. const [bandEvidenceOpen, setBandEvidenceOpen] = useState(false); useEffect(() => { if (!band) setBandEvidenceOpen(false); }, [band]); // Esc dismisses the overlay (audit S3): it floats over the center // column like a modal, so it should leave like one. useEffect(() => { if (!band || !bandEvidenceOpen) return; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") setBandEvidenceOpen(false); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [band, bandEvidenceOpen]); // FlowCanvas can ask us to surface the CircuitPicker when the user // tries to run without a circuit selected. const hintExpandLeftPane = useApp((s) => s.hintExpandLeftPane); useEffect(() => { if (hintExpandLeftPane === 0) return; if (isDesktop) setLeftCollapsed(false); else setLeftDrawerOpen(true); }, [hintExpandLeftPane, isDesktop]); // Same bridge for the right (Evidence) pane — scenario boots use it // to make sure the funnel / lineage / comparison the figure needs is // actually on screen. const hintExpandRightPane = useApp((s) => s.hintExpandRightPane); useEffect(() => { if (hintExpandRightPane === 0) return; if (!isDesktop) setRightDrawerOpen(true); else if (band) setBandEvidenceOpen(true); else setRightCollapsed(false); }, [hintExpandRightPane, isDesktop, band]); // True while a PaneResizer drag is in flight. We use it to suppress the // CSS `transition-[width]` during dragging: setRightW / setLeftW fire on // every mousemove, so each value change would otherwise kick off a 150ms // animation, causing the visible aside width to lag behind the inner // content's layout — the cards would briefly overflow the aside while it // animates. With the transition off during drag, width tracks the cursor // exactly; we re-enable it for the smooth collapse/expand toggles. const [isResizing, setIsResizing] = useState(false); // Persist user's preferred widths (desktop only; the values are loaded // at mount either way so that toggling screen size restores them). useEffect(() => { try { window.localStorage.setItem(LS_LEFT, String(leftW)); } catch { /* ignore quota errors */ } }, [leftW]); useEffect(() => { try { window.localStorage.setItem(LS_RIGHT, String(rightW)); } catch { /* ignore quota errors */ } }, [rightW]); useEffect(() => { try { window.localStorage.setItem(LS_LEFT_COLLAPSED, leftCollapsed ? "1" : "0"); } catch { /* ignore */ } }, [leftCollapsed]); useEffect(() => { try { window.localStorage.setItem( LS_RIGHT_COLLAPSED, rightCollapsed ? "1" : "0", ); } catch { /* ignore */ } }, [rightCollapsed]); // Auto-expand the right pane (desktop) or pop the right drawer (mobile) // when a pipeline run kicks off, so the user sees progress. Only on the // false→true edge — repeat runs while already expanded are a no-op. const prevRunningRef = useRef(running); useEffect(() => { if (running && !prevRunningRef.current) { if (!isDesktop) { setRightDrawerOpen(true); } else if (band) { // Band: surface progress as the overlay — the in-flow pane // must not take layout width here (RESPONSIVE CONTRACT). // UNLESS the Evidence theater will auto-open on this run's // first streaming frame: stacking both overlays buried the // board under two layers (audit S2). With theater auto-open // disabled, this overlay stays the band's only progress // surface and still opens. if (!theaterAutoOpenEnabled()) setBandEvidenceOpen(true); } else if (rightCollapsed) { setRightCollapsed(false); } } prevRunningRef.current = running; }, [running, rightCollapsed, isDesktop, band]); // Close any open drawer as soon as we leave mobile layout, so switching // from portrait to landscape doesn't leave a floating panel hanging. useEffect(() => { if (isDesktop) { setLeftDrawerOpen(false); setRightDrawerOpen(false); } }, [isDesktop]); // Mount + window-resize clamp → shrink oversized panes back into // the safe zone (RESPONSIVE CONTRACT). The drag-time clamp keeps // users from making things too wide while dragging; this covers (a) // the user shrinking the window afterwards and (b) BOOTING at a // narrower viewport than the persisted widths came from — (b) is why // it runs once immediately, not only on resize events. useEffect(() => { if (!isDesktop) return; const clamp = () => { const ww = window.innerWidth; // In the band the right pane is always its strip in flow (the // expanded pane is an overlay, costing no layout width). const rightInFlow = band || rightCollapsed ? COLLAPSED_W : rightW + RESIZER_W; const leftInFlow = leftCollapsed ? COLLAPSED_W : leftW + RESIZER_W; // Headroom each pane has, given the *other* pane's in-flow // footprint, its own resizer and the canvas floor. const maxLeft = Math.min( LEFT_MAX, ww - rightInFlow - RESIZER_W - MIN_CANVAS_W, ); const maxRight = Math.min( RIGHT_MAX, ww - leftInFlow - RESIZER_W - MIN_CANVAS_W, ); setLeftW((w) => Math.max(LEFT_MIN, Math.min(w, Math.max(LEFT_MIN, maxLeft)))); setRightW((w) => Math.max(RIGHT_MIN, Math.min(w, Math.max(RIGHT_MIN, maxRight)))); }; clamp(); window.addEventListener("resize", clamp); return () => window.removeEventListener("resize", clamp); }, [isDesktop, band, leftCollapsed, rightCollapsed, leftW, rightW]); const setPlugins = useApp((s) => s.setPlugins); useEffect(() => { api .health() .then((h) => { setHealth(h); setReady(true); }) .catch(() => setReady(true)); // Fetch this browser's plugin manifests (the plugin protocol is a // paper claim — the execution path stays fully functional even // though the upload UI was removed in Wave P; uploads go via API). const refreshPlugins = () => { api .listPlugins(getUserId()) .then(setPlugins) .catch(() => { /* ignore */ }); }; refreshPlugins(); // Multi-tab sync: an upload/delete in tab A bumps pluginsRev → // tab B refetches its plugin list. const onStorage = (e: StorageEvent) => { if (e.key === "quda.pluginsRev") refreshPlugins(); }; window.addEventListener("storage", onStorage); return () => window.removeEventListener("storage", onStorage); }, [setHealth, setPlugins]); // Boot sequencing: (1) first visit seeds the browser's empty run // archive with the bundled demo evidence (real seeded runs; see // lib/demoArchive.ts); (2) a ?scenario=Fn URL (Wave P figure // states) takes over the whole UI state afterwards. IA inversion // (board = home): the Evidence board is the STORE's default // workspace — a first visit has no quda.workspaceMode preference // and the mode is read synchronously at store creation, so the // board paints from the very first frame and the demo import no // longer forces a landing (the fresh-import path and the plain // first-visit path are the same path). Returning users boot into // their persisted mode the same way. A scenario's mode write lands // after this effect resolves, so scenario still wins over both. const bootedRef = useRef(false); useEffect(() => { // StrictMode dev-mounts run effects twice on one component // instance; the boot sequence (demo import + scenario auto-run) // must fire exactly once (audit S3). Refs survive the re-invoke. if (bootedRef.current) return; bootedRef.current = true; const scenarioKey = new URLSearchParams(window.location.search).get( "scenario", ); // Scenario first (audit S3, mode-flash): activateScenario writes // the scripted workspace mode synchronously before its own awaits, // and force-imports the archive itself when the figure needs it. // The plain ensureDemoArchive afterwards is idempotent (first-visit // seeding for scenarios that don't need the archive). Unknown keys // fall through to a normal boot — loudly, not silently (audit S3). void (async () => { if (scenarioKey) { const ok = await activateScenario(scenarioKey); if (!ok) console.warn( `Unknown scenario key "?scenario=${scenarioKey}" — booting normally (known keys: F0..F8).`, ); } await ensureDemoArchive(); })(); }, []); const leftWidth = leftCollapsed ? COLLAPSED_W : leftW; // In the band the Evidence pane's in-flow footprint is ALWAYS the // strip; the expanded pane is an overlay (RESPONSIVE CONTRACT). const rightStrip = band || rightCollapsed; const rightWidth = rightStrip ? COLLAPSED_W : rightW; return ( // h-[100dvh] uses the dynamic viewport height — iOS Safari shrinks // 100vh by the address bar, clipping ~80px of UI off the bottom. // 100dvh tracks the actually-visible area. We keep w-screen since // viewport WIDTH isn't dynamic.
setLeftDrawerOpen(true)} onOpenRightDrawer={() => setRightDrawerOpen(true)} onOpenTour={() => setTourOpen(true)} /> {isDesktop ? ( /* ===================== Desktop ===================== */
{!leftCollapsed && ( setLeftW((w) => { // Dynamic upper bound: never push left pane so wide that // the canvas falls below MIN_CANVAS_W. Account for the // right pane's current footprint (collapsed strip when // collapsed, full width otherwise) and both resizers. const rightFoot = rightStrip ? COLLAPSED_W : rightW + RESIZER_W; const dynamicMax = Math.min( LEFT_MAX, window.innerWidth - rightFoot - RESIZER_W - MIN_CANVAS_W, ); return Math.min( Math.max(LEFT_MIN, dynamicMax), Math.max(LEFT_MIN, w + dx), ); }) } onDoubleClick={() => setLeftW(LEFT_DEFAULT)} onDragChange={setIsResizing} ariaLabel="Resize pipeline input pane" /> )}
{/* Multiverse overlay: FlowCanvas stays mounted underneath so the in-progress graph and its pendingRestore watcher survive mode flips (the board's Open action depends on that watcher firing while the canvas is covered). */} {workspaceMode === "multiverse" && (
)} {/* Evidence theater: the steering view, overlaid above BOTH the canvas and the multiverse board (z-30 > z-20) — auto-opens on the first progress frame of a streaming sampled step; the canvas stays mounted underneath so the run keeps streaming into it. */} {theaterOpen && (
)} {/* Learn-from-zero overlay (marker: learn-lab) — same center-column pattern; mounted LAST so it paints above the theater when both are up (equal z-30, later DOM order wins). Toy sim only — nothing underneath runs. */} {learnLabOpen && (
)}
{!rightStrip && ( setRightW((w) => { // Dynamic upper bound: never push right pane so wide // that the canvas falls below MIN_CANVAS_W. Mirror // logic of the left resizer. const leftFoot = leftCollapsed ? COLLAPSED_W : leftW + RESIZER_W; const dynamicMax = Math.min( RIGHT_MAX, window.innerWidth - leftFoot - RESIZER_W - MIN_CANVAS_W, ); return Math.min( Math.max(RIGHT_MIN, dynamicMax), Math.max(RIGHT_MIN, w - dx), ); }) } onDoubleClick={() => setRightW(RIGHT_DEFAULT)} onDragChange={setIsResizing} ariaLabel="Resize evidence pane" /> )} {/* Band Evidence overlay (RESPONSIVE CONTRACT): expanded Evidence floats over the center column instead of taking layout width, so the canvas/board keep computing layout from real viewport space and the page can never scroll horizontally. Width: the persisted pane width, capped so a sliver of the center column stays visible. */} {band && bandEvidenceOpen && (
setBandEvidenceOpen(false)} />
)}
) : ( /* ===================== Mobile ====================== */
{/* Multiverse overlay: FlowCanvas stays mounted underneath so the in-progress graph and its pendingRestore watcher survive mode flips (the board's Open action depends on that watcher firing while the canvas is covered). */} {workspaceMode === "multiverse" && (
)} {/* Evidence theater: the steering view, overlaid above BOTH the canvas and the multiverse board (z-30 > z-20) — auto-opens on the first progress frame of a streaming sampled step; the canvas stays mounted underneath so the run keeps streaming into it. */} {theaterOpen && (
)} {/* Learn-from-zero overlay (marker: learn-lab) — same center-column pattern; mounted LAST so it paints above the theater when both are up (equal z-30, later DOM order wins). Toy sim only — nothing underneath runs. */} {learnLabOpen && (
)}
setLeftDrawerOpen(false)} side="left" title="Pipeline input" > setRightDrawerOpen(false)} side="right" title="Evidence" >
)} setTourOpen(false)} /> {/* Guided lessons overlay — small bottom-right card (z-40); not a mode, floats over whichever view the active lesson step drove the app into (store flag lessonsOpen; see LessonCard). */} {!ready && ( // z-50: the boot overlay must paint over EVERYTHING (board // z-20, theater z-30, header z-40) — without it the connecting // state showed through whichever surface stacked higher // (audit S2).
Connecting to compute backend…
)}
); } /** * Narrow vertical strip shown when an aside pane is collapsed. * * Clicking anywhere on it expands the pane. The chevron sits near the top * and the label is rotated 90° to fill the strip. `side` controls which * edge the chevron points towards — a left-side strip points right (come * back out to the right) and a right-side strip points left. */ function CollapsedStrip({ label, side, onExpand, }: { label: string; side: "left" | "right"; onExpand: () => void; }) { // Use the same panel-toggle icons the mobile TopBar uses for its // drawer toggles, so the visual cue for "open this side panel" is // consistent across breakpoints. PanelLeft = left edge highlight; // PanelRight = right edge highlight — picks the one that points // *toward* the pane that's about to slide open. const Icon = side === "left" ? PanelLeft : PanelRight; return ( ); } /** * 4-px wide vertical drag handle between two panes. * * - Report deltas in page pixels via onResize(dx) where dx is the movement * relative to the mousedown position. The parent decides whether to add * or subtract dx depending on which edge the handle sits on. * - Double-click snaps the parent's width back to its default. * - onDragChange (optional) fires once with `true` on mousedown and once * with `false` on mouseup. App.tsx uses it to suppress the * `transition-[width]` on the aside while dragging — every mousemove * updates state, and an active CSS transition would lag the visual * width behind the cursor while the inner content is already laid out * at the new width, briefly overflowing the aside. */ function PaneResizer({ onResize, onDoubleClick, onDragChange, ariaLabel, }: { onResize: (dx: number) => void; onDoubleClick?: () => void; onDragChange?: (resizing: boolean) => void; ariaLabel: string; }) { const dragging = useRef(false); const lastX = useRef(0); // Pointer events, not mouse events (audit S3): identical drag code // path for mouse, touch and pen; pointercancel ends a drag the OS // interrupts. touch-action:none (className) keeps mobile browsers // from claiming the gesture for scrolling. const onPointerMove = useCallback( (e: PointerEvent) => { if (!dragging.current) return; const dx = e.clientX - lastX.current; lastX.current = e.clientX; if (dx !== 0) onResize(dx); }, [onResize], ); const onPointerUp = useCallback(() => { dragging.current = false; document.body.style.cursor = ""; document.body.style.userSelect = ""; onDragChange?.(false); window.removeEventListener("pointermove", onPointerMove); window.removeEventListener("pointerup", onPointerUp); window.removeEventListener("pointercancel", onPointerUp); }, [onPointerMove, onDragChange]); const onPointerDown = (e: React.PointerEvent) => { e.preventDefault(); dragging.current = true; lastX.current = e.clientX; document.body.style.cursor = "col-resize"; document.body.style.userSelect = "none"; onDragChange?.(true); window.addEventListener("pointermove", onPointerMove); window.addEventListener("pointerup", onPointerUp); window.addEventListener("pointercancel", onPointerUp); }; return (
{ if (e.key === "ArrowLeft") { e.preventDefault(); onResize(-16); } else if (e.key === "ArrowRight") { e.preventDefault(); onResize(16); } else if (e.key === "Enter") { onDoubleClick?.(); } }} className="group shrink-0 w-1 cursor-col-resize touch-none bg-edge/40 hover:bg-accent/60 active:bg-accent focus-visible:bg-accent focus-visible:outline-none transition-colors" /> ); } /** * Global toast host (audit backlog: NodePalette's alert() was the one * hold-out against the toast idiom). Renders the store's * `globalNotice` fixed to the bottom-center of the VIEWPORT at z-50 * (above drawers and both overlays) — the channel for feedback from * components that don't own a positional toast. The canvas keeps its * own FlowCanvas-local Notice: that one is positionally contextual * (anchored inside the canvas, clearing the Run FAB) and works. * Styling mirrors that canvas toast: same tone palette, auto-fade * ok=4s / warn=8s, danger sticky until explicitly dismissed (every * tone gets the x). */ function GlobalToastHost() { const notice = useApp((s) => s.globalNotice); const setGlobalNotice = useApp((s) => s.setGlobalNotice); useEffect(() => { if (!notice || notice.tone === "danger") return; const ms = notice.tone === "ok" ? 4000 : 8000; const t = window.setTimeout(() => setGlobalNotice(null), ms); return () => window.clearTimeout(t); }, [notice, setGlobalNotice]); if (!notice) return null; const palette = { ok: { border: "border-ok/40", bg: "bg-ok/10", icon: , }, warn: { border: "border-warn/40", bg: "bg-warn/10", icon: , }, danger: { border: "border-danger/40", bg: "bg-danger/10", icon: , }, }[notice.tone]; return (
{palette.icon}
{notice.text}
); }