Open History to see the last commit
1import type { EnvironmentId, PullRequestRef, PullRequestReviewGuideStop } from "@t3tools/contracts";
2import { msg, T, useGT, useMessages } from "gt-react";
3import { ScrollTextIcon, TriangleAlertIcon } from "lucide-react";
4import { useEffect, useEffectEvent } from "react";
5
6import type { ReviewCommentContext } from "~/reviewCommentContext";
7
8import { Alert, AlertAction, AlertDescription } from "../ui/alert";
9import { Button } from "../ui/button";
10import { Spinner } from "../ui/spinner";
11import type { ExcerptCommenting } from "./guide/GuideExcerpt";
12import { GuideProseContext } from "./guide/GuideProse";
13import { GuideTour } from "./guide/GuideTour";
14import { useReviewGuideWriter } from "./guide/useReviewGuideWriter";
15import {
16 nextPendingReviewCommentId,
17 pullRequestReviewKey,
18 usePendingReviewComments,
19 usePullRequestReviewStore,
20} from "./pullRequestReviewStore";
21import type { PullRequestCompletionScope } from "./PullRequestMarkdownTextarea";
22
23export function PullRequestGuideTab({
24 environmentId,
25 reference,
26 title,
27 workspaceRoot,
28 active,
29 canComment,
30 rangeComments,
31 completionScope,
32 onAskAboutLines,
33 onAskAboutStop,
34 onFinishReview,
35 onOpenFile,
36 onTourShown,
37}: {
38 environmentId: EnvironmentId;
39 reference: PullRequestRef;
40 title: string | null;
41 workspaceRoot: string;
42 active: boolean;
43 canComment: boolean;
44 rangeComments: boolean;
45 completionScope: PullRequestCompletionScope | null;
46 onAskAboutLines: ((comment: ReviewCommentContext, request: string) => void) | null;
47 onAskAboutStop:
48 | ((stop: PullRequestReviewGuideStop, index: number, question: string) => void)
49 | null;
50 onFinishReview: (() => void) | null;
51 onOpenFile: (path: string) => void;
52 onTourShown?: (() => void) | undefined;
53}) {
54 const gt = useGT();
55 const resolve = useMessages();
56 const { query, data, running, canWrite, write } = useReviewGuideWriter({
57 environmentId,
58 reference,
59 title,
60 });
61 const reviewKey = pullRequestReviewKey(reference);
62 const pending = usePendingReviewComments(reference);
63 const addComment = usePullRequestReviewStore((store) => store.addComment);
64 const removeComment = usePullRequestReviewStore((store) => store.removeComment);
65 const commenting: ExcerptCommenting | null =
66 !canComment && onAskAboutLines === null
67 ? null
68 : {
69 canComment,
70 rangeComments,
71 completionScope,
72 pending,
73 onComment: ({ path, oldPath, position, startPosition, body }) =>
74 addComment(reviewKey, {
75 id: nextPendingReviewCommentId(),
76 path,
77 ...(oldPath === null ? {} : { oldPath }),
78 ...(startPosition === undefined ? {} : { startPosition }),
79 position,
80 body,
81 }),
82 onRemove: (commentId) => removeComment(reviewKey, commentId),
83 onAskAgent: onAskAboutLines,
84 };
85 const guide = data?.guide ?? null;
86 const tourShown = active && guide !== null;
87 const reportTourShown = useEffectEvent(() => onTourShown?.());
88 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- tells the parent once the loaded guide tour is on screen, which data arrival can cause
89 useEffect(() => {
90 if (tourShown) reportTourShown();
91 }, [tourShown]);
92 const runState = data?.run ?? null;
93 const currentHeadSha = data?.currentHeadSha ?? null;
94 const stale = guide !== null && currentHeadSha !== null && currentHeadSha !== guide.headSha;
95 const failure =
96 runState?._tag === "Failed" && runState.headSha === currentHeadSha ? runState.detail : null;
97 const writeButton = (label: string, size: "sm" | "xs") =>
98 canWrite ? (
99 <Button size={size} variant="outline" disabled={running} onClick={() => void write()}>
100 {running ? <Spinner /> : <ScrollTextIcon aria-hidden className="size-3.5" />}
101 {running ? gt`Reading the change...` : label}
102 </Button>
103 ) : null;
104
105 if (guide === null) {
106 if (query.isPending && data === null) {
107 return (
108 <div
109 tabIndex={-1}
110 data-workspace-focus-default
111 className="flex min-h-48 items-center justify-center outline-none"
112 >
113 <Spinner />
114 </div>
115 );
116 }
117 return (
118 <div
119 tabIndex={-1}
120 data-workspace-focus-default
121 className="scrollbar-gutter-both flex h-full flex-col items-center justify-center-safe gap-2 overflow-y-auto px-4 py-10 text-center outline-none"
122 >
123 <p className="text-sm font-medium text-foreground">
124 {running
125 ? gt`Writing a review guide`
126 : failure === null
127 ? gt`No review guide yet`
128 : gt`Could not write a review guide`}
129 </p>
130 <p className="max-w-md text-xs text-muted-foreground">
131 {running
132 ? (query.error ??
133 gt`An agent is reading the change. A large one takes a few minutes. You can leave; you will be told when it is ready.`)
134 : failure === null
135 ? (query.error ??
136 gt`A guide is a tour of the change: stops through the code in reading order, with the calls only a maintainer can make.`)
137 : resolve(FAILURE_HINT)}
138 </p>
139 {writeButton(failure === null ? gt`Write guide` : gt`Try again`, "sm")}
140 {failure === null || running ? null : <FailureDetails detail={failure} />}
141 </div>
142 );
143 }
144
145 const banner =
146 stale || failure !== null || running ? (
147 <Alert variant={running ? "info" : "warning"}>
148 {running ? <Spinner /> : <TriangleAlertIcon />}
149 <AlertDescription>
150 {running ? (
151 gt`Rewriting this guide. You will be told when the new one is ready.`
152 ) : failure === null ? (
153 gt`Written before the latest push.`
154 ) : (
155 <>
156 {gt`Could not rewrite this guide. Check the review guide model in Settings → Source control.`}
157 <FailureDetails detail={failure} />
158 </>
159 )}
160 </AlertDescription>
161 {!running && canWrite ? <AlertAction>{writeButton(gt`Rewrite`, "xs")}</AlertAction> : null}
162 </Alert>
163 ) : null;
164
165 return (
166 <GuideProseContext value={{ environmentId, cwd: workspaceRoot }}>
167 <GuideTour
168 environmentId={environmentId}
169 reference={reference}
170 guide={guide}
171 banner={banner}
172 active={active}
173 commenting={commenting}
174 onAsk={
175 onAskAboutStop === null
176 ? null
177 : (index, question) => {
178 const stop = guide.stops[index];
179 if (stop !== undefined) onAskAboutStop(stop, index, question);
180 }
181 }
182 onFinish={onFinishReview}
183 onOpenFile={onOpenFile}
184 />
185 </GuideProseContext>
186 );
187}
188
189const FAILURE_HINT = msg("Check the review guide model in Settings → Source control & issues.");
190
191function FailureDetails({ detail }: { detail: string }) {
192 return (
193 <details className="w-full max-w-lg text-xs text-muted-foreground">
194 <summary className="cursor-pointer rounded-sm -outline-offset-2 hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring">
195 <T>Technical details</T>
196 </summary>
197 <pre className="scrollbar-gutter-both mt-2 max-h-40 overflow-auto whitespace-pre-wrap break-words rounded-lg border border-border bg-muted/40 p-3 text-left font-mono text-xs">
198 {detail}
199 </pre>
200 </details>
201 );
202}