| 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 | export function CommentItem({ |
| 154 | kind = "issue" |
| 155 | username, |
| 156 | repo, |
| 157 | number, |
| 158 | commentId, |
| 159 | authorUsername, |
| 160 | body, |
| 161 | viewer, |
| 162 | canModerate, |
| 163 | children, |
| 164 | }: { |
| 165 | kind?: keyof |
| 166 | username: string |
| 167 | repo: string |
| 168 | number: number |
| 169 | commentId: string |
| 170 | authorUsername: string |
| 171 | body: string |
| 172 | viewer?: string |
| 173 | canModerate: boolean |
| 174 | children: ReactNode |
| 175 | }) { |
| 176 | const router = |
| 177 | const [editing, |
| 178 | const [draft, |
| 179 | |
| 180 | const canEdit = |
| 181 | |
| 182 | const actions = |
| 183 | const update = |
| 184 | onSuccess: () => |
| 185 | setEditing(false); |
| 186 | router.refresh(); |
| 187 | }, |
| 188 | onError: ({ error |
| 189 | toast.error(error |
| 190 | }); |
| 191 | |
| 192 | const remove = |
| 193 | onSuccess: () => |
| 194 | onError: ({ error |
| 195 | toast.error(error |
| 196 | }); |
| 197 | |
| 198 | return ( |
| 199 | <div className="px-4 py-3 |
| 200 | {editing ? ( |
| 201 | <form |
| 202 | className="flex flex-col gap-2" |
| 203 | onSubmit={(event) |
| 204 | event.preventDefault(); |
| 205 | update.execute({ |
| 206 | username, |
| 207 | repo, |
| 208 | number, |
| 209 | commentId, |
| 210 | body: draft, |
| 211 | }); |
| 212 | }} |
| 213 | > |
| 214 | <Textarea |
| 215 | value={draft} |
| 216 | onChange={(event) |
| 217 | rows={4} |
| 218 | maxLength={20000} |
| 219 | autoFocus |
| 220 | disabled={update.isExecuting |
| 221 | /> |
| 222 | <div className="flex justify-end gap-2 |
| 223 | <Button |
| 224 | type="button" |
| 225 | variant="outline" |
| 226 | size="sm" |
| 227 | disabled={update.isExecuting |
| 228 | onClick={() => |
| 229 | > |
| 230 | Cancel |
| 231 | </Button> |
| 232 | <Button |
| 233 | type="submit" |
| 234 | size="sm" |
| 235 | // Clearing a review's summary is allowed; a comment cannot be emptied. |
| 236 | disabled={ |
| 237 | update.isExecuting || |
| 238 | } |
| 239 | > |
| 240 | {update.isExecuting && |
| 241 | Save |
| 242 | </Button> |
| 243 | </div> |
| 244 | </form> |
| 245 | ) : ( |
| 246 | <> |
| 247 | <div className="text-sm |
| 248 | {canEdit && ( |
| 249 | <div className="mt-2 flex gap-1 |
| 250 | <Button |
| 251 | variant="ghost" |
| 252 | size="sm" |
| 253 | onClick={() => |
| 254 | setDraft(body); |
| 255 | setEditing(true); |
| 256 | }} |
| 257 | > |
| 258 | <Pencil data-icon=" |
| 259 | Edit |
| 260 | </Button> |
| 261 | {actions.remove && |
| 262 | <Button |
| 263 | variant="ghost" |
| 264 | size="sm" |
| 265 | disabled={remove.isExecuting |
| 266 | onClick={() => |
| 267 | remove.execute({ |
| 268 | } |
| 269 | > |
| 270 | <Trash2 data-icon=" |
| 271 | Delete |
| 272 | </Button> |
| 273 | )} |
| 274 | </div> |
| 275 | )} |
| 276 | </> |
| 277 | )} |
| 278 | </div> |
| 279 | ); |
| 280 | } |