"use client"; import { createContext, useContext, type ReactNode } from "react"; import type { CitationView } from "../../lib/report/views"; // A PAGE'S CITATIONS, ONCE. Every inline marker (InlineCite) on a report page // needs its citation's view — the label's href, the number, the preview card. // Handing each marker its own copy as a prop would serialize that view into the // page's RSC payload once per marker; a page with hundreds of markers paid for // it hundreds of times. Instead the page wraps its content in one // CitationsProvider, whose map is serialized once, and each marker carries only // its id and reads the view here at render time (on the server for the HTML, // in the browser for the preview). const CitationsContext = createContext> | null>(null); export function CitationsProvider({ citations, children, }: { citations: Readonly>; children: ReactNode; }) { return {children}; } // The view of citation `id` from the nearest provider, or undefined. export function useCitation(id: string | undefined): CitationView | undefined { const map = useContext(CitationsContext); return id !== undefined && map && Object.hasOwn(map, id) ? map[id] : undefined; }