Open History to see the last commit
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="flex min-h-48 flex-col items-center justify-center gap-2 px-4 py-10 text-center">
108 <p className="text-sm font-medium text-foreground">
109 {running ? "Writing a review guide" : "No review guide yet"}
110 </p>
111 <p className="max-w-md text-xs text-muted-foreground">
112 {failure ??
113 query.error ??
114 (running
115 ? "An agent is reading the change. A large one takes a few minutes. You can leave; you will be told when it is ready."
116 : "A guide is a tour of the change: stops through the code in reading order, with the calls only a maintainer can make.")}
117 </p>
118 {writeButton(failure === null ? "Write guide" : "Try again", "sm")}
119 </div>
120 );
121 }
122
123 const banner =
124 stale || failure !== null || running ? (
125 <Alert variant={running ? "info" : "warning"}>
126 {running ? <Spinner /> : <TriangleAlertIcon />}
127 <AlertDescription>
128 {running
129 ? "Rewriting this guide. You will be told when the new one is ready."
130 : (failure ?? "Written before the latest push.")}
131 </AlertDescription>
132 {!running && canWrite ? <AlertAction>{writeButton("Rewrite", "xs")}</AlertAction> : null}
133 </Alert>
134 ) : null;
135
136 return (
137 <GuideProseContext value={{ environmentId, cwd: workspaceRoot }}>
138 <GuideTour
139 environmentId={environmentId}
140 reference={reference}
141 guide={guide}
142 banner={banner}
143 active={active}
144 commenting={commenting}
145 onAsk={
146 onAskAboutStop === null
147 ? null
148 : (index, question) => {
149 const stop = guide.stops[index];
150 if (stop !== undefined) onAskAboutStop(stop, index, question);
151 }
152 }
153 onFinish={onFinishReview}
154 onOpenFile={onOpenFile}
155 />
156 </GuideProseContext>
157 );
158}