import { useMediaQuery } from '@mantine/hooks'; import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from 'react'; import { useAuth } from '../../auth/AuthContext'; import type { AskState } from '../../chat/useAsk'; export type PanelTab = 'live' | 'screens' | 'report' | 'excel' | 'steps'; export const PANEL_TABS: PanelTab[] = ['live', 'screens', 'report', 'excel', 'steps']; /** Per-user layout choices, kept in this browser only (nothing important lives here). */ export interface UiPrefs { /** 'expanded' = the sidebar shows; 'rail' = hidden (kept under its old name so saved layouts still load). */ sidebar: 'expanded' | 'rail'; panel: { open: boolean; width: number | null; tab: PanelTab }; /** Id of the environment chosen in the environment switcher: the preselected pod for new test runs. */ env: number | null; } export const DEFAULT_PREFS: UiPrefs = { sidebar: 'expanded', panel: { open: true, width: null, tab: 'live' }, env: null }; const storeKey = (user?: string | null) => `fpt.ui.${user ?? ''}`; /** Reads the saved prefs; any missing, broken or blocked storage gives the defaults. */ export function readPrefs(user?: string | null): UiPrefs { try { const raw = window.localStorage.getItem(storeKey(user)); if (!raw) return DEFAULT_PREFS; const p = JSON.parse(raw) as Partial; const panel = { ...DEFAULT_PREFS.panel, ...(p.panel && typeof p.panel === 'object' ? p.panel : {}) }; if (!PANEL_TABS.includes(panel.tab)) panel.tab = 'live'; if (typeof panel.width !== 'number' || !Number.isFinite(panel.width)) panel.width = null; return { sidebar: p.sidebar === 'rail' ? 'rail' : 'expanded', panel: { open: panel.open !== false, width: panel.width, tab: panel.tab }, env: typeof p.env === 'number' ? p.env : null, }; } catch { return DEFAULT_PREFS; } } function writePrefs(user: string | null | undefined, prefs: UiPrefs) { try { window.localStorage.setItem(storeKey(user), JSON.stringify(prefs)); } catch { /* private mode or blocked storage */ } } type PrefsPatch = Partial> & { panel?: Partial }; interface PrefsState { prefs: UiPrefs; set: (patch: PrefsPatch) => void } const PrefsCtx = createContext({ prefs: DEFAULT_PREFS, set: () => undefined }); export function UiPrefsProvider({ children }: { children: ReactNode }) { const user = useAuth().user?.username ?? null; const [prefs, setPrefs] = useState(() => readPrefs(user)); useEffect(() => setPrefs(readPrefs(user)), [user]); const set = useCallback((patch: PrefsPatch) => { setPrefs((p) => { const next: UiPrefs = { ...p, ...patch, panel: { ...p.panel, ...(patch.panel ?? {}) } }; writePrefs(user, next); return next; }); }, [user]); const value = useMemo(() => ({ prefs, set }), [prefs, set]); return {children}; } /** Per-user UI prefs (localStorage `fpt.ui.`). Outside the app shell it returns the defaults. */ export function useUiPrefs() { return useContext(PrefsCtx); } export type ShellSize = 'desktop' | 'laptop' | 'tablet' | 'phone'; /** Desktop >= 1280, laptop 1024-1279, tablet 768-1023, phone < 768. */ export function useShellSize(): ShellSize { const o = { getInitialValueInEffect: false }; const phone = useMediaQuery('(max-width: 767px)', false, o); const tablet = useMediaQuery('(max-width: 1023px)', false, o); const laptop = useMediaQuery('(max-width: 1279px)', false, o); return phone ? 'phone' : tablet ? 'tablet' : laptop ? 'laptop' : 'desktop'; } /** What pages need from the app shell: the screen size, the sidebar, the menu drawer, the command palette, Ask. */ export interface ShellState { size: ShellSize; /** The sidebar is showing (inline on desktop and laptop; the drawer opens through openNav on smaller screens). */ sidebarExpanded: boolean; toggleSidebar: () => void; openNav: () => void; openPalette: () => void; openShortcuts: () => void; /** Open the Start-a-test-run sheet on the current page (the `new=1` search parameter). */ newRun: (area?: string) => void; /** Go to the Ask page with what the person is looking at as context (and, optionally, a question). */ chat: AskState | null; } export const ShellCtx = createContext({ size: 'desktop', sidebarExpanded: true, toggleSidebar: () => undefined, openNav: () => undefined, openPalette: () => undefined, openShortcuts: () => undefined, newRun: () => undefined, chat: null, }); export const useShell = () => useContext(ShellCtx);