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 { guideRunKey, useGuideRunStore } from "./guide/guideRunStore";
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";
21
22export function PullRequestGuideTab({
23 environmentId,
24 reference,
25 title,
26 workspaceRoot,
27 active,
28 canComment,
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 onAskAboutLines: ((comment: ReviewCommentContext, request: string) => void) | null;
42 onAskAboutStop:
43 | ((stop: PullRequestReviewGuideStop, index: number, question: string) => void)
44 | null;
45 onFinishReview: (() => void) | null;
46 onOpenFile: (path: string) => void;
47 onTourShown?: (() => void) | undefined;
48}) {
49 const { query, data, running, canWrite, write } = useReviewGuideWriter({
50 environmentId,
51 reference,
52 title,
53 });
54 const runKey = guideRunKey(environmentId, reference);
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 pending,
65 onComment: ({ path, oldPath, position, body }) =>
66 addComment(reviewKey, {
67 id: nextPendingReviewCommentId(),
68 path,
69 ...(oldPath === null ? {} : { oldPath }),
70 position,
71 body,
72 }),
73 onRemove: (commentId) => removeComment(reviewKey, commentId),
74 onAskAgent: onAskAboutLines,
75 };
76 useEffect(() => {
77 if (!active) return;
78 useGuideRunStore.getState().setShown(runKey);
79 return () => {
80 if (useGuideRunStore.getState().shownKey === runKey)
81 useGuideRunStore.getState().setShown(null);
82 };
83 }, [active, runKey]);
84
85 const guide = data?.guide ?? null;
86 const tourShown = active && guide !== null;
87 const reportTourShown = useEffectEvent(() => onTourShown?.());
88 useEffect(() => {
89 if (tourShown) reportTourShown();
90 }, [tourShown]);
91 const runState = data?.run ?? null;
92 const currentHeadSha = data?.currentHeadSha ?? null;
93 const stale = guide !== null && currentHeadSha !== null && currentHeadSha !== guide.headSha;
94 const failure =
95 runState?._tag === "Failed" && runState.headSha === currentHeadSha ? runState.detail : null;
96 const writeButton = (label: string, size: "sm" | "xs") =>
97 canWrite ? (
98 <Button size={size} variant="outline" disabled={running} onClick={() => void write()}>
99 {running ? <Spinner /> : <SparklesIcon aria-hidden className="size-3.5" />}
100 {running ? "Reading the change..." : label}
101 </Button>
102 ) : null;
103
104 if (guide === null) {
105 if (query.isPending && data === null) {
106 return (
107 <div className="flex min-h-48 items-center justify-center">
108 <Spinner />
109 </div>
110 );
111 }
112 return (
113 <div className="flex min-h-48 flex-col items-center justify-center gap-2 px-4 py-10 text-center">
114 <p className="text-sm font-medium text-foreground">
115 {running ? "Writing a review guide" : "No review guide yet"}
116 </p>
117 <p className="max-w-md text-xs text-muted-foreground">
118 {failure ??
119 query.error ??
120 (running
121 ? "An agent is reading the change. A large one takes a few minutes. You can leave; you will be told when it is ready."
122 : "A guide is a tour of the change: stops through the code in reading order, with the calls only a maintainer can make.")}
123 </p>
124 {writeButton(failure === null ? "Write guide" : "Try again", "sm")}
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 ?? "Written before the latest push.")}
137 </AlertDescription>
138 {!running && canWrite ? <AlertAction>{writeButton("Rewrite", "xs")}</AlertAction> : null}
139 </Alert>
140 ) : null;
141
142 return (
143 <GuideProseContext value={{ environmentId, cwd: workspaceRoot }}>
144 <GuideTour
145 environmentId={environmentId}
146 reference={reference}
147 guide={guide}
148 banner={banner}
149 active={active}
150 commenting={commenting}
151 onAsk={
152 onAskAboutStop === null
153 ? null
154 : (index, question) => {
155 const stop = guide.stops[index];
156 if (stop !== undefined) onAskAboutStop(stop, index, question);
157 }
158 }
159 onFinish={onFinishReview}
160 onOpenFile={onOpenFile}
161 />
162 </GuideProseContext>
163 );
164}