The best way to control Claude Code, Codex and more from any computer or phone

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