import { useSyncExternalStore, type ComponentType, type JSXElementConstructor, type ReactNode } from 'react'; import { isObject } from '@/lib/objects'; import type { SlotName } from '@/extensions/registry'; import { settledLoad } from './lazy'; /** Where a view can render; `app` covers every signed-in area. */ export type ViewContext = 'app' | 'server' | 'account' | 'admin' | 'auth'; export type ViewKind = 'layout' | 'page' | 'block'; /** The model of every engine view by name. Definition modules add their entries. */ export interface ViewModels {} /** Props a view takes besides its model, such as the server of a card. */ export interface ViewInputs {} export type ViewName = Extract; export type ViewModel = N extends keyof ViewModels ? ViewModels[N] : unknown; // oxlint-disable-next-line rules/no-unsafe-dictionary-type -- a view name erased to `string` takes any inputs. export type LooseViewInput = Readonly>; export type ViewInput = N extends keyof ViewInputs ? ViewInputs[N] : N extends keyof ViewModels ? unknown : // oxlint-disable-next-line rules/no-unsafe-dictionary-type -- a view name erased to `string` takes any inputs. LooseViewInput; /** `Default` renders the layer below; every prop is optional and replaces what this layer received. */ export interface DefaultViewProps { readonly model?: ViewModel; readonly className?: string; readonly children?: ReactNode; } export type ViewProps = { readonly model: ViewModel; readonly Default: ComponentType>; readonly className?: string; readonly children?: ReactNode; } & ViewInput; /** A layer of a view. */ export type ViewComponent = ComponentType>; /** The hook of a view its parent places with the parent's model, such as a piece of a card. */ export const modelFromInput = ({ model }: { readonly model: M }): M => model; /** The engine side of a view: the hook producing its model, and the fixtures standing in for it. */ export interface ViewImplementation { /** The engine hook producing the model; the only data source of the view. */ use(input: ViewInput): ViewModel; /** Models for tests; the first is the default. */ readonly fixtures?: Readonly ViewModel>>; /** The inputs a fixture renders with, for views that take inputs. */ fixtureInput?(model: ViewModel): ViewInput; /** The model of a view placed with inputs from fixtures, such as a card for each server of a list fixture. */ fixtureFromInput?(input: ViewInput): ViewModel; } /** What a module passes to `implementViews`, by view name. */ export type ViewImplementations = { readonly [N in ViewName]?: ViewImplementation }; /** Imports the module implementing a view; an area's views share one, so it loads once for all of them. */ export type ViewLoader = () => Promise; interface ViewDeclaration { readonly context: ViewContext; readonly slots?: readonly SlotName[]; /** Shown while extension layers of this view load. */ readonly placeholder?: ReactNode; } /** A view implemented where it is defined, or by the module `load` imports when the view is first needed. */ export type ViewOptions = ViewDeclaration & (ViewImplementation | { readonly load: ViewLoader }); export interface ViewDefinition extends ViewDeclaration { readonly name: N; readonly kind: ViewKind; /** Unset until the module `load` imports has implemented the view. */ use?(input: ViewInput): ViewModel; readonly fixtures?: Readonly ViewModel>>; fixtureInput?(model: ViewModel): ViewInput; fixtureFromInput?(input: ViewInput): ViewModel; readonly load?: ViewLoader; } export type ViewLayerSource = 'classic' | 'theme' | 'extension'; export interface ViewLayer { /** The package the layer belongs to, which its failures are attributed to. */ readonly id: string; readonly source: ViewLayerSource; readonly component: JSXElementConstructor; } export type ViewImporter = () => Promise<{ default: ViewComponent }>; /** An extension layer: a component, or an importer loaded while the view shows its placeholder. */ export type ViewOverride = ViewComponent | { readonly load: ViewImporter }; /** Views a theme or extension provides, keyed by name. */ export type ThemeViews = { readonly [N in ViewName]?: ViewComponent }; interface LayerSet { base?: ViewLayer; themes: ViewLayer[]; } const definitions = new Map(); /** Implementations by view name, kept for a definition registered after its module ran. */ const implementations = new Map>(); const loads = new WeakMap>(); const layers = new Map(); const chains = new Map(); const listeners = new Set<() => void>(); const emptyChain: readonly ViewLayer[] = Object.freeze([]); let sortedDefinitions: readonly ViewDefinition[] | undefined; function notify(): void { for (const listener of listeners) { listener(); } } // Chains are kept when only definitions change, so an area loading its views does not re-render mounted ones. function publishDefinitions(): void { sortedDefinitions = undefined; notify(); } function publish(): void { chains.clear(); publishDefinitions(); } export function subscribeViews(listener: () => void): () => void { listeners.add(listener); return () => { listeners.delete(listener); }; } export function viewKind(name: string): ViewKind { if (name.startsWith('layout.')) { return 'layout'; } return name.startsWith('page.') ? 'page' : 'block'; } /** Core definitions replace an earlier one of the same name, so a hot reload re-registers cleanly. */ export function defineView(name: N, options: ViewOptions): ViewDefinition { const definition: ViewDefinition = Object.freeze({ ...options, name, kind: viewKind(name) }); // The inputs of are checked against the definition where it is rendered. A module // that ran before the definition has implemented the view already. definitions.set(name, Object.freeze({ ...(definition as ViewDefinition), ...implementations.get(name) })); publishDefinitions(); return definition; } /** Called by the module a view's `load` imports, which implements every view of its area. */ export function implementViews(views: ViewImplementations): void { for (const [name, implementation] of Object.entries(views)) { // Each implementation is typed by its view's name where it is written. const erased = implementation as ViewImplementation; const definition = definitions.get(name); implementations.set(name, erased); if (definition) { definitions.set(name, Object.freeze({ ...definition, ...erased })); } } publishDefinitions(); } async function importImplementation(load: ViewLoader): Promise { try { await load(); } catch (cause) { // A chunk that failed to load is tried again the next time a view needs it. loads.delete(load); throw cause; } } /** * The import of the module implementing a view, started the first time one of its views needs it and * shared by all of them; null when the view was implemented without one. */ export function loadViewImplementation(name: string): Promise | null { const definition = definitions.get(name); if (!definition?.load) { return null; } const { load } = definition; let loading = loads.get(load); if (!loading && !definition.use) { loading = settledLoad(importImplementation(load)); loads.set(load, loading); } return loading ?? null; } export function getViewDefinition(name: string): ViewDefinition | undefined { return definitions.get(name); } export function getViewDefinitions(): readonly ViewDefinition[] { return (sortedDefinitions ??= Object.freeze( [...definitions.values()].sort((a, b) => a.name.localeCompare(b.name)) )); } export function useViewDefinition(name: string): ViewDefinition | undefined { return useSyncExternalStore(subscribeViews, () => definitions.get(name)); } function layerSet(name: string): LayerSet { let set = layers.get(name); if (!set) { set = { themes: [] }; layers.set(name, set); } return set; } /** A function component, or a memo, forwardRef or lazy wrapper of one. */ export function isViewComponent(value: T): value is T & JSXElementConstructor { return ( value instanceof Function || (isObject(value) && '$$typeof' in value && ['react.memo', 'react.forward_ref', 'react.lazy'].some((type) => Symbol.for(type) === value.$$typeof)) ); } function toLayer(id: string, source: ViewLayerSource, component: JSXElementConstructor): ViewLayer { return Object.freeze({ id, source, component }); } /** Classic is registered once at boot; theme layers stack in chain order, parent first. */ export function registerViewLayer(id: string, source: 'classic' | 'theme', views: ThemeViews): void { for (const [name, component] of Object.entries(views)) { if (!component) { continue; } const set = layerSet(name); const layer = toLayer(id, source, component); if (source === 'classic') { set.base = layer; } else { set.themes = [...set.themes.filter((existing) => existing.id !== id), layer]; } } publish(); } /** Drops every theme layer, leaving classic; used when a theme fails to boot. */ export function clearThemeLayers(): void { for (const set of layers.values()) { set.themes = []; } publish(); } /** Classic, then the theme chain; extension overrides come from the view session. */ export function getViewChain(name: string): readonly ViewLayer[] { let chain = chains.get(name); if (!chain) { const set = layers.get(name); chain = set ? Object.freeze([...(set.base ? [set.base] : []), ...set.themes]) : emptyChain; chains.set(name, chain); } return chain; } export function useViewChain(name: string): readonly ViewLayer[] { return useSyncExternalStore(subscribeViews, () => getViewChain(name)); }