import type { Window } from '@astrale-os/shell' /** * useWindows / useWindowList (API §10) — reactive views over the windows a * `ViewHost`/`AppHost` or the framework-neutral Shell mounts. The hub mirrors * Shell's immutable window-change feed, so credential, location, lifecycle, * and imperative mounts remain reactive through one snapshot authority. */ import { useMemo, useSyncExternalStore } from 'react' import { useShell } from '../../session/shell.hook.js' import { getStageHub } from '../hub.js' /** Every host-mounted window in this shell's tree, keyed by windowId. Reactive. */ export function useWindows(): ReadonlyMap { const shell = useShell() const hub = useMemo(() => getStageHub(shell), [shell]) return useSyncExternalStore( hub.registry.subscribe, hub.registry.getSnapshot, hub.registry.getSnapshot, ) } /** One open window as a list entry. */ export interface WindowListEntry { readonly windowId: string readonly window: Window } /** * The open windows as a reactive, insertion-ordered list — the array form of * `useWindows()`. The underlying map is referentially stable between mutations, * so the derived array is too (no render churn). */ export function useWindowList(): readonly WindowListEntry[] { const windows = useWindows() return useMemo(() => [...windows].map(([windowId, window]) => ({ windowId, window })), [windows]) }