| 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 { Button } |
| 23 | import { Spinner } |
| 24 | import { Textarea } |
| 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 | <Textarea |
| 95 | value={body} |
| 96 | onChange={(event) |
| 97 | rows={4} |
| 98 | maxLength={20000} |
| 99 | placeholder="Leave a comment. Markdown is supported." |
| 100 | disabled={comment.isExecuting |
| 101 | /> |
| 102 | |
| 103 | <div className="flex flex-wrap justify-end gap-2 |
| 104 | {canChangeState && |
| 105 | (state === "open |
| 106 | <Button |
| 107 | type="button" |
| 108 | size="sm" |
| 109 | variant="outline" |
| 110 | disabled={close.isExecuting |
| 111 | onClick={() => |
| 112 | > |
| 113 | {close.isExecuting && |
| 114 | <CircleCheck data-icon=" |
| 115 | Close issue |
| 116 | </Button> |
| 117 | ) : ( |
| 118 | <Button |
| 119 | type="button" |
| 120 | size="sm" |
| 121 | variant="outline" |
| 122 | disabled={reopen.isExecuting |
| 123 | onClick={() => |
| 124 | > |
| 125 | {reopen.isExecuting && |
| 126 | <CircleDot data-icon=" |
| 127 | Reopen issue |
| 128 | </Button> |
| 129 | ))} |
| 130 | <Button |
| 131 | type="submit" |
| 132 | size="sm" |
| 133 | disabled={comment.isExecuting |
| 134 | > |
| 135 | {comment.isExecuting && |
| 136 | Comment |
| 137 | </Button> |
| 138 | </div> |
| 139 | </form> |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | // A review's summary can be edited but not deleted: the review stays, with or without one. |
| 144 | const commentActions = { |
| 145 | issue: { update: updateIssueComment |
| 146 | "review-comment": { |
| 147 | update: updateReviewComment, |
| 148 | remove: deleteReviewComment, |
| 149 | }, |
| 150 | review: { update: updateReviewSummary |
| 151 | }; |
| 152 | |
| 153 | /** 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. */ |
| 154 | export function CommentItem({ |
| 155 | kind = "issue" |
| 156 | username, |
| 157 | repo, |
| 158 | number, |
| 159 | commentId, |
| 160 | authorUsername, |
| 161 | body, |
| 162 | viewer, |
| 163 | canModerate, |
| 164 | header, |
| 165 | children, |
| 166 | }: { |
| 167 | kind?: keyof |
| 168 | username: string |
| 169 | repo: string |
| 170 | number: number |
| 171 | commentId: string |
| 172 | authorUsername: string |
| 173 | body: string |
| 174 | viewer?: string |
| 175 | canModerate: boolean |
| 176 | header: ReactNode |
| 177 | children: ReactNode |
| 178 | }) { |
| 179 | const router = |
| 180 | const [editing, |
| 181 | const [draft, |
| 182 | |
| 183 | // Only the author edits a comment; a moderator may only take it down. |
| 184 | const canEdit = |
| 185 | const canDelete = |
| 186 | |
| 187 | const actions = |
| 188 | const update = |
| 189 | onSuccess: () => |
| 190 | setEditing(false); |
| 191 | router.refresh(); |
| 192 | }, |
| 193 | onError: ({ error |
| 194 | toast.error(error |
| 195 | }); |
| 196 | |
| 197 | const remove = |
| 198 | onSuccess: () => |
| 199 | onError: ({ error |
| 200 | toast.error(error |
| 201 | }); |
| 202 | |
| 203 | const inThread = |
| 204 | const content = |
| 205 | <> |
| 206 | <div |
| 207 | className={ |
| 208 | inThread |
| 209 | ? "flex min-h-7 flex-wrap items-center gap-2 px-4 pt-3 text-xs text-muted-foreground" |
| 210 | : "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" |
| 211 | } |
| 212 | > |
| 213 | {header} |
| 214 | {(canEdit || ( |
| 215 | <div className="ml-auto flex gap-1 |
| 216 | {canEdit && ( |
| 217 | <Button |
| 218 | variant="ghost" |
| 219 | size="sm" |
| 220 | onClick={() => |
| 221 | setDraft(body); |
| 222 | setEditing(true); |
| 223 | }} |
| 224 | > |
| 225 | <Pencil data-icon=" |
| 226 | Edit |
| 227 | </Button> |
| 228 | )} |
| 229 | {canDelete && actions |
| 230 | <Button |
| 231 | variant="ghost" |
| 232 | size="sm" |
| 233 | disabled={remove.isExecuting |
| 234 | onClick={() => |
| 235 | remove.execute({ |
| 236 | } |
| 237 | > |
| 238 | {remove.isExecuting ? |
| 239 | <Spinner /> |
| 240 | ) : ( |
| 241 | <Trash2 data-icon=" |
| 242 | )} |
| 243 | Delete |
| 244 | </Button> |
| 245 | )} |
| 246 | </div> |
| 247 | )} |
| 248 | </div> |
| 249 | |
| 250 | <div className="px-4 py-3 |
| 251 | {editing ? ( |
| 252 | <form |
| 253 | className="flex flex-col gap-2" |
| 254 | onSubmit={(event) |
| 255 | event.preventDefault(); |
| 256 | update.execute({ |
| 257 | username, |
| 258 | repo, |
| 259 | number, |
| 260 | commentId, |
| 261 | body: draft, |
| 262 | }); |
| 263 | }} |
| 264 | > |
| 265 | <Textarea |
| 266 | value={draft} |
| 267 | onChange={(event) |
| 268 | rows={4} |
| 269 | maxLength={20000} |
| 270 | autoFocus |
| 271 | disabled={update.isExecuting |
| 272 | /> |
| 273 | <div className="flex justify-end gap-2 |
| 274 | <Button |
| 275 | type="button" |
| 276 | variant="outline" |
| 277 | size="sm" |
| 278 | disabled={update.isExecuting |
| 279 | onClick={() => |
| 280 | > |
| 281 | Cancel |
| 282 | </Button> |
| 283 | <Button |
| 284 | type="submit" |
| 285 | size="sm" |
| 286 | // Clearing a review's summary is allowed; a comment cannot be emptied. |
| 287 | disabled={ |
| 288 | update.isExecuting || |
| 289 | } |
| 290 | > |
| 291 | {update.isExecuting && |
| 292 | Save |
| 293 | </Button> |
| 294 | </div> |
| 295 | </form> |
| 296 | ) : ( |
| 297 | <div className="text-sm |
| 298 | )} |
| 299 | </div> |
| 300 | </> |
| 301 | ); |
| 302 | |
| 303 | return inThread ? |
| 304 | content |
| 305 | ) : ( |
| 306 | <div className="rounded-lg border |
| 307 | ); |
| 308 | } |