import type { Node } from '@astrale-os/sdk/graph/node' import { Path } from '@astrale-os/sdk/graph/path' import { useCallback, useMemo, useSyncExternalStore } from 'react' import type { GraphResource } from '../../graph/resource.js' import { useDynamicObject } from '../../schema/object.hook.js' import { useShell } from '../../session/shell.hook.js' /** * Read the canonical target admitted by the parent handshake and subsequent * parent-owned hot swaps without remounting this View document. */ export function useTargetNode(): GraphResource { return useDynamicObject(useTargetPath()) } /** Reactive canonical target Path; null represents an explicit parent clear. */ export function useTargetPath(): Path | null { const shell = useShell() const subscribe = useCallback( (listener: () => void) => shell.onTargetChange(() => listener()), [shell], ) const snapshot = useCallback(() => shell.targetNodeId, [shell]) const target = useSyncExternalStore(subscribe, snapshot, snapshot) return useMemo(() => (target === undefined ? null : Path.parse(target)), [target]) }