File size: 4,742 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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<UiPrefs>;
    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<Omit<UiPrefs, 'panel'>> & { panel?: Partial<UiPrefs['panel']> };
interface PrefsState { prefs: UiPrefs; set: (patch: PrefsPatch) => void }

const PrefsCtx = createContext<PrefsState>({ prefs: DEFAULT_PREFS, set: () => undefined });

export function UiPrefsProvider({ children }: { children: ReactNode }) {
  const user = useAuth().user?.username ?? null;
  const [prefs, setPrefs] = useState<UiPrefs>(() => 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 <PrefsCtx.Provider value={value}>{children}</PrefsCtx.Provider>;
}

/** Per-user UI prefs (localStorage `fpt.ui.<username>`). 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<ShellState>({
  size: 'desktop', sidebarExpanded: true, toggleSidebar: () => undefined, openNav: () => undefined, openPalette: () => undefined, openShortcuts: () => undefined, newRun: () => undefined, chat: null,
});
export const useShell = () => useContext(ShellCtx);