83 lines · 4.3 KBRaw
| 1 | import type { CodeChange, CodeMapState, CodeMapSummary } from "@refs/backend/contract"; |
| 2 | import { Empty, LayerCard, LinkButton, Shimmer } from "@refs/design-system"; |
| 3 | import { FileCodeIcon, TreeStructureIcon, WarningCircleIcon } from "@refs/design-system/icons"; |
| 4 | import { useEffect, type ReactNode } from "react"; |
| 5 | import { useRevalidator } from "react-router"; |
| 6 | import type { CardNotes } from "./code-card"; |
| 7 | import { CodeNavigator } from "./code-navigator"; |
| 8 | import { useFillHeight } from "./fill"; |
| 9 | import { useCodeGraph } from "./use-code-graph"; |
| 10 | |
| 11 | const RECHECK_MS = 2500; |
| 12 | |
| 13 | /** The map of a commit in each of its states: being built, loading into the page, ready, or not available. */ |
| 14 | export function CodeMapPanel({ repo, summary, changes, notes, codeHref }: { repo: string; summary: CodeMapSummary; changes?: readonly CodeChange[]; notes?: CardNotes; codeHref: string }) { |
| 15 | if (summary.kind === "indexing") return <MapIndexing commit={summary.commit} />; |
| 16 | if (summary.kind === "unavailable") return <MapUnavailable state={summary} codeHref={codeHref} />; |
| 17 | return <LoadedMap repo={repo} commit={summary.commit} changes={changes} notes={notes} codeHref={codeHref} />; |
| 18 | } |
| 19 | |
| 20 | function LoadedMap({ repo, commit, changes, notes, codeHref }: { repo: string; commit: string; changes?: readonly CodeChange[]; notes?: CardNotes; codeHref: string }) { |
| 21 | const load = useCodeGraph(repo, commit); |
| 22 | if (load.kind === "loading") return <MapPlaceholder title={<>Loading the map of <code className="font-mono text-xs text-muted">{commit.slice(0, 7)}</code></>} />; |
| 23 | if (load.kind === "failed") return <MapUnavailable state={{ kind: "unavailable", commit, reason: "failed" }} codeHref={codeHref} />; |
| 24 | return <CodeNavigator key={commit} graph={load.graph} repo={repo} changes={changes} notes={notes} />; |
| 25 | } |
| 26 | |
| 27 | /** While a map is being built, the page checks again every few seconds and opens the map when it is ready. */ |
| 28 | function MapIndexing({ commit }: { commit: string }) { |
| 29 | const revalidator = useRevalidator(); |
| 30 | useEffect(() => { |
| 31 | const timer = setInterval(() => { |
| 32 | if (revalidator.state === "idle") void revalidator.revalidate(); |
| 33 | }, RECHECK_MS); |
| 34 | return () => clearInterval(timer); |
| 35 | }, [revalidator]); |
| 36 | return <MapPlaceholder title={<>Indexing the code at <code className="font-mono text-xs text-muted">{commit.slice(0, 7)}</code></>} note="The map opens here when it is ready. This takes a few seconds for most repositories." />; |
| 37 | } |
| 38 | |
| 39 | function MapPlaceholder({ title, note }: { title: ReactNode; note?: string }) { |
| 40 | const fill = useFillHeight<HTMLDivElement>(); |
| 41 | return ( |
| 42 | <LayerCard ref={fill} aria-busy="true"> |
| 43 | <LayerCard.Secondary> |
| 44 | <TreeStructureIcon /> |
| 45 | {title} |
| 46 | </LayerCard.Secondary> |
| 47 | <div className="grid flex-1 grid-cols-3 gap-6 bg-rail p-8 max-md:grid-cols-1"> |
| 48 | {[0, 1, 2].map((column) => ( |
| 49 | <div key={column} className="flex flex-col gap-3 max-md:not-first:hidden"> |
| 50 | <Shimmer className="h-4 w-1/3" /> |
| 51 | <Shimmer className="h-28 w-full rounded-panel" /> |
| 52 | <Shimmer className="h-20 w-5/6 rounded-panel" /> |
| 53 | </div> |
| 54 | ))} |
| 55 | </div> |
| 56 | {note && <p className="m-0 px-4 py-3 text-compact text-muted shadow-[inset_0_1px_0_var(--hairline)]">{note}</p>} |
| 57 | </LayerCard> |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | const UNAVAILABLE: Record<Extract<CodeMapState, { kind: "unavailable" }>["reason"], { title: string; description: string }> = { |
| 62 | empty: { title: "No TypeScript or JavaScript to map", description: "The map reads .ts, .tsx, .js and .jsx files. This commit has none." }, |
| 63 | 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." }, |
| 64 | failed: { title: "The map could not be built", description: "refs tries again on its own in a few minutes. Until then, browse the code." }, |
| 65 | }; |
| 66 | |
| 67 | function MapUnavailable({ state, codeHref }: { state: Extract<CodeMapState, { kind: "unavailable" }>; codeHref: string }) { |
| 68 | const text = UNAVAILABLE[state.reason]; |
| 69 | return ( |
| 70 | <LayerCard> |
| 71 | <Empty |
| 72 | icon={state.reason === "failed" ? <WarningCircleIcon /> : <FileCodeIcon />} |
| 73 | title={text.title} |
| 74 | description={text.description} |
| 75 | contents={ |
| 76 | <LinkButton href={codeHref} variant="secondary"> |
| 77 | Browse the code |
| 78 | </LinkButton> |
| 79 | } |
| 80 | /> |
| 81 | </LayerCard> |
| 82 | ); |
| 83 | } |