import { envKind, useChosenEnv } from './shell/EnvSwitcher'; import { Center, Drawer, Loader } from '@mantine/core'; import { Suspense, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { Outlet, useLocation, useNavigate, useSearchParams } from 'react-router-dom'; import { useHealth, useKill, useRuns, useSavedState } from '../api/hooks'; import { useAuth } from '../auth/AuthContext'; import { desktop, statusTransitions } from '../domain/desktop'; import { useAsk } from '../chat/useAsk'; import { StartRunSheet } from '../pages/runs/StartRunWizard'; import { CommandPalette } from './CommandPalette'; import { ScreenErrorBoundary } from './ErrorPages'; import { KeyboardSheet } from './KeyboardSheet'; import { AppSidebar } from './shell/AppSidebar'; import { CursorFx } from './shell/CursorFx'; import { TopNav } from './shell/TopNav'; import { ShellCtx, UiPrefsProvider, useShellSize, type ShellState } from './shell/prefs'; function DesktopNotifier() { const runs = useRuns(30, 0); const prev = useRef | null>(null); useEffect(() => { if (!runs.data) return; const next = Object.fromEntries(runs.data.items.map((r) => [r.run_id, r.attention ? `${r.status}|${r.attention}` : r.status])); if (prev.current) for (const n of statusTransitions(prev.current, next)) desktop()?.notify(n.title, n.body); prev.current = next; }, [runs.data]); return null; } /** Shown only when saving to backup storage has stopped: the person must know before a restart can lose their work. */ function SavedStateNotice() { const s = useSavedState().data; if (!s || !s.on || (s.ok !== false && !s.paused)) return null; return (
{s.paused && s.paused_reason === 'fenced' ? 'Saving has stopped: a newer copy of the app has started and saved since this one did, so this one will not overwrite it. Reload the page in a minute to reach the newer copy; nothing you do here is saved.' : s.paused ? 'Saving is paused: this instance could not read its saved data when it started, so it will not overwrite it. Restart the Space to try again; nothing you do now is saved.' : 'The latest changes could not be saved to backup storage. They will be lost if the app restarts before this clears.'}
); } /** Banners that belong to the whole app: the emergency stop, and the practice app not answering (only when the practice * app is the chosen or default test environment: it says nothing about an Oracle Fusion pod). */ function GlobalBanners() { const kill = useKill(); const health = useHealth(); const env = useChosenEnv().pod; const down = !!health.data && !health.data.mock_up && !!env && envKind(env.kind) === 'mock'; if (!kill.data?.engaged && !down) return null; return (
{kill.data?.engaged &&
Emergency stop is on. New test runs are refused until an administrator releases it.
} {down &&
The practice app is not answering. Test runs on the practice app cannot start until it is back.
}
); } const typing = (el: EventTarget | null) => el instanceof HTMLElement && (el.isContentEditable || ['INPUT', 'TEXTAREA', 'SELECT'].includes(el.tagName)); const GO: Record = { h: '/', r: '/runs', s: '/scenarios', d: '/tickets', e: '/pods', p: '/reports' }; function Shell() { const { can } = useAuth(); const size = useShellSize(); const loc = useLocation(); const nav = useNavigate(); const [, setParams] = useSearchParams(); const chat = useAsk(); const [drawer, setDrawer] = useState(false); // tablet / phone: the menu as a drawer const [palette, setPalette] = useState(false); const [keys, setKeys] = useState(false); useEffect(() => { setDrawer(false); }, [loc.pathname, loc.search]); const toggleSidebar = useCallback(() => setDrawer((v) => !v), []); const newRun = useCallback((area?: string) => { setParams((p) => { const n = new URLSearchParams(p); n.set('new', '1'); if (area) n.set('area', area); return n; }); }, [setParams]); // single-key shortcuts work when focus is not in a field and no sheet is open; Ctrl or Command + K works anywhere const pending = useRef(null); // "g" was pressed: the next letter goes somewhere useEffect(() => { const onKey = (e: KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && !e.altKey && e.key.toLowerCase() === 'k') { e.preventDefault(); setPalette((v) => !v); return; } if (e.ctrlKey || e.metaKey || e.altKey || typing(e.target) || document.querySelector('[role="dialog"]')) return; const k = e.key; if (pending.current) { window.clearTimeout(pending.current); pending.current = null; const to = GO[k.toLowerCase()]; if (to) { e.preventDefault(); nav(to); } return; } if (k === 'g' || k === 'G') { pending.current = window.setTimeout(() => { pending.current = null; }, 1200); return; } if (k === '/') { e.preventDefault(); setPalette(true); } else if (k === '?') { e.preventDefault(); setKeys(true); } else if (k === '[') { e.preventDefault(); toggleSidebar(); } else if ((k === 'n' || k === 'N') && can('run.start')) { e.preventDefault(); newRun(); } }; window.addEventListener('keydown', onKey); return () => window.removeEventListener('keydown', onKey); }, [can, nav, newRun, toggleSidebar]); const shell: ShellState = useMemo(() => ({ size, sidebarExpanded: drawer, toggleSidebar, openNav: () => setDrawer(true), openPalette: () => setPalette(true), openShortcuts: () => setKeys(true), newRun, chat, }), [size, drawer, toggleSidebar, newRun, chat]); const runRoute = /^\/(runs\/[^/]+|assistant)/.test(loc.pathname); return (
setDrawer(false)} position="left" size={size === 'phone' ? 'min(300px, 86vw)' : 288} withCloseButton={false} padding={0} title={Main navigation} styles={{ header: { padding: 0, minHeight: 0 }, body: { height: '100%', padding: 0 }, content: { background: 'var(--mat-sidebar-solid)', borderRadius: 0 } }}> setDrawer(false)} />
{desktop() && } }>
setPalette(false)} /> setKeys(false)} />
); } export function Layout() { return ; }