import type { CodeChange, CodeMapState, CodeMapSummary } from "@refs/backend/contract"; import { Empty, LayerCard, LinkButton, Shimmer } from "@refs/design-system"; import { FileCodeIcon, TreeStructureIcon, WarningCircleIcon } from "@refs/design-system/icons"; import { useEffect, type ReactNode } from "react"; import { useRevalidator } from "react-router"; import type { CardNotes } from "./code-card"; import { CodeNavigator } from "./code-navigator"; import { useFillHeight } from "./fill"; import { useCodeGraph } from "./use-code-graph"; const RECHECK_MS = 2500; /** The map of a commit in each of its states: being built, loading into the page, ready, or not available. */ export function CodeMapPanel({ repo, summary, changes, notes, codeHref }: { repo: string; summary: CodeMapSummary; changes?: readonly CodeChange[]; notes?: CardNotes; codeHref: string }) { if (summary.kind === "indexing") return ; if (summary.kind === "unavailable") return ; return ; } function LoadedMap({ repo, commit, changes, notes, codeHref }: { repo: string; commit: string; changes?: readonly CodeChange[]; notes?: CardNotes; codeHref: string }) { const load = useCodeGraph(repo, commit); if (load.kind === "loading") return Loading the map of {commit.slice(0, 7)}} />; if (load.kind === "failed") return ; return ; } /** While a map is being built, the page checks again every few seconds and opens the map when it is ready. */ function MapIndexing({ commit }: { commit: string }) { const revalidator = useRevalidator(); useEffect(() => { const timer = setInterval(() => { if (revalidator.state === "idle") void revalidator.revalidate(); }, RECHECK_MS); return () => clearInterval(timer); }, [revalidator]); return Indexing the code at {commit.slice(0, 7)}} note="The map opens here when it is ready. This takes a few seconds for most repositories." />; } function MapPlaceholder({ title, note }: { title: ReactNode; note?: string }) { const fill = useFillHeight(); return ( {title}
{[0, 1, 2].map((column) => (
))}
{note &&

{note}

}
); } const UNAVAILABLE: Record["reason"], { title: string; description: string }> = { empty: { title: "No TypeScript or JavaScript to map", description: "The map reads .ts, .tsx, .js and .jsx files. This commit has none." }, too_large: { title: "This code is too large to map", description: "The map of this commit is larger than refs can store. Browse the code instead." }, failed: { title: "The map could not be built", description: "refs tries again on its own in a few minutes. Until then, browse the code." }, }; function MapUnavailable({ state, codeHref }: { state: Extract; codeHref: string }) { const text = UNAVAILABLE[state.reason]; return ( : } title={text.title} description={text.description} contents={ Browse the code } /> ); }