import type { AnchorHTMLAttributes, FocusEvent, KeyboardEvent, MouseEvent, PointerEvent, ReactElement, ReactNode, } from 'react' import { Query } from '@astrale-os/sdk/query' /** * Link (API §8.2) — navigates the nearest scope. Renders `` when the * scope's adapter exposes a URL form (cmd/ctrl/middle-click then falls through * to the browser), else an href-less `` with identical behavior. * Default prefetch `'intent'` warms the target shape on hover/pointer-down/focus. */ import { useCallback, useEffect } from 'react' import type { NavigationDefinition, NavigationTarget } from './codec.js' import type { NavTarget } from './snapshot.js' import { useNavCtx } from './scope.context.js' import { serializeTarget } from './snapshot.js' export interface LinkProps extends AnchorHTMLAttributes { to: NavTarget replace?: boolean /** Warm the target shape ahead of navigation. Default `'intent'`. */ prefetch?: 'intent' | 'render' | false children: ReactNode } export function Link(props: LinkProps): ReactElement { const { to, replace, prefetch = 'intent', children, onClick, onPointerEnter, onPointerDown, onFocus, onKeyDown, ...rest } = props const ctx = useNavCtx() const target = serializeTarget(to) const href = ctx.adapter.href?.(target) const nodeRef = target.node const preload = useCallback(() => { const query = Query.from({ nodes: [nodeRef] }).select({ kind: 'graph' }) void ctx.store.preload(query, { page: { size: 1 } }).catch(() => {}) }, [ctx.store, nodeRef]) useEffect(() => { if (prefetch === 'render') preload() }, [prefetch, preload]) const intent = useCallback(() => { if (prefetch === 'intent') preload() }, [prefetch, preload]) const go = useCallback(() => ctx.navigate(to, { replace }), [ctx, to, replace]) const handleClick = (e: MouseEvent): void => { onClick?.(e) if (e.defaultPrevented) return const modified = e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0 if (href !== undefined && modified) return // let the browser open it (new tab / window) e.preventDefault() go() } const handleKeyDown = (e: KeyboardEvent): void => { onKeyDown?.(e) if (e.defaultPrevented || href !== undefined) return // a real anchor handles Enter itself if (e.key === 'Enter' || e.key === ' ') { e.preventDefault() go() } } const handlePointerEnter = (e: PointerEvent): void => { onPointerEnter?.(e) intent() } const handlePointerDown = (e: PointerEvent): void => { onPointerDown?.(e) intent() } const handleFocus = (e: FocusEvent): void => { onFocus?.(e) intent() } const linkRole = href !== undefined ? { href } : { role: 'link', tabIndex: rest.tabIndex ?? 0 } return ( {children} ) } export type ViewLinkProps = Omit & { readonly navigation: Definition readonly to: NavigationTarget } /** Codec-admitted link for one declared View location. */ export function ViewLink( props: ViewLinkProps, ): ReactElement { const { navigation, to, ...link } = props return } export type NodeLinkProps = Omit & { readonly to: Extract } /** Canonical node-only link; use ViewLink when a View/params contract exists. */ export function NodeLink(props: NodeLinkProps): ReactElement { return }