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

PullRequestGuideTab.tsx

157 lines5.5 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 useEffect(() => {
82 if (tourShown) reportTourShown();
83 }, [tourShown]);
84 const runState = data?.run ?? null;
85 const currentHeadSha = data?.currentHeadSha ?? null;
86 const stale = guide !== null && currentHeadSha !== null && currentHeadSha !== guide.headSha;
87 const failure =
88 runState?._tag === "Failed" && runState.headSha === currentHeadSha ? runState.detail : null;
89 const writeButton = (label: string, size: "sm" | "xs") =>
90 canWrite ? (
91 <Button size={size} variant="outline" disabled={running} onClick={() => void write()}>
92 {running ? <Spinner /> : <SparklesIcon aria-hidden className="size-3.5" />}
93 {running ? "Reading the change..." : label}
94 </Button>
95 ) : null;
96​
97 if (guide === null) {
98 if (query.isPending && data === null) {
99 return (
100 <div className="flex min-h-48 items-center justify-center">
101 <Spinner />
102 </div>
103 );
104 }
105 return (
106 <div className="flex min-h-48 flex-col items-center justify-center gap-2 px-4 py-10 text-center">
107 <p className="text-sm font-medium text-foreground">
108 {running ? "Writing a review guide" : "No review guide yet"}
109 </p>
110 <p className="max-w-md text-xs text-muted-foreground">
111 {failure ??
112 query.error ??
113 (running
114 ? "An agent is reading the change. A large one takes a few minutes. You can leave; you will be told when it is ready."
115 : "A guide is a tour of the change: stops through the code in reading order, with the calls only a maintainer can make.")}
116 </p>
117 {writeButton(failure === null ? "Write guide" : "Try again", "sm")}
118 </div>
119 );
120 }
121​
122 const banner =
123 stale || failure !== null || running ? (
124 <Alert variant={running ? "info" : "warning"}>
125 {running ? <Spinner /> : <TriangleAlertIcon />}
126 <AlertDescription>
127 {running
128 ? "Rewriting this guide. You will be told when the new one is ready."
129 : (failure ?? "Written before the latest push.")}
130 </AlertDescription>
131 {!running && canWrite ? <AlertAction>{writeButton("Rewrite", "xs")}</AlertAction> : null}
132 </Alert>
133 ) : null;
134​
135 return (
136 <GuideProseContext value={{ environmentId, cwd: workspaceRoot }}>
137 <GuideTour
138 environmentId={environmentId}
139 reference={reference}
140 guide={guide}
141 banner={banner}
142 active={active}
143 commenting={commenting}
144 onAsk={
145 onAskAboutStop === null
146 ? null
147 : (index, question) => {
148 const stop = guide.stops[index];
149 if (stop !== undefined) onAskAboutStop(stop, index, question);
150 }
151 }
152 onFinish={onFinishReview}
153 onOpenFile={onOpenFile}
154 />
155 </GuideProseContext>
156 );