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

PullRequestReviewAnnotation.tsx

236 lines7.5 KB

1/**
2 * Pull-request-specific annotations: conversations already on the host and comments queued for
3 * the review being written. New comment composition uses the shared diff annotation.
4 */
5import type { PullRequestReviewThread } from "@t3tools/contracts";
6import {
7 CheckCircle2Icon,
8 CircleIcon,
9 HammerIcon,
10 MessageSquareIcon,
11 Trash2Icon,
12} from "lucide-react";
13import { useRef, useState } from "react";
14​
15import { formatRelativeTimeLabel } from "~/timestampFormat";
16import { cn } from "~/lib/utils";
17​
18import { Button } from "../ui/button";
19import { Textarea } from "../ui/textarea";
20import { isCommentSubmitShortcut } from "../diffs/commentSubmitShortcut";
21import { PullRequestActorLabel } from "./pullRequestPresentation";
22import { PullRequestMarkdown } from "./PullRequestMarkdown";
23import type { PendingReviewComment } from "./pullRequestReviewStore";
24​
25const CARD_CLASS =
26 "mx-3 my-2 rounded-xl border border-border/70 bg-background p-3 text-sm shadow-sm";
27​
28/** Sends a reply on ⌘/Ctrl+Enter and abandons it on Escape. */
29function submitKeys(input: {
30 readonly value: string;
31 readonly pending: boolean;
32 readonly onSubmit: () => void;
33 readonly onCancel?: (() => void) | undefined;
34}) {
35 return (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
36 if (event.key === "Escape" && input.onCancel) {
37 event.preventDefault();
38 input.onCancel();
39 }
40 if (isCommentSubmitShortcut(event, input.value, input.pending)) {
41 event.preventDefault();
42 input.onSubmit();
43 }
44 };
46​
47/** A comment waiting to be sent with the rest of the review. */
48export function PendingReviewCommentCard({
49 comment,
50 onRemove,
51}: {
52 comment: PendingReviewComment;
53 onRemove: () => void;
54}) {
55 return (
56 <div
57 className={cn(CARD_CLASS, "border-dashed")}
58 contentEditable={false}
59 onPointerDown={(event) => event.stopPropagation()}
60 >
61 <div className="flex items-center gap-2 text-xs text-muted-foreground">
62 <MessageSquareIcon className="size-3.5" />
63 <span>Pending — sent when you submit the review</span>
64 <Button
65 size="icon-xs"
66 variant="ghost"
67 className="ml-auto"
68 aria-label="Discard this comment"
69 onClick={onRemove}
70 >
71 <Trash2Icon className="size-3.5" />
72 </Button>
73 </div>
74 <p className="mt-2 whitespace-pre-wrap leading-relaxed">{comment.body}</p>
75 </div>
76 );
78​
79/** A conversation already on the host, with whatever this host lets the reader do to it. */
80export function ReviewThreadCard({
81 thread,
82 workspaceRoot,
83 canReply,
84 canResolve,
85 pending,
86 fixPending,
87 onFix,
88 onReply,
89 onToggleResolved,
90}: {
91 thread: PullRequestReviewThread;
92 workspaceRoot: string;
93 canReply: boolean;
94 canResolve: boolean;
95 pending: boolean;
96 /** True while this thread's own hand-off is preparing, so only its button says so. */
97 fixPending?: boolean;
98 /** Absent where a thread is shown outside the pull request page's reach. */
99 onFix?: () => void;
100 /** Resolves to whether the host took it, so a reply that failed keeps the words it was given. */
101 onReply: (body: string) => Promise<boolean>;
102 onToggleResolved: () => void;
103}) {
104 // A resolved thread is finished work, so it opens collapsed and stays one line until asked for.
105 const [expanded, setExpanded] = useState(!thread.isResolved);
106 const [replying, setReplying] = useState(false);
107 const [reply, setReply] = useState("");
108 const sendingRef = useRef(false);
109​
110 const send = async () => {
111 const trimmed = reply.trim();
112 if (trimmed.length === 0 || pending || sendingRef.current) return;
113 sendingRef.current = true;
114 // Cleared only once the host has it. Otherwise a failed reply leaves an error toast and an
115 // empty box, and the words have to be written again.
116 try {
117 if (await onReply(trimmed)) {
118 setReply("");
119 setReplying(false);
120 }
121 } finally {
122 sendingRef.current = false;
123 }
124 };
125​
126 return (
127 <div
128 className={CARD_CLASS}
129 contentEditable={false}
130 onPointerDown={(event) => event.stopPropagation()}
131 >
132 <div className="flex items-center gap-2 text-xs text-muted-foreground">
133 {thread.isResolved ? (
134 <CheckCircle2Icon className="size-3.5 text-emerald-600 dark:text-emerald-500" />
135 ) : (
136 <CircleIcon className="size-3.5" />
137 )}
138 <button
139 type="button"
140 className="hover:text-foreground"
141 aria-expanded={expanded}
142 onClick={() => setExpanded((current) => !current)}
143 >
144 {thread.isResolved ? "Resolved" : "Open"} · {thread.comments.length}{" "}
145 {thread.comments.length === 1 ? "comment" : "comments"}
146 </button>
147 {thread.isOutdated ? <span>outdated</span> : null}
148 {onFix ? (
149 <Button
150 size="xs"
151 variant="ghost"
152 className="ml-auto"
153 disabled={pending || fixPending}
154 onClick={onFix}
155 >
156 <HammerIcon className="size-3" />
157 {fixPending ? "Preparing..." : "Fix in a thread"}
158 </Button>
159 ) : null}
160 {canResolve ? (
161 <Button
162 size="xs"
163 variant="ghost"
164 className={onFix ? undefined : "ml-auto"}
165 disabled={pending}
166 onClick={onToggleResolved}
167 >
168 {thread.isResolved ? "Unresolve" : "Resolve"}
169 </Button>
170 ) : null}
171 </div>
172​
173 {expanded ? (
174 <>
175 <div className="mt-2 space-y-3">
176 {thread.comments.map((comment) => (
177 <article key={comment.id} className="min-w-0">
178 <div className="flex items-center gap-2 text-xs text-muted-foreground">
179 <PullRequestActorLabel actor={comment.author} className="text-foreground" />
180 <span>{formatRelativeTimeLabel(comment.createdAt)}</span>
181 </div>
182 <PullRequestMarkdown
183 className="mt-1 text-sm"
184 text={comment.body}
185 cwd={workspaceRoot}
186 />
187 </article>
188 ))}
189 </div>
190​
191 {canReply ? (
192 replying ? (
193 <div className="mt-2">
194 <Textarea
195 autoFocus
196 size="sm"
197 value={reply}
198 placeholder="Reply"
199 aria-label="Reply to this conversation"
200 onChange={(event) => setReply(event.target.value)}
201 onKeyDown={submitKeys({
202 value: reply,
203 pending,
204 onSubmit: () => void send(),
205 onCancel: () => setReplying(false),
206 })}
207 />
208 <div className="mt-2 flex justify-end gap-2">
209 <Button size="xs" variant="ghost" onClick={() => setReplying(false)}>
210 Cancel
211 </Button>
212 <Button
213 size="xs"
214 disabled={pending || reply.trim().length === 0}
215 onClick={() => void send()}
216 >
217 Reply
218 </Button>
219 </div>
220 </div>
221 ) : (
222 <Button
223 size="xs"
224 variant="ghost"
225 className="mt-2 px-1"
226 onClick={() => setReplying(true)}
227 >
228 Reply
229 </Button>
230 )
231 ) : null}
232 </>
233 ) : null}
234 </div>
235 );