import type { PathLike } from '@astrale-os/sdk/graph/path' import type { NodeTarget, SpaceNode } from '@astrale-os/shell' import { Path } from '@astrale-os/sdk/graph/path' /** * useSpace(ref) (API §12.2) — a single space by ref; `null`/`undefined` (or an * unavailable OS plane) is idle (§1.3). One-shot today (§12 flag). */ import { useMemo } from 'react' import type { CurrentSpaceResult } from './current-space.hook.js' import { useAsyncResource } from '../../graph/async.js' import { isIdleInput } from '../../graph/memory/read/idle.js' import { useShell } from '../../session/shell.hook.js' import { isOsPlaneAvailable } from '../user/user.store.js' export type SpaceResult = CurrentSpaceResult export function useSpace(ref: NodeTarget | null | undefined): SpaceResult { const shell = useShell() const idle = isIdleInput(ref) || !isOsPlaneAvailable(shell) // The inline guard (not the stored `idle`) narrows `ref` to non-null below. const key = idle ? null : targetKey(ref as NodeTarget) const res = useAsyncResource( idle ? null : () => shell.spaces.get(ref as NodeTarget), null, [shell, key], ) return useMemo( () => ({ space: idle ? null : res.data, pending: idle ? false : res.pending, error: idle ? undefined : res.error, live: false, seq: 0, refetch: res.refetch, }), [idle, res], ) } function targetKey(input: NodeTarget): string { return typeof input === 'string' ? `text:${input}` : `path:${Path.from(input as PathLike).raw}` }