| 1 | "use client"; |
| 2 | |
| 3 | import { CircleCheck, CircleX, MessageSquare } from "lucide-react"; |
| 4 | import { useRouter } |
| 5 | import { useAction } |
| 6 | import { useState } |
| 7 | import { toast } |
| 8 | |
| 9 | import { DiffHunk } |
| 10 | import { FromNowHoverCard } |
| 11 | import { CommentItem } |
| 12 | import { Markdown } |
| 13 | import { UserLink } |
| 14 | import { |
| 15 | applySuggestion, |
| 16 | dismissReview, |
| 17 | replyToReviewComment, |
| 18 | } from "@/components/pull-requests/actions"; |
| 19 | import { Badge } |
| 20 | import { Button } |
| 21 | import { Input } |
| 22 | import { Spinner } |
| 23 | import { Textarea } |
| 24 | import type { |
| 25 | PullRequestReview, |
| 26 | ReviewThread as ReviewThreadData, |
| 27 | } from "@/types/pull-request"; |
| 28 | |
| 29 | const verdicts = { |
| 30 | approved: { |
| 31 | icon: CircleCheck, |
| 32 | text: "approved these changes", |
| 33 | tone: "text-emerald-500", |
| 34 | }, |
| 35 | changes_requested: { |
| 36 | icon: CircleX, |
| 37 | text: "requested changes", |
| 38 | tone: "text-red-500", |
| 39 | }, |
| 40 | commented: { |
| 41 | icon: MessageSquare, |
| 42 | text: "reviewed", |
| 43 | tone: "text-muted-foreground", |
| 44 | }, |
| 45 | } as const; |
| 46 | |
| 47 | type Viewing |
| 48 | username: string |
| 49 | repo: string |
| 50 | number: number |
| 51 | viewer?: string |
| 52 | /** May delete anyone's comments and dismiss reviews; editing stays with each comment's author. */ |
| 53 | canModerate: boolean |
| 54 | }; |
| 55 | |
| 56 | /** A submitted review in the conversation, laid out like GitHub's: a line saying who reviewed and how, then the summary and each file thread as cards of their own beneath it. */ |
| 57 | export function ReviewItem({ |
| 58 | review, |
| 59 | headSha, |
| 60 | ...viewing |
| 61 | }: Viewing |
| 62 | const { icon: |
| 63 | const { username, |
| 64 | const dismissable = |
| 65 | canModerate && review. |
| 66 | |
| 67 | return ( |
| 68 | <div className="flex flex-col gap-3 |
| 69 | <div className="flex flex-wrap items-center gap-1.5 px-4 text-sm text-muted-foreground |
| 70 | <Icon className={ |
| 71 | <UserLink |
| 72 | username={review.authorUsername |
| 73 | image={review.authorImage |
| 74 | avatar="sm" |
| 75 | /> |
| 76 | {text} <FromNowHoverCard |
| 77 | {review.dismissalMessage && |
| 78 | {dismissable && <DismissReview |
| 79 | </div> |
| 80 | |
| 81 | {(review.dismissalMessage |
| 82 | review.body || |
| 83 | review.comments.length |
| 84 | <div className="ml-6 flex flex-col gap-3 border-l-2 pl-4 |
| 85 | {review.dismissalMessage && |
| 86 | <p className="text-xs text-muted-foreground |
| 87 | <UserLink username={ |
| 88 | this review: {review.dismissalMessage} |
| 89 | </p> |
| 90 | )} |
| 91 | |
| 92 | {review.body && |
| 93 | <CommentItem |
| 94 | kind="review" |
| 95 | username={username} |
| 96 | repo={repo} |
| 97 | number={number} |
| 98 | commentId={review.id |
| 99 | authorUsername={review.authorUsername |
| 100 | body={review.body |
| 101 | viewer={viewer} |
| 102 | canModerate={canModerate} |
| 103 | header={ |
| 104 | <> |
| 105 | <UserLink |
| 106 | username={review.authorUsername |
| 107 | image={review.authorImage |
| 108 | avatar="sm" |
| 109 | /> |
| 110 | left a review |
| 111 | </> |
| 112 | } |
| 113 | > |
| 114 | <Markdown repository={ |
| 115 | </CommentItem> |
| 116 | )} |
| 117 | |
| 118 | {review.comments.map |
| 119 | <div key={thread |
| 120 | <div className="flex flex-wrap items-center gap-2 border-b bg-muted/40 px-4 py-2 font-mono text-xs |
| 121 | <span className="text-foreground |
| 122 | <span className="text-muted-foreground |
| 123 | {thread.startLine |
| 124 | ? `lines ${thread. |
| 125 | : `line ${thread. |
| 126 | </span> |
| 127 | {thread.commitSha !== |
| 128 | <Badge variant=" |
| 129 | )} |
| 130 | </div> |
| 131 | {thread.diffHunk && |
| 132 | <div className="border-b |
| 133 | <DiffHunk path={ |
| 134 | </div> |
| 135 | )} |
| 136 | <ReviewThread {...viewing |
| 137 | </div> |
| 138 | ))} |
| 139 | </div> |
| 140 | )} |
| 141 | </div> |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | /** A line comment, its replies, and a box to add one. A pending comment takes no replies until its review is submitted. */ |
| 146 | export function ReviewThread({ |
| 147 | thread, |
| 148 | pending = false, |
| 149 | canApply = false, |
| 150 | ...viewing |
| 151 | }: Viewing |
| 152 | thread: ReviewThreadData |
| 153 | pending?: boolean |
| 154 | /** The thread is on the head as it stands and the viewer may push to it, so its suggestions can be applied. */ |
| 155 | canApply?: boolean |
| 156 | }) { |
| 157 | const { username, |
| 158 | |
| 159 | return ( |
| 160 | <div className="bg-background font-sans |
| 161 | {[thread, ... |
| 162 | <div key={comment |
| 163 | <CommentItem |
| 164 | kind="review-comment" |
| 165 | {...viewing} |
| 166 | commentId={comment.id |
| 167 | authorUsername={comment.authorUsername |
| 168 | body={comment.body |
| 169 | header={ |
| 170 | <> |
| 171 | <UserLink |
| 172 | username={comment.authorUsername |
| 173 | image={comment.authorImage |
| 174 | avatar="sm" |
| 175 | /> |
| 176 | <FromNowHoverCard date={ |
| 177 | {pending && <Badge |
| 178 | </> |
| 179 | } |
| 180 | > |
| 181 | <Markdown repository={ |
| 182 | </CommentItem> |
| 183 | {canApply && viewer |
| 184 | <ApplySuggestion {...viewing |
| 185 | )} |
| 186 | </div> |
| 187 | ))} |
| 188 | {viewer && !pending |
| 189 | </div> |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | function ApplySuggestion({ |
| 194 | username, |
| 195 | repo, |
| 196 | number, |
| 197 | commentId, |
| 198 | }: Viewing |
| 199 | const router = |
| 200 | const apply = |
| 201 | onSuccess: () => |
| 202 | toast.success("Suggestion committed to the branch. |
| 203 | router.refresh(); |
| 204 | }, |
| 205 | onError: ({ error |
| 206 | toast.error(error |
| 207 | }); |
| 208 | |
| 209 | return ( |
| 210 | <div className="px-4 pb-3 |
| 211 | <Button |
| 212 | size="sm" |
| 213 | variant="outline" |
| 214 | disabled={apply.isExecuting |
| 215 | onClick={() => |
| 216 | > |
| 217 | {apply.isExecuting && |
| 218 | Apply suggestion |
| 219 | </Button> |
| 220 | </div> |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | function ReplyBox({ |
| 225 | username, |
| 226 | repo, |
| 227 | number, |
| 228 | commentId, |
| 229 | }: Viewing |
| 230 | const router = |
| 231 | const [open, |
| 232 | const [body, |
| 233 | |
| 234 | const reply = |
| 235 | onSuccess: () => |
| 236 | setBody(""); |
| 237 | setOpen(false); |
| 238 | router.refresh(); |
| 239 | }, |
| 240 | onError: ({ error |
| 241 | toast.error( |
| 242 | error.validationErrors?.body |
| 243 | error.serverError ?? |
| 244 | "Could not post this reply.", |
| 245 | ), |
| 246 | }); |
| 247 | |
| 248 | if (!open) |
| 249 | return ( |
| 250 | <div className="border-t px-4 py-2 |
| 251 | <Button variant=" |
| 252 | Reply |
| 253 | </Button> |
| 254 | </div> |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | return ( |
| 259 | <form |
| 260 | className="flex flex-col gap-2 border-t px-4 py-3" |
| 261 | onSubmit={(event) |
| 262 | event.preventDefault(); |
| 263 | reply.execute({ |
| 264 | }} |
| 265 | > |
| 266 | <Textarea |
| 267 | value={body} |
| 268 | onChange={(event) |
| 269 | rows={3} |
| 270 | maxLength={20000} |
| 271 | placeholder="Reply. Markdown is supported." |
| 272 | autoFocus |
| 273 | disabled={reply.isExecuting |
| 274 | /> |
| 275 | <div className="flex justify-end gap-2 |
| 276 | <Button |
| 277 | type="button" |
| 278 | variant="outline" |
| 279 | size="sm" |
| 280 | disabled={reply.isExecuting |
| 281 | onClick={() => |
| 282 | > |
| 283 | Cancel |
| 284 | </Button> |
| 285 | <Button |
| 286 | type="submit" |
| 287 | size="sm" |
| 288 | disabled={reply.isExecuting |
| 289 | > |
| 290 | {reply.isExecuting && |
| 291 | Reply |
| 292 | </Button> |
| 293 | </div> |
| 294 | </form> |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | function DismissReview({ |
| 299 | username, |
| 300 | repo, |
| 301 | number, |
| 302 | reviewId, |
| 303 | }: Viewing |
| 304 | const router = |
| 305 | const [open, |
| 306 | const [message, |
| 307 | |
| 308 | const dismiss = |
| 309 | onSuccess: () => |
| 310 | setOpen(false); |
| 311 | router.refresh(); |
| 312 | }, |
| 313 | onError: ({ error |
| 314 | toast.error( |
| 315 | error.validationErrors?.message |
| 316 | error.serverError ?? |
| 317 | "Could not dismiss this review.", |
| 318 | ), |
| 319 | }); |
| 320 | |
| 321 | if (!open) |
| 322 | return ( |
| 323 | <Button |
| 324 | variant="ghost" |
| 325 | size="sm" |
| 326 | className="ml-auto" |
| 327 | onClick={() => |
| 328 | > |
| 329 | Dismiss |
| 330 | </Button> |
| 331 | ); |
| 332 | } |
| 333 | |
| 334 | return ( |
| 335 | <form |
| 336 | className="ml-auto flex w-full gap-2 sm:w-auto" |
| 337 | onSubmit={(event) |
| 338 | event.preventDefault(); |
| 339 | dismiss.execute({ |
| 340 | }} |
| 341 | > |
| 342 | <Input |
| 343 | value={message} |
| 344 | onChange={(event) |
| 345 | maxLength={1000} |
| 346 | placeholder="Why is this review dismissed?" |
| 347 | autoFocus |
| 348 | disabled={dismiss.isExecuting |
| 349 | className="h-8" |
| 350 | /> |
| 351 | <Button |
| 352 | type="submit" |
| 353 | size="sm" |
| 354 | disabled={dismiss.isExecuting |
| 355 | > |
| 356 | {dismiss.isExecuting && |
| 357 | Dismiss |
| 358 | </Button> |
| 359 | </form> |
| 360 | ); |
| 361 | } |