import type { Node } from '@astrale-os/sdk/graph/node' import type { NodePath, PathLike } from '@astrale-os/sdk/graph/path' import type { ResolvedView } from '@astrale-os/shell' import { Path } from '@astrale-os/sdk/graph/path' /** * useViewsFor (API §10) — "what Views can render this node", as mountable * `ViewDescriptor`s for the open-with menu. Backed one-shot (`live:false`) by the * kernel `View.resolve` syscall, which derives the node's class and returns the * caller-visible Views (USE-visibility enforced kernel-side), each with mount url. */ import { useMemo } from 'react' import type { ReadState } from '../../graph/memory/read/envelope.js' import { useAsyncResource } from '../../graph/async.js' import { useShell } from '../../session/shell.hook.js' export interface ViewsForResult extends ReadState { readonly views: readonly ResolvedView[] } const NO_VIEWS: readonly ResolvedView[] = Object.freeze([]) export function useViewsFor(node: Node | PathLike | null | undefined): ViewsForResult { const shell = useShell() const target = resolvePath(node) const res = useAsyncResource( target === null ? null : () => shell.views.resolve(target), NO_VIEWS, [shell, target], ) return useMemo( () => ({ views: res.data, pending: res.pending, error: res.error, live: false, seq: 0, refetch: res.refetch, }), [res.data, res.pending, res.error, res.refetch], ) } /** One canonical target, or `null` for an idle subject. */ function resolvePath(node: Node | PathLike | null | undefined): NodePath | null { if (node === null || node === undefined) return null if ('id' in node) return Path.id(node.id).raw return Path.from(node).raw }