129 lines · 5.7 KBRaw
| 1 | import { Empty, Shimmer, Tabs } from "@refs/design-system"; |
| 2 | import { LayerCard } from "@refs/design-system"; |
| 3 | import { FileCodeIcon, FileDashedIcon } from "@refs/design-system/icons"; |
| 4 | import type { ChangeFile, ChangeSet, Choice, OnlineAgent, ReviewThread } from "@refs/backend/contract"; |
| 5 | import { Suspense, lazy, useEffect, useMemo, useRef, useState } from "react"; |
| 6 | |
| 7 | export interface ReviewContext { |
| 8 | readonly agents: readonly OnlineAgent[]; |
| 9 | readonly people: readonly Choice[]; |
| 10 | /** Threads to show beside their lines; outdated ones belong in the conversation only. */ |
| 11 | readonly threads?: readonly ReviewThread[]; |
| 12 | readonly renderThread?: (thread: ReviewThread) => React.ReactNode; |
| 13 | } |
| 14 | |
| 15 | export type DiffStyle = "split" | "unified"; |
| 16 | |
| 17 | const STYLE_KEY = "refs.diffStyle"; |
| 18 | const pierre = import.meta.env.SSR ? null : import("./pierre-file"); |
| 19 | const PierreFile = pierre && lazy(() => pierre); |
| 20 | const PierrePool = pierre && lazy(() => pierre.then((module) => ({ default: module.DiffPool }))); |
| 21 | |
| 22 | /** A file's header (path, line counts) over its content: the chrome around a diff. */ |
| 23 | export function FileFrame({ file, children, flush = false }: { file: ChangeFile; children: React.ReactNode; flush?: boolean }) { |
| 24 | return ( |
| 25 | <LayerCard> |
| 26 | <LayerCard.Secondary className="flex flex-wrap items-center justify-between gap-x-3 gap-y-1 py-2 font-normal"> |
| 27 | <span className="flex min-w-0 items-center gap-2"> |
| 28 | <FileCodeIcon size={14} className="shrink-0" /> |
| 29 | <span className="truncate font-mono text-xs font-medium text-ink">{file.path}</span> |
| 30 | </span> |
| 31 | <span className="font-mono text-xs tabular-nums"> |
| 32 | <span className="text-success">+{file.additions}</span> <span className="text-danger">−{file.deletions}</span> |
| 33 | </span> |
| 34 | </LayerCard.Secondary> |
| 35 | {flush ? children : <LayerCard.Primary className="px-4 py-4">{children}</LayerCard.Primary>} |
| 36 | </LayerCard> |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | function Unavailable({ file }: { file: ChangeFile }) { |
| 41 | const title = file.kind === "too_large" ? "File too large to display" : file.kind === "missing" ? "File content unavailable" : "Binary file not shown"; |
| 42 | return ( |
| 43 | <FileFrame file={file}> |
| 44 | <Empty size="sm" icon={<FileDashedIcon size={32} />} title={title} /> |
| 45 | </FileFrame> |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | function Loading({ file }: { file: ChangeFile }) { |
| 50 | return ( |
| 51 | <FileFrame file={file}> |
| 52 | <div role="status" aria-label="Loading diff" className="flex flex-col gap-2.5"> |
| 53 | {["w-[46%]", "w-[62%]", "w-[38%]", "w-[54%]"].map((width) => ( |
| 54 | <Shimmer key={width} className={`h-2.5 ${width}`} /> |
| 55 | ))} |
| 56 | </div> |
| 57 | </FileFrame> |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** Renders its children once the placeholder comes within about two screens of the viewport, and keeps them after. */ |
| 62 | function NearViewport({ placeholder, children }: { placeholder: React.ReactNode; children: React.ReactNode }) { |
| 63 | const marker = useRef<HTMLDivElement>(null); |
| 64 | const [near, setNear] = useState(false); |
| 65 | useEffect(() => { |
| 66 | const node = marker.current; |
| 67 | if (!node || near) return; |
| 68 | const observer = new IntersectionObserver((entries) => entries.some((entry) => entry.isIntersecting) && setNear(true), { rootMargin: "1500px 0px" }); |
| 69 | observer.observe(node); |
| 70 | return () => observer.disconnect(); |
| 71 | }, [near]); |
| 72 | return near ? children : <div ref={marker}>{placeholder}</div>; |
| 73 | } |
| 74 | |
| 75 | /** Renders each file with @pierre/diffs in the browser. With `review`, lines can be selected and commented on, mentioning teammates and agents. */ |
| 76 | export function DiffView({ changes, review = null }: { changes: ChangeSet; review?: ReviewContext | null }) { |
| 77 | const [style, setStyle] = useState<DiffStyle>("split"); |
| 78 | const [mounted, setMounted] = useState(false); |
| 79 | useEffect(() => { |
| 80 | setMounted(true); |
| 81 | try { |
| 82 | if (localStorage.getItem(STYLE_KEY) === "unified") setStyle("unified"); |
| 83 | } catch {} |
| 84 | }, []); |
| 85 | const choose = (value: DiffStyle) => { |
| 86 | setStyle(value); |
| 87 | try { |
| 88 | localStorage.setItem(STYLE_KEY, value); |
| 89 | } catch {} |
| 90 | }; |
| 91 | |
| 92 | const paths = useMemo(() => changes.files.filter((file) => file.kind === "text").map((file) => file.path), [changes.files]); |
| 93 | const placeholders = changes.files.map((file) => (file.kind === "text" ? <Loading key={file.path} file={file} /> : <Unavailable key={file.path} file={file} />)); |
| 94 | const files = changes.files.map((file) => |
| 95 | file.kind !== "text" ? ( |
| 96 | <Unavailable key={file.path} file={file} /> |
| 97 | ) : ( |
| 98 | <NearViewport key={file.path} placeholder={<Loading file={file} />}> |
| 99 | <Suspense fallback={<Loading file={file} />}>{PierreFile && <PierreFile file={file} revision={changes.head} diffStyle={style} review={review} />}</Suspense> |
| 100 | </NearViewport> |
| 101 | ), |
| 102 | ); |
| 103 | if (!changes.files.length) return <Empty icon={<FileCodeIcon size={40} />} title="No file changes" description="These revisions have identical trees." />; |
| 104 | return ( |
| 105 | <div className="flex flex-col gap-4"> |
| 106 | <div className="flex items-center justify-between gap-3"> |
| 107 | <span className="text-compact text-muted tabular-nums"> |
| 108 | {changes.files.length} {changes.files.length === 1 ? "file" : "files"} changed · <span className="font-mono text-xs text-success">+{changes.additions}</span> <span className="font-mono text-xs text-danger">−{changes.deletions}</span> |
| 109 | </span> |
| 110 | <Tabs |
| 111 | variant="segmented" |
| 112 | value={style} |
| 113 | onValueChange={(value) => choose(value as DiffStyle)} |
| 114 | tabs={[ |
| 115 | { value: "split", label: "Split" }, |
| 116 | { value: "unified", label: "Unified" }, |
| 117 | ]} |
| 118 | /> |
| 119 | </div> |
| 120 | {mounted && PierrePool ? ( |
| 121 | <Suspense fallback={placeholders}> |
| 122 | <PierrePool paths={paths}>{files}</PierrePool> |
| 123 | </Suspense> |
| 124 | ) : ( |
| 125 | placeholders |
| 126 | )} |
| 127 | </div> |
| 128 | ); |
| 129 | } |