Download web/src/components/Layout.tsx from EnergyVenom/patch-testing-coworker: direct link, hf CLI and curl.
- Browser
- Download file 7.95 kB
-
https://huggingface.co/spaces/EnergyVenom/patch-testing-coworker/resolve/main/web/src/components/Layout.tsx
- Command line
-
hf download hf://spaces/EnergyVenom/patch-testing-coworker/web/src/components/Layout.tsx
-
curl -L -o Layout.tsx https://huggingface.co/spaces/EnergyVenom/patch-testing-coworker/resolve/main/web/src/components/Layout.tsx
7.95 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 '../desktop'; | |
| import { useAsk } from '../chat/useAsk'; | |
| import { StartRunSheet } from '../pages/StartRunWizard'; | |
| import { CommandPalette } from './CommandPalette'; | |
| import { ScreenErrorBoundary } from './ErrorPages'; | |
| import { KeyboardSheet } from './KeyboardSheet'; | |
| import { AppSidebar } from './shell/AppSidebar'; | |
| import { IconRail } from './shell/IconRail'; | |
| import { ShellCtx, UiPrefsProvider, useShellSize, useUiPrefs, 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 | |
| ? '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 { prefs, set } = useUiPrefs(); | |
| 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 inline = size === 'desktop' || size === 'laptop'; | |
| useEffect(() => { if (inline) setDrawer(false); }, [inline]); | |
| const toggleSidebar = useCallback(() => set({ sidebar: prefs.sidebar === 'expanded' ? 'rail' : 'expanded' }), [prefs.sidebar, set]); | |
| 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 sidebarOpen = inline && prefs.sidebar === 'expanded'; | |
| const shell: ShellState = useMemo(() => ({ | |
| size, sidebarExpanded: inline ? sidebarOpen : drawer, toggleSidebar, openNav: () => setDrawer(true), openPalette: () => setPalette(true), openShortcuts: () => setKeys(true), newRun, chat, | |
| }), [size, inline, sidebarOpen, 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={sidebarOpen ? 'open' : 'closed'}> | |
| {inline && (sidebarOpen | |
| ? ( | |
| <aside className="fpt-sidebar" data-mode="open" aria-label="Main navigation"> | |
| <AppSidebar /> | |
| </aside> | |
| ) | |
| // folded: the pages as icons, never nothing ([ or the toolbar button opens it again) | |
| : ( | |
| <aside className="pw-rail-aside" aria-label="Main navigation"> | |
| <IconRail /> | |
| </aside> | |
| ))} | |
| {!inline && ( | |
| <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)} /> | |
| </div> | |
| </ShellCtx.Provider> | |
| ); | |
| } | |
| export function Layout() { | |
| return <UiPrefsProvider><Shell /></UiPrefsProvider>; | |
| } | |