Open History to see the last commit
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 {
6 EnvironmentId,
7 PullRequestRef,
8 PullRequestReviewThread,
9 PullRequestThreadComment,
10} from "@t3tools/contracts";
11import {
12 CheckCircle2Icon,
13 CircleIcon,
14 HammerIcon,
15 MessageSquareIcon,
16 PencilIcon,
17 Trash2Icon,
18} from "lucide-react";
19import { useRef, useState } from "react";
20
21import { formatRelativeTimeLabel } from "~/timestampFormat";
22import { cn } from "~/lib/utils";
23
24import { Button } from "../ui/button";
25import { Textarea } from "../ui/textarea";
26import { isCommentSubmitShortcut } from "../diffs/commentSubmitShortcut";
27import { PullRequestActorLabel } from "./pullRequestPresentation";
28import { PullRequestMarkdown } from "./PullRequestMarkdown";
29import { PullRequestMarkdownEditor } from "./PullRequestMarkdownEditor";
30import { PullRequestReactionBar } from "./PullRequestReactions";
31import type { PendingReviewComment } from "./pullRequestReviewStore";
32
33const CARD_CLASS =
34 "mx-3 my-2 rounded-xl border border-border/70 bg-background p-3 text-sm shadow-sm";
35
36/** Sends a reply on ⌘/Ctrl+Enter and abandons it on Escape. */
37function submitKeys(input: {
38 readonly value: string;
39 readonly pending: boolean;
40 readonly onSubmit: () => void;
41 readonly onCancel?: (() => void) | undefined;
42}) {
43 return (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
44 if (event.key === "Escape" && input.onCancel) {
45 event.preventDefault();
46 input.onCancel();
47 }
48 if (isCommentSubmitShortcut(event, input.value, input.pending)) {
49 event.preventDefault();
50 input.onSubmit();
51 }
52 };
53}
54
55/** A comment waiting to be sent with the rest of the review. */
56export function PendingReviewCommentCard({
57 comment,
58 onRemove,
59}: {
60 comment: PendingReviewComment;
61 onRemove: () => void;
62}) {
63 return (
64 <div
65 className={cn(CARD_CLASS, "border-dashed")}
66 contentEditable={false}
67 onPointerDown={(event) => event.stopPropagation()}
68 >
69 <div className="flex items-center gap-2 text-xs text-muted-foreground">
70 <MessageSquareIcon className="size-3.5" />
71 <span>Pending — sent when you submit the review</span>
72 <Button
73 size="icon-xs"
74 variant="ghost"
75 className="ml-auto"
76 aria-label="Discard this comment"
77 onClick={onRemove}
78 >
79 <Trash2Icon className="size-3.5" />
80 </Button>
81 </div>
82 <p className="mt-2 whitespace-pre-wrap leading-relaxed">{comment.body}</p>
83 </div>
84 );
85}
86
87/** A conversation already on the host, with whatever this host lets the reader do to it. */
88export function ReviewThreadCard({
89 thread,
90 workspaceRoot,
91 canReply,
92 canResolve,
93 canReact,
94 environmentId,
95 reference,
96 pending,
97 fixPending,
98 fixLabel = "Fix in a thread",
99 onFix,
100 onReply,
101 canEditComment,
102 onEditComment,
103 onToggleResolved,
104 onReacted,
105}: {
106 thread: PullRequestReviewThread;
107 workspaceRoot: string;
108 canReply: boolean;
109 canResolve: boolean;
110 canReact: boolean;
111 environmentId: EnvironmentId;
112 reference: PullRequestRef;
113 pending: boolean;
114 /** True while this thread's own hand-off is preparing, so only its button says so. */
115 fixPending?: boolean;
116 fixLabel?: string;
117 /** Absent where a thread is shown outside the pull request page's reach. */
118 onFix?: () => void;
119 /** Resolves to whether the host took it, so a reply that failed keeps the words it was given. */
120 onReply: (body: string) => Promise<boolean>;
121 /** Whether this reader wrote this remark, which is what rewriting one takes. */
122 canEditComment: (comment: PullRequestThreadComment) => boolean;
123 /** Resolves to whether the host took it, like `onReply`. */
124 onEditComment: (commentId: string, body: string) => Promise<boolean>;
125 onToggleResolved: () => void;
126 onReacted: () => void;
127}) {
128 // A resolved thread is finished work, so it opens collapsed and stays one line until asked for.
129 const [expanded, setExpanded] = useState(!thread.isResolved);
130 const [replying, setReplying] = useState(false);
131 const [reply, setReply] = useState("");
132 const [editingId, setEditingId] = useState<string | null>(null);
133 const [savingEdit, setSavingEdit] = useState(false);
134 const sendingRef = useRef(false);
135
136 const saveEdit = async (commentId: string, body: string) => {
137 if (savingEdit) return;
138 setSavingEdit(true);
139 const saved = await onEditComment(commentId, body);
140 setSavingEdit(false);
141 if (saved) setEditingId(null);
142 };
143
144 const send = async () => {
145 const trimmed = reply.trim();
146 if (trimmed.length === 0 || pending || sendingRef.current) return;
147 sendingRef.current = true;
148 // Cleared only once the host has it. Otherwise a failed reply leaves an error toast and an
149 // empty box, and the words have to be written again.
150 try {
151 if (await onReply(trimmed)) {
152 setReply("");
153 setReplying(false);
154 }
155 } finally {
156 sendingRef.current = false;
157 }
158 };
159
160 return (
161 <div
162 className={CARD_CLASS}
163 contentEditable={false}
164 onPointerDown={(event) => event.stopPropagation()}
165 >
166 <div className="flex items-center gap-2 text-xs text-muted-foreground">
167 {thread.isResolved ? (
168 <CheckCircle2Icon className="size-3.5 text-emerald-600 dark:text-emerald-500" />
169 ) : (
170 <CircleIcon className="size-3.5" />
171 )}
172 <button
173 type="button"
174 className="hover:text-foreground"
175 aria-expanded={expanded}
176 onClick={() => setExpanded((current) => !current)}
177 >
178 {thread.isResolved ? "Resolved" : "Open"} · {thread.comments.length}{" "}
179 {thread.comments.length === 1 ? "comment" : "comments"}
180 </button>
181 {thread.isOutdated ? <span>outdated</span> : null}
182 {onFix ? (
183 <Button
184 size="xs"
185 variant="ghost"
186 className="ml-auto"
187 disabled={pending || fixPending}
188 onClick={onFix}
189 >
190 <HammerIcon className="size-3" />
191 {fixPending ? "Preparing..." : fixLabel}
192 </Button>
193 ) : null}
194 {canResolve ? (
195 <Button
196 size="xs"
197 variant="ghost"
198 className={onFix ? undefined : "ml-auto"}
199 disabled={pending}
200 onClick={onToggleResolved}
201 >
202 {thread.isResolved ? "Unresolve" : "Resolve"}
203 </Button>
204 ) : null}
205 </div>
206
207 {expanded ? (
208 <>
209 <div className="mt-2 space-y-3">
210 {thread.comments.map((comment) => (
211 <article key={comment.id} className="group min-w-0">
212 <div className="flex items-center gap-2 text-xs text-muted-foreground">
213 <PullRequestActorLabel actor={comment.author} className="text-foreground" />
214 <span>{formatRelativeTimeLabel(comment.createdAt)}</span>
215 </div>
216 {editingId === comment.id ? (
217 <PullRequestMarkdownEditor
218 className="mt-1"
219 value={comment.body}
220 cwd={workspaceRoot}
221 label="Edit comment"
222 saving={savingEdit}
223 onSave={(body) => void saveEdit(comment.id, body)}
224 onCancel={() => setEditingId(null)}
225 />
226 ) : (
227 <div className="mt-1 flex items-start gap-1">
228 <PullRequestMarkdown
229 className="min-w-0 flex-1 text-sm"
230 text={comment.body}
231 cwd={workspaceRoot}
232 />
233 {canEditComment(comment) ? (
234 <Button
235 size="icon-xs"
236 variant="ghost"
237 className="shrink-0 text-muted-foreground opacity-0 transition-opacity group-focus-within:opacity-100 group-hover:opacity-100 focus-visible:opacity-100"
238 aria-label="Edit comment"
239 onClick={() => setEditingId(comment.id)}
240 >
241 <PencilIcon className="size-3" />
242 </Button>
243 ) : null}
244 </div>
245 )}
246 <PullRequestReactionBar
247 className="mt-1.5"
248 reactions={comment.reactions ?? []}
249 canReact={canReact}
250 subjectId={comment.id}
251 environmentId={environmentId}
252 reference={reference}
253 onRefresh={onReacted}
254 />
255 </article>
256 ))}
257 </div>
258
259 {canReply ? (
260 replying ? (
261 <div className="mt-2">
262 <Textarea
263 autoFocus
264 size="sm"
265 value={reply}
266 placeholder="Reply"
267 aria-label="Reply to this conversation"
268 onChange={(event) => setReply(event.target.value)}
269 onKeyDown={submitKeys({
270 value: reply,
271 pending,
272 onSubmit: () => void send(),
273 onCancel: () => setReplying(false),
274 })}
275 />
276 <div className="mt-2 flex justify-end gap-2">
277 <Button size="xs" variant="ghost" onClick={() => setReplying(false)}>
278 Cancel
279 </Button>
280 <Button
281 size="xs"
282 disabled={pending || reply.trim().length === 0}
283 onClick={() => void send()}
284 >
285 Reply
286 </Button>
287 </div>
288 </div>
289 ) : (
290 <Button
291 size="xs"
292 variant="ghost"
293 className="mt-2 px-1"
294 onClick={() => setReplying(true)}
295 >
296 Reply
297 </Button>
298 )
299 ) : null}
300 </>
301 ) : null}
302 </div>
303 );
304}