| 1 | "use client"; |
| 2 | |
| 3 | import { CircleCheck, CircleDot |
| 4 | import Link from "next/link |
| 5 | import { useRouter } |
| 6 | import { useAction } |
| 7 | import { type ReactNode, |
| 8 | import { toast } |
| 9 | |
| 10 | import { |
| 11 | closeIssue, |
| 12 | commentOnIssue, |
| 13 | deleteIssueComment, |
| 14 | reopenIssue, |
| 15 | updateIssueComment, |
| 16 | } from "@/components/issues/actions"; |
| 17 | import { |
| 18 | deleteReviewComment, |
| 19 | updateReviewComment, |
| 20 | updateReviewSummary, |
| 21 | } from "@/components/pull-requests/actions"; |
| 22 | import { MarkdownEditor } |
| 23 | import { Button } |
| 24 | import { Spinner } |
| 25 | |
| 26 | export function CommentBox({ |
| 27 | username, |
| 28 | repo, |
| 29 | number, |
| 30 | signedIn, |
| 31 | state, |
| 32 | canChangeState = false, |
| 33 | }: { |
| 34 | username: string |
| 35 | repo: string |
| 36 | number: number |
| 37 | signedIn: boolean |
| 38 | state?: string |
| 39 | canChangeState?: boolean |
| 40 | }) { |
| 41 | const router = |
| 42 | const [body, |
| 43 | |
| 44 | const close = |
| 45 | onSuccess: () => |
| 46 | toast.success("Issue closed. |
| 47 | router.refresh(); |
| 48 | }, |
| 49 | onError: ({ error |
| 50 | toast.error(error |
| 51 | }); |
| 52 | |
| 53 | const reopen = |
| 54 | onSuccess: () => |
| 55 | toast.success("Issue reopened. |
| 56 | router.refresh(); |
| 57 | }, |
| 58 | onError: ({ error |
| 59 | toast.error(error |
| 60 | }); |
| 61 | |
| 62 | const comment = |
| 63 | onSuccess: () => |
| 64 | setBody(""); |
| 65 | router.refresh(); |
| 66 | }, |
| 67 | onError: ({ error |
| 68 | toast.error( |
| 69 | error.validationErrors?.body |
| 70 | error.serverError ?? |
| 71 | "Could not post this comment.", |
| 72 | ), |
| 73 | }); |
| 74 | |
| 75 | if (!signedIn) |
| 76 | return ( |
| 77 | <p className="rounded-lg border px-4 py-3 text-sm text-muted-foreground |
| 78 | <Link href=" |
| 79 | Sign in |
| 80 | </Link>{" " |
| 81 | to join the conversation. |
| 82 | </p> |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | return ( |
| 87 | <form |
| 88 | className="flex flex-col gap-2 rounded-lg border px-4 py-3" |
| 89 | onSubmit={(event) |
| 90 | event.preventDefault(); |
| 91 | comment.execute({ |
| 92 | }} |
| 93 | > |
| 94 | <MarkdownEditor |
| 95 | value={body} |
| 96 | onChange={setBody} |
| 97 | repository={{ username |
| 98 | placeholder="Leave a comment" |
| 99 | disabled={comment.isExecuting |
| 100 | /> |
| 101 | |
| 102 | <div className="flex flex-wrap justify-end gap-2 |
| 103 | {canChangeState && |
| 104 | (state === "open |
| 105 | <Button |
| 106 | type="button" |
| 107 | size="sm" |
| 108 | variant="outline" |
| 109 | disabled={close.isExecuting |
| 110 | onClick={() => |
| 111 | > |
| 112 | {close.isExecuting && |
| 113 | <CircleCheck data-icon=" |
| 114 | Close issue |
| 115 | </Button> |
| 116 | ) : ( |
| 117 | <Button |
| 118 | type="button" |
| 119 | size="sm" |
| 120 | variant="outline" |
| 121 | disabled={reopen.isExecuting |
| 122 | onClick={() => |
| 123 | > |
| 124 | {reopen.isExecuting && |
| 125 | <CircleDot data-icon=" |
| 126 | Reopen issue |
| 127 | </Button> |
| 128 | ))} |
| 129 | <Button |
| 130 | type="submit" |
| 131 | size="sm" |
| 132 | disabled={comment.isExecuting |
| 133 | > |
| 134 | {comment.isExecuting && |
| 135 | Comment |
| 136 | </Button> |
| 137 | </div> |
| 138 | </form> |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | // A review's summary can be edited but not deleted: the review stays, with or without one. |
| 143 | const commentActions = { |
| 144 | issue: { update: updateIssueComment |
| 145 | "review-comment": { |
| 146 | update: updateReviewComment, |
| 147 | remove: deleteReviewComment, |
| 148 | }, |
| 149 | review: { update: updateReviewSummary |
| 150 | }; |
| 151 | |
| 152 | /** A comment with its header; Edit and Delete sit at the header's right. Line comments sit in a thread, so their header is a plain row rather than a card's top bar. */ |
| 153 | export function CommentItem({ |
| 154 | kind = "issue" |
| 155 | username, |
| 156 | repo, |
| 157 | number, |
| 158 | commentId, |
| 159 | authorUsername, |
| 160 | body, |
| 161 | viewer, |
| 162 | canModerate, |
| 163 | header, |
| 164 | children, |
| 165 | }: { |
| 166 | kind?: keyof |
| 167 | username: string |
| 168 | repo: string |
| 169 | number: number |
| 170 | commentId: string |
| 171 | authorUsername: string |
| 172 | body: string |
| 173 | viewer?: string |
| 174 | canModerate: boolean |
| 175 | header: ReactNode |
| 176 | children: ReactNode |
| 177 | }) { |
| 178 | const router = |
| 179 | const [editing, |
| 180 | const [draft, |
| 181 | |
| 182 | // Only the author edits a comment; a moderator may only take it down. |
| 183 | const canEdit = |
| 184 | const canDelete = |
| 185 | |
| 186 | const actions = |
| 187 | const update = |
| 188 | onSuccess: () => |
| 189 | setEditing(false); |
| 190 | router.refresh(); |
| 191 | }, |
| 192 | onError: ({ error |
| 193 | toast.error(error |
| 194 | }); |
| 195 | |
| 196 | const remove = |
| 197 | onSuccess: () => |
| 198 | onError: ({ error |
| 199 | toast.error(error |
| 200 | }); |
| 201 | |
| 202 | const inThread = |
| 203 | const content = |
| 204 | <> |
| 205 | <div |
| 206 | className={ |
| 207 | inThread |
| 208 | ? "flex min-h-7 flex-wrap items-center gap-2 px-4 pt-3 text-xs text-muted-foreground" |
| 209 | : "flex min-h-11 flex-wrap items-center gap-1.5 border-b bg-muted/40 px-4 py-1.5 text-sm text-muted-foreground" |
| 210 | } |
| 211 | > |
| 212 | {header} |
| 213 | {(canEdit || ( |
| 214 | <div className="ml-auto flex gap-1 |
| 215 | {canEdit && ( |
| 216 | <Button |
| 217 | variant="ghost" |
| 218 | size="sm" |
| 219 | onClick={() => |
| 220 | setDraft(body); |
| 221 | setEditing(true); |
| 222 | }} |
| 223 | > |
| 224 | <Pencil data-icon=" |
| 225 | Edit |
| 226 | </Button> |
| 227 | )} |
| 228 | {canDelete && actions |
| 229 | <Button |
| 230 | variant="ghost" |
| 231 | size="sm" |
| 232 | disabled={remove.isExecuting |
| 233 | onClick={() => |
| 234 | remove.execute({ |
| 235 | } |
| 236 | > |
| 237 | {remove.isExecuting ? |
| 238 | <Spinner /> |
| 239 | ) : ( |
| 240 | <Trash2 data-icon=" |
| 241 | )} |
| 242 | Delete |
| 243 | </Button> |
| 244 | )} |
| 245 | </div> |
| 246 | )} |
| 247 | </div> |
| 248 | |
| 249 | <div className="px-4 py-3 |
| 250 | {editing ? ( |
| 251 | <form |
| 252 | className="flex flex-col gap-2" |
| 253 | onSubmit={(event) |
| 254 | event.preventDefault(); |
| 255 | update.execute({ |
| 256 | username, |
| 257 | repo, |
| 258 | number, |
| 259 | commentId, |
| 260 | body: draft, |
| 261 | }); |
| 262 | }} |
| 263 | > |
| 264 | <MarkdownEditor |
| 265 | value={draft} |
| 266 | onChange={setDraft} |
| 267 | repository={{ username |
| 268 | autoFocus |
| 269 | disabled={update.isExecuting |
| 270 | /> |
| 271 | <div className="flex justify-end gap-2 |
| 272 | <Button |
| 273 | type="button" |
| 274 | variant="outline" |
| 275 | size="sm" |
| 276 | disabled={update.isExecuting |
| 277 | onClick={() => |
| 278 | > |
| 279 | Cancel |
| 280 | </Button> |
| 281 | <Button |
| 282 | type="submit" |
| 283 | size="sm" |
| 284 | // Clearing a review's summary is allowed; a comment cannot be emptied. |
| 285 | disabled={ |
| 286 | update.isExecuting || |
| 287 | } |
| 288 | > |
| 289 | {update.isExecuting && |
| 290 | Save |
| 291 | </Button> |
| 292 | </div> |
| 293 | </form> |
| 294 | ) : ( |
| 295 | <div className="text-sm |
| 296 | )} |
| 297 | </div> |
| 298 | </> |
| 299 | ); |
| 300 | |
| 301 | return inThread ? |
| 302 | content |
| 303 | ) : ( |
| 304 | <div className="rounded-lg border |
| 305 | ); |
| 306 | } |