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

PullRequestGuideTab.tsx

186 lines6.7 KB

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