Skip to content
refs
History
160 lines · 8.0 KBRaw
1import { Banner, Button, Empty, InputArea } from "@refs/design-system";
2import { LayerCard, Ago } from "@refs/design-system";
3import { ArrowsClockwiseIcon, PathIcon } from "@refs/design-system/icons";
4import { MAX_BODY_LENGTH, type CommitSummary } from "@refs/backend/contract";
5
6import { parseDecisions, type DecisionRecord } from "@refs/protocol";
7import { useEffect, useRef, useState } from "react";
8import { useFetcher } from "react-router";
9import { DecisionView } from "./decision-view";
10
11interface Entry {
12 readonly key: string;
13 readonly record: DecisionRecord;
14 readonly commit: CommitSummary;
15}
16
17export function decisionsOf(commits: readonly CommitSummary[]): Entry[] {
18 return commits.flatMap((commit) => parseDecisions(commit.message ?? "", commit.hash).decisions.map((record, index) => ({ key: `${commit.hash}-${index}`, record, commit })));
19}
20
21interface Shared {
22 readonly refName: string;
23 readonly repo: string;
24 readonly now: number;
25 readonly canRevisit: boolean;
26}
27
28function Revisit({ entry, refName, draft, onDraft }: { entry: Entry; refName: string; draft: string; onDraft: (draft: string | null) => void }) {
29 const fetcher = useFetcher<{ error: string | null }>();
30 const [body, setBody] = useState(draft);
31 const area = useRef<HTMLDivElement>(null);
32 useEffect(() => {
33 setBody(draft);
34 area.current?.querySelector("textarea")?.focus();
35 }, [draft]);
36
37 return (
38 <fetcher.Form method="post" className="reveal flex flex-col gap-3 rounded-panel bg-panel p-4 shadow-[0_0_0_1px_var(--accent-hover),0_0_0_4px_var(--accent-tint)]">
39 <input type="hidden" name="intent" value="revisit" />
40 <input type="hidden" name="ref" value={refName} />
41 <input type="hidden" name="commit" value={entry.commit.hash} />
42 <input type="hidden" name="decision" value={entry.record.decisionId} />
43 <div ref={area}>
44 <InputArea
45 label="What should change?"
46 name="body"
47 value={body}
48 onValueChange={setBody}
49 maxLength={MAX_BODY_LENGTH}
50 rows={3}
51 description={`This opens a task to rework every later commit on ${refName} that builds on this decision.`}
52 />
53 </div>
54 {fetcher.data?.error && <Banner variant="error" description={fetcher.data.error} />}
55 <div className="flex flex-wrap justify-end gap-2">
56 <Button type="button" variant="secondary" onClick={() => onDraft(null)}>
57 Cancel
58 </Button>
59 <Button type="submit" variant="primary" icon={<ArrowsClockwiseIcon size={16} />} disabled={!body.trim()} loading={fetcher.state !== "idle"}>
60 Open task
61 </Button>
62 </div>
63 </fetcher.Form>
64 );
65}
66
67function Detail({ entry, supersededBy, onSelect, shared }: { entry: Entry; supersededBy: Entry | undefined; onSelect: (key: string) => void; shared: Shared }) {
68 const { record, commit } = entry;
69 const [draft, setDraft] = useState<string | null>(null);
70 const live = supersededBy === undefined;
71 const editable = live && shared.canRevisit;
72
73 return (
74 <LayerCard>
75 <LayerCard.Secondary className="font-normal">
76 <a href={`/${shared.repo}/commit/${commit.hash}`} className="shrink-0 rounded-chip bg-sunken px-1.5 py-0.5 shadow-[inset_0_0_0_1px_var(--hairline)] font-mono text-xs text-ink hover:text-accent-text">
77 {commit.hash.slice(0, 7)}
78 </a>
79 <span className="min-w-0 truncate text-compact">
80 {commit.title} · {commit.author} · <Ago at={commit.committedAt} now={shared.now} />
81 </span>
82 </LayerCard.Secondary>
83 <LayerCard.Primary className="flex flex-col gap-5 p-4">
84 {supersededBy && (
85 <p className="m-0 flex flex-wrap items-center gap-x-2 text-compact text-muted">
86 Reconsidered later.
87 <button type="button" onClick={() => onSelect(supersededBy.key)} className="cursor-pointer rounded-control text-accent-text transition-colors duration-150 hover:text-accent-hover">
88 See the decision that replaced it
89 </button>
90 </p>
91 )}
92 <DecisionView record={record} muted={!live} picks={editable ? { challenge: (assumption) => setDraft(`This assumption doesn't hold: "${assumption}". `), takePath: (name) => setDraft(`Go with ${name} instead. `) } : undefined} />
93 {editable &&
94 (draft === null ? (
95 <div className="pt-4 shadow-[inset_0_1px_0_var(--hairline)]">
96 <Button type="button" variant="secondary" icon={<ArrowsClockwiseIcon size={16} />} onClick={() => setDraft("")}>
97 Revisit this decision
98 </Button>
99 </div>
100 ) : (
101 <Revisit entry={entry} refName={shared.refName} draft={draft} onDraft={setDraft} />
102 ))}
103 </LayerCard.Primary>
104 </LayerCard>
105 );
106}
107
108export function HistoryReasoning({ commits, ...shared }: Shared & { commits: readonly CommitSummary[] }) {
109 const entries = decisionsOf(commits);
110 const supersededBy = new Map<string, Entry>();
111 for (const entry of entries) for (const id of entry.record.supersedes) supersededBy.set(id, entry);
112 const replaced = (entry: Entry) => supersededBy.get(entry.record.decisionId);
113 const [selected, setSelected] = useState<string | null>(null);
114 if (entries.length === 0) {
115 return <Empty icon={<PathIcon size={40} />} title="No decisions recorded" description={`No commit on this page of ${shared.refName} carries a decision record.`} />;
116 }
117
118 const current = entries.find((entry) => entry.key === selected) ?? entries.find((entry) => !replaced(entry)) ?? entries[0];
119 const reconsidered = entries.filter(replaced).length;
120 const assumptions = entries.filter((entry) => !replaced(entry)).reduce((total, entry) => total + entry.record.assumptions.length, 0);
121 const detail = (entry: Entry) => <Detail key={entry.key} entry={entry} supersededBy={replaced(entry)} onSelect={setSelected} shared={shared} />;
122
123 return (
124 <div className="flex flex-col gap-3">
125 <p className="m-0 text-compact text-muted tabular-nums">
126 {entries.length} {entries.length === 1 ? "decision" : "decisions"}
127 {reconsidered > 0 && ` · ${reconsidered} reconsidered`}
128 {assumptions > 0 && ` · ${assumptions} ${assumptions === 1 ? "assumption" : "assumptions"} to check`}
129 </p>
130 <div className="grid grid-cols-[minmax(0,1fr)] items-start gap-4 lg:grid-cols-[minmax(0,22rem)_minmax(0,1fr)]">
131 <ul aria-label="Decisions" className="m-0 flex list-none flex-col gap-0.5 p-0 lg:sticky lg:top-4 lg:max-h-[calc(100vh-2rem)] lg:overflow-y-auto">
132 {entries.map((entry) => {
133 const active = entry.key === current.key;
134 const live = !replaced(entry);
135 return (
136 <li key={entry.key} className="flex min-w-0 flex-col gap-3">
137 <button
138 type="button"
139 aria-current={active || undefined}
140 onClick={() => setSelected(entry.key)}
141 className={`flex w-full cursor-pointer items-start gap-3 rounded-control px-3 py-2.5 text-left transition-colors duration-150 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-accent-hover ${active ? "bg-hover" : "hover:bg-hover/60"}`}
142 >
143 <span aria-hidden="true" className={`mt-1.5 size-2 shrink-0 rounded-full ${live ? "bg-accent-hover" : "shadow-[inset_0_0_0_1px_var(--line)]"}`} />
144 <span className="flex min-w-0 flex-col gap-1">
145 <span className={`line-clamp-2 text-sm leading-snug ${active ? "font-medium text-ink" : live ? "text-ink" : "text-muted line-through decoration-1"}`}>{entry.record.decision}</span>
146 <span className="text-xs text-muted">
147 <span className="font-mono">{entry.commit.hash.slice(0, 7)}</span> · <Ago at={entry.commit.committedAt} now={shared.now} />
148 </span>
149 </span>
150 </button>
151 {active && <div className="mb-3 lg:hidden">{detail(entry)}</div>}
152 </li>
153 );
154 })}
155 </ul>
156 <div className="max-lg:hidden">{detail(current)}</div>
157 </div>
158 </div>
159 );
160}