/** * useNavScope (API §8.2) — the nearest scope, for chrome/persistence: the live * location, a `snapshot()` handle, and the imperative navigate. Throws outside a * scope. */ import { useCallback, useMemo } from 'react' import type { NavSnapshot } from '../history.js' import type { NavLocation } from '../location.js' import type { Navigate } from '../scope.context.js' import { buildLocation } from '../location.js' import { useGraphPoint, useNavCtx } from '../scope.context.js' import { useNavigateHandle } from './navigate.hook.js' /** The nearest scope, for chrome/persistence (API §8.2). */ export interface NavScopeHandle { readonly location: NavLocation snapshot(): NavSnapshot navigate: Navigate } /** The nearest scope, for chrome/persistence (API §8.2). Throws outside a scope. */ export function useNavScope(): NavScopeHandle { const ctx = useNavCtx() const navigate = useNavigateHandle(ctx) const entry = ctx.history.current const result = useGraphPoint(entry?.node ?? null) const location = useMemo(() => buildLocation(entry, result), [entry, result]) const snapshot = useCallback(() => ctx.history.snapshot(), [ctx.history]) return { location, snapshot, navigate } }