Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GuideStop.tsx

292 lines11.4 KB

1import type { FileDiffContentsLoader, FileDiffMetadata } from "@pierre/diffs";
2import type {
3 PullRequestReviewGuideAttention,
4 PullRequestReviewGuideStop,
5} from "@t3tools/contracts";
6import {
7 ArrowRightIcon,
8 ChevronDownIcon,
9 ChevronRightIcon,
10 ChevronUpIcon,
11 EyeIcon,
12 FileIcon,
13 MessageCircleQuestionIcon,
14 MessageSquareIcon,
15} from "lucide-react";
16import type { ReactNode } from "react";
17​
18import { getDiffLineStat } from "~/lib/diffRendering";
19import { cn } from "~/lib/utils";
20​
21import { DiffWorkerPoolProvider } from "../../DiffWorkerPoolProvider";
22import { Alert, AlertDescription, AlertTitle } from "../../ui/alert";
23import { Button } from "../../ui/button";
24import { Kbd } from "../../ui/kbd";
25import { PullRequestDiffStat } from "../pullRequestPresentation";
26import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../../ui/collapsible";
27import { GuideExcerpt, type ExcerptCommenting } from "./GuideExcerpt";
28import { GuideFileDiff } from "./GuideFileDiff";
29import { GuideNotes } from "./GuideNotes";
30import { GuideProse } from "./GuideProse";
31import { letterStopNotes } from "@t3tools/client-runtime/review-guide-tour";
32​
33const ATTENTION_LABELS = {
34 tradeoff: "A tradeoff to weigh",
35 contract: "A contract to check",
36 product: "A product call",
37 risk: "A risk to judge",
38 accessibility: "An accessibility call",
39} as const satisfies Record<PullRequestReviewGuideAttention["kind"], string>;
40​
41export function GuideStop({
42 stop,
43 index,
44 stopCount,
45 railOpen,
46 onRailOpenChange,
47 stopList,
48 nextTitle,
49 files,
50 loadDiffFiles,
51 diffLoading,
52 commenting,
53 onStep,
54 onAsk,
55 onFinish,
56 onOpenFile,
57}: {
58 stop: PullRequestReviewGuideStop;
59 index: number;
60 stopCount: number;
61 railOpen: boolean;
62 onRailOpenChange: (open: boolean) => void;
63 stopList: ReactNode;
64 nextTitle: string | null;
65 files: ReadonlyMap<string, FileDiffMetadata>;
66 loadDiffFiles: FileDiffContentsLoader;
67 diffLoading: boolean;
68 commenting: ExcerptCommenting | null;
69 onStep: (delta: 1 | -1) => void;
70 onAsk: ((question: string) => void) | null;
71 onFinish: (() => void) | null;
72 onOpenFile: (path: string) => void;
73}) {
74 const { lettered, byExcerpt } = letterStopNotes(stop);
75 const excerptPaths = new Set(stop.excerpts.map((excerpt) => excerpt.path));
76 const otherFiles = stop.files.filter((file) => !excerptPaths.has(file.path));
77​
78 const hasNotes = lettered.length > 0;
79​
80 return (
81 <div
82 className={cn("grid gap-8", hasNotes && "@min-[90rem]/guide:grid-cols-[minmax(0,1fr)_20rem]")}
83 >
84 <article className="flex min-w-0 flex-col gap-5">
85 <Collapsible open={railOpen} onOpenChange={onRailOpenChange}>
86 <header className="flex items-center justify-between gap-2">
87 <p className="text-xs text-muted-foreground tabular-nums @max-[56rem]/guide:hidden">
88 Stop {index + 1} of {stopCount}
89 </p>
90 <CollapsibleTrigger
91 render={
92 <Button size="xs" variant="ghost-muted" className="@min-[56rem]/guide:hidden" />
93 }
94 >
95 <span className="tabular-nums">
96 Stop {index + 1} of {stopCount}
97 </span>
98 <ChevronDownIcon aria-hidden className={cn(railOpen && "rotate-180")} />
99 </CollapsibleTrigger>
100 <div className="flex items-center gap-1">
101 <Button
102 size="icon-xs"
103 variant="outline"
104 aria-label="Previous stop"
105 disabled={index === 0}
106 onClick={() => onStep(-1)}
107 >
108 <ChevronUpIcon aria-hidden />
109 </Button>
110 <Button
111 size="icon-xs"
112 variant="outline"
113 aria-label="Next stop"
114 disabled={index === stopCount - 1}
115 onClick={() => onStep(1)}
116 >
117 <ChevronDownIcon aria-hidden />
118 </Button>
119 <span
120 aria-hidden
121 className="ml-1 flex items-center gap-0.5 @max-[30rem]/guide:hidden"
122 >
123 <Kbd>J</Kbd>
124 <Kbd>K</Kbd>
125 </span>
126 </div>
127 </header>
128 <CollapsiblePanel className="@min-[56rem]/guide:hidden">
129 <div className="pt-3">{stopList}</div>
130 </CollapsiblePanel>
131 </Collapsible>
132​
133 <div className="flex max-w-[60rem] flex-col gap-2">
134 <h2 className="text-lg font-semibold text-foreground">{stop.title}</h2>
135 {stop.summary.trim() !== "" ? (
136 <div className="text-sm text-muted-foreground">
137 <GuideProse text={stop.summary} />
138 </div>
139 ) : null}
140 </div>
141​
142 {stop.attention !== null ? (
143 <Alert controlAlignment="first-line">
144 <EyeIcon />
145 <AlertTitle>{ATTENTION_LABELS[stop.attention.kind]}</AlertTitle>
146 <AlertDescription>
147 <div>
148 <GuideProse text={stop.attention.note} />
149 </div>
150 </AlertDescription>
151 </Alert>
152 ) : null}
153​
154 {stop.excerpts.length > 0 ? (
155 <div className="flex min-w-0 flex-col gap-4">
156 <DiffWorkerPoolProvider>
157 {stop.excerpts.map((excerpt, excerptIndex) => (
158 <GuideExcerpt
159 key={`${excerpt.path}:${excerpt.side}:${excerpt.start}-${excerpt.end}`}
160 excerpt={excerpt}
161 file={files.get(excerpt.path) ?? null}
162 loading={diffLoading}
163 notes={byExcerpt[excerptIndex] ?? []}
164 commenting={commenting}
165 loadDiffFiles={loadDiffFiles}
166 onOpenFile={onOpenFile}
167 />
168 ))}
169 </DiffWorkerPoolProvider>
170 </div>
171 ) : null}
172 <GuideNotes notes={lettered} className="@min-[90rem]/guide:hidden" />
173​
174 {otherFiles.length > 0 ? (
175 <section className="flex flex-col gap-1.5">
176 <h3 className="text-xs font-medium text-muted-foreground">
177 {stop.excerpts.length > 0 ? "Also in this stop" : "Files in this stop"}
178 </h3>
179 <DiffWorkerPoolProvider>
180 <ul className="flex flex-col overflow-hidden rounded-lg border border-border/60">
181 {otherFiles.map((file) => {
182 const diff = files.get(file.path);
183 const stat = diff === undefined ? null : getDiffLineStat([diff]);
184 return (
185 <li key={file.path} className="border-border/60 not-first:border-t">
186 <Collapsible>
187 <CollapsibleTrigger className="group flex w-full min-w-0 items-center gap-2 px-3 py-2 text-left text-xs outline-none hover:bg-accent/50 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset">
188 <ChevronRightIcon
189 aria-hidden
190 className="size-3.5 shrink-0 text-muted-foreground group-data-panel-open:rotate-90"
191 />
192 <FileIcon
193 aria-hidden
194 className="size-3.5 shrink-0 text-muted-foreground"
195 />
196 <span className="min-w-0 truncate font-mono text-foreground">
197 {file.path}
198 </span>
199 {file.note !== undefined && file.note.trim() !== "" ? (
200 <span className="ml-auto min-w-0 truncate text-muted-foreground @max-[30rem]/guide:hidden">
201 <GuideProse text={file.note} plain />
202 </span>
203 ) : null}
204 {stat !== null ? (
205 <PullRequestDiffStat
206 additions={stat.additions}
207 deletions={stat.deletions}
208 className="shrink-0 font-mono"
209 />
210 ) : null}
211 </CollapsibleTrigger>
212 <CollapsiblePanel animate={false}>
213 {diff !== undefined ? (
214 <GuideFileDiff file={diff} loadDiffFiles={loadDiffFiles} />
215 ) : (
216 <p className="px-3 py-2 text-xs text-muted-foreground">
217 {diffLoading
218 ? "Loading the diff..."
219 : "This file is not in the loaded diff."}
220 </p>
221 )}
222 </CollapsiblePanel>
223 </Collapsible>
224 </li>
225 );
226 })}
227 </ul>
228 </DiffWorkerPoolProvider>
229 </section>
230 ) : null}
231​
232 {stop.questions.length > 0 || onAsk !== null ? (
233 <section className="flex flex-col gap-1.5">
234 {stop.questions.length > 0 ? (
235 <h3 className="text-xs font-medium text-muted-foreground">Questions to consider</h3>
236 ) : null}
237 <div className="flex flex-col items-start gap-1.5">
238 {stop.questions.map((question) =>
239 onAsk === null ? (
240 <div key={question} className="text-sm text-foreground">
241 <GuideProse text={question} />
242 </div>
243 ) : (
244 <button
245 key={question}
246 type="button"
247 onClick={() => onAsk(question)}
248 className="flex min-w-0 items-start gap-2 rounded-lg border border-border/60 px-3 py-1.5 text-left text-sm text-foreground outline-none hover:bg-accent/50 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
249 >
250 <MessageCircleQuestionIcon
251 aria-hidden
252 className="size-3.5 h-lh shrink-0 text-muted-foreground"
253 />
254 <span className="sr-only">Ask an agent: </span>
255 <span>
256 <GuideProse text={question} plain />
257 </span>
258 </button>
259 ),
260 )}
261 {onAsk !== null ? (
262 <Button size="sm" variant="ghost-muted" onClick={() => onAsk("")}>
263 <MessageCircleQuestionIcon aria-hidden />
264 Ask about this stop
265 </Button>
266 ) : null}
267 </div>
268 </section>
269 ) : null}
270​
271 <footer className="flex flex-wrap items-center justify-end gap-3 border-t border-border/60 pt-4">
272 {nextTitle !== null ? (
273 <Button size="multiline" className="max-w-full" onClick={() => onStep(1)}>
274 <span className="min-w-0 break-words">Next: {nextTitle}</span>
275 <ArrowRightIcon aria-hidden />
276 </Button>
277 ) : onFinish !== null ? (
278 <Button onClick={onFinish}>
279 <MessageSquareIcon aria-hidden />
280 Finish review
281 </Button>
282 ) : null}
283 </footer>
284 </article>
285 {hasNotes ? (
286 <aside className="hidden @min-[90rem]/guide:block @min-[90rem]/guide:sticky @min-[90rem]/guide:top-4 @min-[90rem]/guide:self-start">
287 <GuideNotes notes={lettered} />
288 </aside>
289 ) : null}
290 </div>
291 );