Skip to content
refs
66 lines · 4.3 KBRaw
1import { cn } from "@refs/design-system";
2import { CaretDownIcon, CaretUpIcon, CheckIcon } from "@refs/design-system/icons";
3import type { CodeModel, LineDiff } from "./model";
4
5function Counts({ diff }: { diff: LineDiff }) {
6 return (
7 <span className="flex shrink-0 gap-1.5 font-mono text-xs tabular-nums">
8 {diff.added > 0 && <span className="text-success">+{diff.added}</span>}
9 {diff.removed > 0 && <span className="text-danger">−{diff.removed}</span>}
10 </span>
11 );
12}
13
14/** Every file a pull request changed, with the declarations it changed in each. A row takes the reader to that change. */
15export function ChangesPanel({ model, current, visited, open, onOpenChange, onChange, onFile }: { model: CodeModel; current: string | null; visited: ReadonlySet<string>; open: boolean; onOpenChange: (open: boolean) => void; onChange: (id: string) => void; onFile: (path: string) => void }) {
16 const byFile = new Map<string, string[]>();
17 for (const id of model.changedSymbols) {
18 const file = model.symbol(id)!.file;
19 byFile.set(file, [...(byFile.get(file) ?? []), id]);
20 }
21 const files = model.changes();
22 const total = files.reduce((sum, file) => ({ added: sum.added + file.additions, removed: sum.removed + file.deletions }), { added: 0, removed: 0 });
23 return (
24 <aside data-map-chrome aria-label="Changes" className={cn("material-popup absolute top-16 left-3 z-10 flex w-[min(19rem,calc(100%-1.5rem))] flex-col overflow-hidden rounded-panel shadow-surface", open && "bottom-3")}>
25 <button type="button" className="flex h-10 shrink-0 cursor-pointer items-center gap-2 px-3 text-left text-compact font-medium hover:bg-hover" aria-expanded={open} onClick={() => onOpenChange(!open)}>
26 Changes
27 <span className="caption truncate text-faint">
28 {model.changedSymbols.length} {model.changedSymbols.length === 1 ? "block" : "blocks"} · {files.length} {files.length === 1 ? "file" : "files"}
29 </span>
30 <span className="ml-auto" />
31 <Counts diff={total} />
32 {open ? <CaretUpIcon size={14} className="text-faint" /> : <CaretDownIcon size={14} className="text-faint" />}
33 </button>
34 {open && (
35 <div className="min-h-0 flex-1 overflow-y-auto p-1 shadow-[inset_0_1px_0_var(--hairline)]">
36 {files.map((file) => {
37 const symbols = byFile.get(file.path) ?? [];
38 const slash = file.path.lastIndexOf("/");
39 const mapped = model.parent(`file:${file.path}`) !== null;
40 return (
41 <div key={file.path} className="mt-1 first:mt-0">
42 <button type="button" disabled={!mapped} className="flex h-7 w-full min-w-0 cursor-pointer items-center gap-2 rounded-control px-2 text-left font-mono text-xs hover:bg-hover disabled:cursor-default disabled:hover:bg-transparent" onClick={() => onFile(file.path)} title={mapped ? file.path : `${file.path} is not in the map`}>
43 <span className="min-w-0 truncate">
44 <span className="text-faint">{file.path.slice(0, slash + 1)}</span>
45 <span className={mapped ? "text-ink" : "text-muted"}>{file.path.slice(slash + 1)}</span>
46 </span>
47 <span className="ml-auto" />
48 {file.status === "deleted" && <span className="caption text-faint">Deleted</span>}
49 <Counts diff={{ added: file.additions, removed: file.deletions }} />
50 </button>
51 {symbols.map((id) => (
52 <button key={id} type="button" aria-current={id === current ? "true" : undefined} className={cn("flex h-7 w-full min-w-0 cursor-pointer items-center gap-2 rounded-control pr-2 pl-5 text-left font-mono text-xs text-muted hover:bg-hover hover:text-ink", id === current && "bg-control font-medium text-ink shadow-control hover:bg-control")} onClick={() => onChange(id)}>
53 <span className="min-w-0 truncate">{model.qualifiedName(id)}</span>
54 <span className="ml-auto" />
55 {visited.has(id) && id !== current && <CheckIcon size={12} className="shrink-0 text-faint" aria-label="Visited" />}
56 <Counts diff={model.diff(id)} />
57 </button>
58 ))}
59 </div>
60 );
61 })}
62 </div>
63 )}
64 </aside>
65 );
66}