EnergyVenom's picture
Astra: PO/GL actions, usage coverage, assisted sign-in, self-healing, recorder, new top navigation and theme (172a4e22a2bf)
fb62a97 verified
Raw History Blame Contribute Delete
7.34 kB
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<Record<string, string> | 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 (
<div className="fpt-savednotice" role="alert" data-testid="saved-state-notice">
{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.'}
</div>
);
}
/** 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 (
<div className="trio-globalbanners">
{kill.data?.engaged && <div className="trio-banner" data-tone="red" role="status"><b>Emergency stop is on.</b> New test runs are refused until an administrator releases it.</div>}
{down && <div className="trio-banner" data-tone="orange" role="status"><b>The practice app is not answering.</b> Test runs on the practice app cannot start until it is back.</div>}
</div>
);
}
const typing = (el: EventTarget | null) => el instanceof HTMLElement && (el.isContentEditable || ['INPUT', 'TEXTAREA', 'SELECT'].includes(el.tagName));
const GO: Record<string, string> = { 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<number | null>(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 (
<ShellCtx.Provider value={shell}>
<div className="fpt-shell" data-size={size} data-sb={drawer ? 'open' : 'closed'} data-nav="top">
<TopNav />
<Drawer opened={drawer} onClose={() => setDrawer(false)} position="left" size={size === 'phone' ? 'min(300px, 86vw)' : 288} withCloseButton={false} padding={0}
title={<span className="fpt-sr-only">Main navigation</span>}
styles={{ header: { padding: 0, minHeight: 0 }, body: { height: '100%', padding: 0 }, content: { background: 'var(--mat-sidebar-solid)', borderRadius: 0 } }}>
<AppSidebar onNavigate={() => setDrawer(false)} />
</Drawer>
<main className="fpt-main" data-reading={!runRoute || undefined}>
{desktop() && <DesktopNotifier />}
<SavedStateNotice />
<GlobalBanners />
<ScreenErrorBoundary resetKey={loc.pathname}><Suspense fallback={<Center py={80}><Loader aria-label="Loading the page" /></Center>}><Outlet /></Suspense></ScreenErrorBoundary>
</main>
<StartRunSheet />
<CommandPalette opened={palette} onClose={() => setPalette(false)} />
<KeyboardSheet opened={keys} onClose={() => setKeys(false)} />
<CursorFx />
</div>
</ShellCtx.Provider>
);
}
export function Layout() {
return <UiPrefsProvider><Shell /></UiPrefsProvider>;
}