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

PullRequestGuideTab.tsx

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