SPB Git

Repository Public

CitationView.tsx 10.8 KB 101 lines tsx

1"use client";2/**3 * file: src/components/editor/nodes/CitationView.tsx4 * description: `citation` (inline atom bound to a research reference: refId, locator, cached "Author Year" label) and `bibliography`5 *   (block listing, in APA style, the references cited in the page — computed from the document, never stored). Both resolve their6 *   references through the page-wide resolver (src/lib/editor/citations.ts): all the ids cited in a page go out as one7 *   `GET /api/research/references?ids=` (SPEC §4.14) and are cached per id; the chip shows the reference's current author–year (the8 *   `label` attr is the offline fallback) and a deleted reference is shown as such. The library is only loaded by the picker.9 * author: Simon-Pierre Boucher — contact@spboucher.ai10 */11import * as React from "react";12import { NodeViewWrapper, useEditorState } from "@tiptap/react";13import { createReferenceResolver, referencesUrl, isRefId, type ResolvedRef, type RefState } from "@/lib/editor/citations";14import { useQuery } from "@tanstack/react-query";15import { BookMarked, Library, Search } from "lucide-react";16import type { CitationAttrs, BibliographyAttrs } from "@/lib/editor/next-node-defs";17import { formatCitation, shortLabel, type CiteRef } from "@/lib/research/cite";18import { fuzzyRank } from "@/lib/client/commands";19import { api } from "@/lib/client/api";20import { Popover, PopoverTrigger, PopoverContent } from "@/components/ui/primitives";21import { NodeFrame, NodeButton, NodeState, useDebounced, type LeafViewProps } from "./frame";2223export interface RefRow extends CiteRef { id: string }24/** "Gorton & Metrick (2019)" → "Gorton & Metrick 2019" — the cached label of SPEC §2 ("Author Year"). */25export const citationLabel = (r: CiteRef) => shortLabel(r).replace(/\s*\((.+)\)$/, " $1");26export const citationText = (a: Pick<CitationAttrs, "label" | "locator" | "refId">) => `(${a.label || (a.refId ? "reference" : "citation")}${a.locator ? `, ${a.locator}` : ""})`;27const useLibrary = (enabled: boolean) => useQuery({ queryKey: ["references-library"], enabled, staleTime: 60_000, queryFn: () => api<RefRow[]>("/api/research/references") });28/** The app's resolver: one batched `?ids=` request for all the citations of a page, cached per id (see src/lib/editor/citations.ts). */29export const referenceResolver = createReferenceResolver((ids) => api<ResolvedRef[]>(referencesUrl(ids)));30/** State of one cited reference: the reference, `null` when the server does not have it (deleted), `undefined` while unknown. */31export function useReference(id: string | null | undefined): RefState {32  const state = React.useSyncExternalStore(referenceResolver.subscribe, () => (id ? referenceResolver.peek(id) : null), () => undefined);33  React.useEffect(() => { if (id && state === undefined) void referenceResolver.load(id); }, [id, state]);34  return state;35}36/** The references of a list of ids, in the order asked, plus the ids the server does not have. */37export function useReferences(ids: string[]): { found: ResolvedRef[]; missing: string[]; loading: boolean; error: Error | null; retry: () => void } {38  const [, bump] = React.useReducer((n: number) => n + 1, 0); const key = ids.join(",");39  React.useEffect(() => referenceResolver.subscribe(bump), []);40  React.useEffect(() => { if (ids.length) void referenceResolver.loadMany(ids); }, [key]); // eslint-disable-line react-hooks/exhaustive-deps41  const states = ids.map((id) => [id, isRefId(id) ? referenceResolver.peek(id) : null] as const);42  const loading = states.some(([, s]) => s === undefined); const error = loading ? referenceResolver.error() : null;43  return { found: states.flatMap(([, s]) => (s ? [s] : [])), missing: states.filter(([, s]) => s === null).map(([id]) => id), loading: loading && !error, error, retry: () => { void referenceResolver.loadMany(ids).then(bump); } };44}4546/** The "/cite" command asks the next citation that mounts (the one it is about to insert) to open its reference picker. */47let openNext = false;48export const requestOpenCitation = () => { openNext = true; setTimeout(() => { openNext = false; }, 1500); };49export function CitationView({ attrs, update, editable, selected }: LeafViewProps<CitationAttrs>) {50  const [open, setOpen] = React.useState(false); const [q, setQ] = React.useState("");51  React.useEffect(() => { if (openNext && editable && !attrs.refId) { openNext = false; setOpen(true); } /* eslint-disable-next-line react-hooks/exhaustive-deps */ }, []);52  const lib = useLibrary(open && editable); const setLocator = useDebounced((v: string) => update({ locator: v }), 400);53  const hits = React.useMemo(() => fuzzyRank(q, lib.data ?? [], (r) => ({ title: `${citationLabel(r)} — ${r.title}`, keywords: `${r.citeKey} ${(r.authors ?? []).join(" ")} ${r.journal ?? ""}` }), undefined, 8), [q, lib.data]);54  const resolved = useReference(attrs.refId); const current = resolved ?? (lib.data ?? []).find((r) => r.id === attrs.refId);55  // what is shown: the reference as it is now; the cached label only while it is unknown (offline, first paint)56  const shown = resolved ? { ...attrs, label: citationLabel(resolved) } : attrs; const gone = Boolean(attrs.refId) && resolved === null;57  const state = !attrs.refId ? "empty" : gone ? "deleted" : resolved ? "resolved" : "cached";58  const chip = <span className="cite-inline" data-selected={selected ? "true" : undefined} data-missing={!attrs.refId || gone ? "true" : undefined} data-ref-state={state} title={gone ? "This reference is no longer in your library" : resolved?.citation ?? undefined} role={editable ? "button" : undefined} tabIndex={editable ? 0 : undefined}>{citationText(shown)}</span>;59  if (!editable) return <NodeViewWrapper as="span" className="nv-inline" data-node="citation">{attrs.refId && !gone ? <a className="cite-inline" data-ref-state={state} title={resolved?.citation ?? undefined} href={`/research?ref=${attrs.refId}`}>{citationText(shown)}</a> : chip}</NodeViewWrapper>;60  return (61    <NodeViewWrapper as="span" className="nv-inline" data-node="citation">62      <Popover open={open} onOpenChange={(o) => { setOpen(o); if (!o) { setLocator.flush(); if (resolved && citationLabel(resolved) !== attrs.label) update({ label: citationLabel(resolved) }); } }}>63        <PopoverTrigger asChild>{chip}</PopoverTrigger>64        <PopoverContent className="w-[min(22rem,calc(100vw-1.5rem))] p-2" onOpenAutoFocus={(e) => { if (attrs.refId) e.preventDefault(); }}>65          {attrs.refId && <div className="mb-2 border-b border-line pb-2"><div className="text-ui font-medium">{gone ? <span className="text-danger-text">This reference is no longer in your library — choose another one below.</span> : current ? formatCitation(current) : attrs.label || "Reference"}</div>66            <label className="mt-2 flex items-center gap-2 text-ui text-fg-muted">Locator<input className="nb-field flex-1" defaultValue={attrs.locator} placeholder="p. 12, chap. 3…" aria-label="Locator" onChange={(e) => setLocator(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { setLocator.flush(); setOpen(false); } }} /></label></div>}67          <label className="flex items-center gap-2 rounded-control border border-line bg-bg px-2"><Search className="size-4 text-fg-faint" aria-hidden /><input autoFocus={!attrs.refId} value={q} onChange={(e) => setQ(e.target.value)} placeholder={attrs.refId ? "Change reference…" : "Search your references…"} aria-label="Search references" className="h-8 min-w-0 flex-1 bg-transparent text-ui outline-none" style={{ outline: "none" }} /></label>68          <div className="mt-1 max-h-56 overflow-y-auto" role="listbox" aria-label="References">69            {lib.isLoading ? <div className="p-2 text-ui text-fg-muted">Loading…</div> : (lib.data ?? []).length === 0 ? <div className="p-2 text-ui text-fg-muted">No reference in your library yet. Add some in Research → References.</div>70              : hits.length === 0 ? <div className="p-2 text-ui text-fg-muted">No reference matches.</div>71              : hits.map(({ item: r }) => <button key={r.id} type="button" role="option" aria-selected={r.id === attrs.refId} className="block w-full rounded-control px-2 py-1.5 text-left hover:bg-bg-hover" onClick={() => { update({ refId: r.id, label: citationLabel(r) }); setQ(""); setOpen(false); }}><span className="block text-ui font-medium">{citationLabel(r)}</span><span className="block truncate text-micro text-fg-muted">{r.title}</span></button>)}72          </div>73        </PopoverContent>74      </Popover>75    </NodeViewWrapper>76  );77}7879/** Reference ids cited in the document, in order of first appearance. */80export function citedIds(doc: { descendants: (f: (n: { type: { name: string }; attrs: Record<string, unknown> }) => void) => void }): string[] {81  const ids: string[] = []; doc.descendants((n) => { if (n.type.name === "citation" && typeof n.attrs.refId === "string" && !ids.includes(n.attrs.refId)) ids.push(n.attrs.refId); }); return ids;82}83export function BibliographyView({ attrs, update, editable, selected, editor }: LeafViewProps<BibliographyAttrs>) {84  // re-computed only when the set of cited ids changes (the selector returns a string, compared by value)85  const idKey = useEditorState({ editor, selector: ({ editor: e }) => citedIds(e.state.doc).join(",") }) ?? "";86  const ids = React.useMemo(() => (idKey ? idKey.split(",") : []), [idKey]);87  const refs = useReferences(ids); const setTitle = useDebounced((v: string) => update({ title: v }), 400);88  const lines = React.useMemo(() => refs.found.map((r) => ({ id: r.id, text: r.citation || formatCitation(r) })).sort((a, b) => a.text.localeCompare(b.text)), [refs.found]); // eslint-disable-line react-hooks/exhaustive-deps89  return (90    <NodeFrame kind="bibliography" icon={<Library />} label="Bibliography" selected={selected} title={<span className="nb-meta">{attrs.style.toUpperCase()} · {ids.length} cited</span>}>91      <div className="bib">92        {editable ? <input className="bib-title" defaultValue={attrs.title} aria-label="Bibliography title" onChange={(e) => setTitle(e.target.value)} onBlur={() => setTitle.flush()} /> : <div className="bib-title">{attrs.title}</div>}93        {ids.length === 0 ? <NodeState icon={<BookMarked />} title="No citation in this page yet" hint="Insert a citation with “/cite”: the references you cite are listed here automatically." />94          : refs.loading && lines.length === 0 ? <div className="skeleton h-5 w-2/3" />95          : refs.error && lines.length === 0 ? <NodeState tone="danger" icon={<BookMarked />} title="The references could not be loaded" hint={refs.error.message}><NodeButton label="Retry" text onClick={refs.retry} /></NodeState>96          : <ol className="bib-list" data-testid="bibliography-list">{lines.map((l) => <li key={l.id}>{l.text}</li>)}{refs.missing.map((id) => <li key={id} className="text-fg-muted" data-missing="true">Reference not found ({id.slice(0, 8)}…) — it may have been deleted.</li>)}</ol>}97      </div>98    </NodeFrame>99  );100}101
Files main
Menu

Theme