import type { SpaceNode } from '@astrale-os/shell' /** * useCurrentSpace (API §12.2) — the selected space, projected from the shared * per-Shell current-space store (so a switch anywhere re-themes it). One-shot * today (§12 flag); OS-plane-unavailable reads idle. */ import { useEffect, useMemo, useSyncExternalStore } from 'react' import type { ReadState } from '../../graph/memory/read/envelope.js' import { useShell } from '../../session/shell.hook.js' import { isOsPlaneAvailable } from '../user/user.store.js' import { currentSpaceStore, idleSnapshot, noopSubscribe } from './current-space.store.js' export interface CurrentSpaceResult extends ReadState { readonly space: SpaceNode | null } export function useCurrentSpace(): CurrentSpaceResult { const shell = useShell() const available = isOsPlaneAvailable(shell) const store = currentSpaceStore(shell) useEffect(() => { if (available) store.ensure() }, [store, available]) const snap = useSyncExternalStore( available ? store.subscribe : noopSubscribe, available ? store.getSnapshot : idleSnapshot, available ? store.getSnapshot : idleSnapshot, ) return useMemo( () => ({ space: snap.space, pending: snap.pending, error: snap.error, live: false, seq: 0, refetch: store.refresh, }), [snap, store], ) }