// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The frame of a system app's window (sysapps.ts: Settings and System): a // section list and one section shown at a time, with a footer of actions. // - Wide windows (from WIDE px, measured on the window, not the viewport) get // the list as a sidebar on the left; narrow ones (phones, a small window) a // row of segments over the section, which scrolls sideways when it must. // - The list is ARIA tabs (vertical in the sidebar): one tab stop, the arrow // keys (either axis), Home and End move between sections, and the focus // follows the selection. Each section is a tabpanel with a real heading. // The list has class `tabs` and each tab data-tab (its section, the old // dialog's tab names where it had them), which the browser tests click. // - One scroll container for the sections (a new section starts at its top; // System's kernel log keeps itself at its end), passed to the sections that // follow it. // - A section with changes not applied yet has a dot after its name. // - Coarse pointers get taller tabs (and style.css taller buttons and fields). import { useEffect, useLayoutEffect, useRef, useState, useSyncExternalStore, type KeyboardEvent, type ReactNode, type RefObject } from "react"; import { useSize } from "../hooks.ts"; import { apps$, selectSection, type AppId } from "../sysapps.ts"; import type { Tab } from "../store.ts"; /** From this width (px) the section list is a sidebar. */ const WIDE = 600; export interface SectionDef { id: Tab; name: string; icon: ReactNode; /** A count after the name (System's errors), with its element id. */ badge?: { text: string; id?: string; tone?: "err" }; /** A line under the heading. */ desc?: ReactNode; /** It has changes not applied yet (Settings: options that take a reboot): a dot after the name. */ changed?: boolean; } /** A sub-heading in a section: ── Title ─────── */ export const H3 = ({ children }: { children: ReactNode }) => (

{children}

); /** A key, as the sections show them. */ export const K = ({ children }: { children: ReactNode }) => {children}; /** The app's current section (re-renders when it changes). */ export function useSection(app: AppId): Tab { return useSyncExternalStore(apps$.subscribe, () => apps$.get()[app]); } export function AppShell({ app, label, sections, render, footer }: { app: AppId; label: string; sections: SectionDef[]; /** A section's content; `body` is the scroll container. */ render: (id: Tab, body: RefObject) => ReactNode; footer?: ReactNode; }) { const section = useSection(app); const [root, setRoot] = useState(null); const list = useRef(null); const body = useRef(null); // Measured on the window; a hidden one (width 0: minimized, parked) keeps its layout. const { w } = useSize(root); const [wide, setWide] = useState(false); if (w && (w >= WIDE) !== wide) setWide(w >= WIDE); // A new section starts at its top: in a layout effect, so before the sections' own effects (System's kernel log // puts itself at its end in one, which a reset after it would undo). useLayoutEffect(() => { if (body.current) body.current.scrollTop = 0; }, [section]); // Focus on a tab follows the selection (arrows, shortcuts), as tabs do. useEffect(() => { const tab = list.current?.querySelector(`[data-tab="${section}"]`); if (list.current?.contains(document.activeElement)) tab?.focus({ preventScroll: true }); tab?.scrollIntoView?.({ block: "nearest", inline: "nearest" }); }, [section]); const onKey = (e: KeyboardEvent) => { if (e.ctrlKey || e.altKey || e.metaKey) return; const i = Math.max(0, sections.findIndex((s) => s.id === section)), n = sections.length; const to = ({ ArrowUp: i - 1, ArrowLeft: i - 1, ArrowDown: i + 1, ArrowRight: i + 1, Home: 0, End: n - 1 } as Record)[e.key]; if (to === undefined) return; e.preventDefault(); selectSection(sections[(to + n) % n]!.id); }; return (
{sections.map((s) => { const on = s.id === section; return ( ); })}
{sections.map((s) => ( ))}
{footer && (
{footer}
)}
); }