| 1 | "use client"; |
| 2 | |
| 3 | import { Pencil, Plus |
| 4 | import Link from "next/link |
| 5 | import { useRouter } |
| 6 | import { useAction } |
| 7 | import { useState } |
| 8 | import { toast } |
| 9 | |
| 10 | import { |
| 11 | createLabel, |
| 12 | deleteLabel, |
| 13 | setIssueAssignees, |
| 14 | setIssueLabels, |
| 15 | } from "@/components/issues/actions"; |
| 16 | import { LabelBadge } |
| 17 | import { LabelForm } |
| 18 | import { Button } |
| 19 | import { Input } |
| 20 | import { Spinner } |
| 21 | import type { IssueLabel |
| 22 | import { cn } |
| 23 | |
| 24 | export function LabelEditor({ |
| 25 | username, |
| 26 | repo, |
| 27 | number, |
| 28 | attached, |
| 29 | available, |
| 30 | canEdit, |
| 31 | canManage, |
| 32 | }: { |
| 33 | username: string |
| 34 | repo: string |
| 35 | number: number |
| 36 | attached: IssueLabel |
| 37 | available: IssueLabel |
| 38 | canEdit: boolean |
| 39 | /** Create and delete the repository's labels, not just apply them. */ |
| 40 | canManage: boolean |
| 41 | }) { |
| 42 | const router = |
| 43 | const [editing, |
| 44 | const [creating, |
| 45 | const [selected, |
| 46 | attached.map((label |
| 47 | ); |
| 48 | |
| 49 | const save = |
| 50 | onSuccess: () => |
| 51 | setEditing(false); |
| 52 | router.refresh(); |
| 53 | }, |
| 54 | onError: ({ error |
| 55 | toast.error(error |
| 56 | }); |
| 57 | |
| 58 | const remove = |
| 59 | onSuccess: () => |
| 60 | onError: ({ error |
| 61 | toast.error(error |
| 62 | }); |
| 63 | |
| 64 | const create = |
| 65 | onSuccess: ({ data |
| 66 | // Attach it right away: creating one from here means you want it. |
| 67 | if (data) setSelected |
| 68 | setCreating(false); |
| 69 | router.refresh(); |
| 70 | }, |
| 71 | onError: ({ error |
| 72 | toast.error(error |
| 73 | }); |
| 74 | |
| 75 | function toggle(name |
| 76 | setSelected((current) |
| 77 | current.includes(name |
| 78 | ? current.filter( |
| 79 | : [...current, |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | if (!editing) |
| 84 | return ( |
| 85 | <div className="flex flex-col gap-2 |
| 86 | <div className="flex items-center justify-between |
| 87 | <h2 className="text-xs font-medium text-muted-foreground |
| 88 | {canEdit && ( |
| 89 | <Button |
| 90 | variant="ghost" |
| 91 | size="sm" |
| 92 | className="h-6 px-2 text-xs" |
| 93 | onClick={() => |
| 94 | setSelected(attached.map |
| 95 | setEditing(true); |
| 96 | }} |
| 97 | > |
| 98 | <Pencil data-icon=" |
| 99 | Edit |
| 100 | </Button> |
| 101 | )} |
| 102 | </div> |
| 103 | {attached.length === |
| 104 | <p className="text-xs text-muted-foreground |
| 105 | ) : ( |
| 106 | <div className="flex flex-wrap gap-1 |
| 107 | {attached.map(( |
| 108 | <LabelBadge key={ |
| 109 | ))} |
| 110 | </div> |
| 111 | )} |
| 112 | </div> |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | // Not a `<form>`: the create form below is one, and forms cannot nest. |
| 117 | return ( |
| 118 | <div className="flex flex-col gap-2 |
| 119 | <h2 className="text-xs font-medium text-muted-foreground |
| 120 | {available.length === |
| 121 | <p className="text-xs text-muted-foreground |
| 122 | No labels in this repository yet. |
| 123 | </p> |
| 124 | ) : ( |
| 125 | <div className="flex max-h-48 flex-col gap-1 overflow-y-auto |
| 126 | {available.map(( |
| 127 | <label |
| 128 | key={label.id |
| 129 | title={label.description |
| 130 | className="group flex cursor-pointer items-center gap-2 rounded px-1 py-0.5 text-xs hover:bg-muted/50" |
| 131 | > |
| 132 | <input |
| 133 | type="checkbox" |
| 134 | checked={selected.includes |
| 135 | onChange={() => |
| 136 | className="accent-current" |
| 137 | /> |
| 138 | <span |
| 139 | className="size-2.5 shrink-0 rounded-full" |
| 140 | style={{ backgroundColor: |
| 141 | /> |
| 142 | <span className="min-w-0 flex-1 truncate |
| 143 | {canManage && ( |
| 144 | <button |
| 145 | type="button" |
| 146 | title={`Delete label ${label |
| 147 | className="invisible text-muted-foreground group-hover:visible hover:text-destructive" |
| 148 | onClick={() => |
| 149 | remove.execute({ |
| 150 | } |
| 151 | > |
| 152 | <Trash2 className=" |
| 153 | </button> |
| 154 | )} |
| 155 | </label> |
| 156 | ))} |
| 157 | </div> |
| 158 | )} |
| 159 | |
| 160 | {canManage && ( |
| 161 | <div className="border-t pt-2 |
| 162 | {creating ? ( |
| 163 | <LabelForm |
| 164 | id={`issue-${number |
| 165 | submitText="Create" |
| 166 | pending={create.isExecuting |
| 167 | onCancel={() => |
| 168 | onSubmit={(values) |
| 169 | create.execute({ |
| 170 | } |
| 171 | /> |
| 172 | ) : ( |
| 173 | <Button |
| 174 | variant="outline" |
| 175 | size="sm" |
| 176 | className="w-full" |
| 177 | onClick={() => |
| 178 | > |
| 179 | <Plus data-icon=" |
| 180 | New label |
| 181 | </Button> |
| 182 | )} |
| 183 | </div> |
| 184 | )} |
| 185 | |
| 186 | <div className="flex items-center justify-between gap-2 |
| 187 | <Link |
| 188 | href={`/${username |
| 189 | className="text-xs text-muted-foreground hover:underline" |
| 190 | > |
| 191 | Manage labels |
| 192 | </Link> |
| 193 | <div className="flex gap-2 |
| 194 | <Button |
| 195 | type="button" |
| 196 | variant="outline" |
| 197 | size="sm" |
| 198 | disabled={save.isExecuting |
| 199 | onClick={() => |
| 200 | > |
| 201 | Cancel |
| 202 | </Button> |
| 203 | <Button |
| 204 | type="button" |
| 205 | size="sm" |
| 206 | disabled={save.isExecuting |
| 207 | onClick={() => |
| 208 | save.execute({ |
| 209 | } |
| 210 | > |
| 211 | {save.isExecuting && |
| 212 | Save |
| 213 | </Button> |
| 214 | </div> |
| 215 | </div> |
| 216 | </div> |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | export function AssigneeEditor({ |
| 221 | username, |
| 222 | repo, |
| 223 | number, |
| 224 | assignees, |
| 225 | canEdit, |
| 226 | }: { |
| 227 | username: string |
| 228 | repo: string |
| 229 | number: number |
| 230 | assignees: string |
| 231 | canEdit: boolean |
| 232 | }) { |
| 233 | const router = |
| 234 | const [editing, |
| 235 | const [draft, |
| 236 | |
| 237 | const save = |
| 238 | onSuccess: () => |
| 239 | setEditing(false); |
| 240 | router.refresh(); |
| 241 | }, |
| 242 | onError: ({ error |
| 243 | toast.error( |
| 244 | error.serverError ?? |
| 245 | ), |
| 246 | }); |
| 247 | |
| 248 | if (!editing) |
| 249 | return ( |
| 250 | <div className="flex flex-col gap-2 |
| 251 | <div className="flex items-center justify-between |
| 252 | <h2 className="text-xs font-medium text-muted-foreground |
| 253 | Assignees |
| 254 | </h2> |
| 255 | {canEdit && ( |
| 256 | <Button |
| 257 | variant="ghost" |
| 258 | size="sm" |
| 259 | className="h-6 px-2 text-xs" |
| 260 | onClick={() => |
| 261 | setDraft(assignees.join |
| 262 | setEditing(true); |
| 263 | }} |
| 264 | > |
| 265 | <Pencil data-icon=" |
| 266 | Edit |
| 267 | </Button> |
| 268 | )} |
| 269 | </div> |
| 270 | {assignees.length === |
| 271 | <p className="text-xs text-muted-foreground |
| 272 | ) : ( |
| 273 | <ul className="flex flex-col gap-1 |
| 274 | {assignees.map(( |
| 275 | <li key={name |
| 276 | <Link |
| 277 | href={`/${name |
| 278 | className="text-primary hover:underline" |
| 279 | > |
| 280 | {name} |
| 281 | </Link> |
| 282 | </li> |
| 283 | ))} |
| 284 | </ul> |
| 285 | )} |
| 286 | </div> |
| 287 | ); |
| 288 | } |
| 289 | |
| 290 | return ( |
| 291 | <form |
| 292 | className={cn(" |
| 293 | onSubmit={(event) |
| 294 | event.preventDefault(); |
| 295 | save.execute({ |
| 296 | username, |
| 297 | repo, |
| 298 | number, |
| 299 | usernames: draft |
| 300 | .split(",") |
| 301 | .map((name) |
| 302 | .filter(Boolean), |
| 303 | }); |
| 304 | }} |
| 305 | > |
| 306 | <h2 className="text-xs font-medium text-muted-foreground |
| 307 | <Input |
| 308 | value={draft} |
| 309 | onChange={(event) |
| 310 | placeholder="octocat, hubot" |
| 311 | autoComplete="off" |
| 312 | autoFocus |
| 313 | disabled={save.isExecuting |
| 314 | /> |
| 315 | <div className="flex justify-end gap-2 |
| 316 | <Button |
| 317 | type="button" |
| 318 | variant="outline" |
| 319 | size="sm" |
| 320 | disabled={save.isExecuting |
| 321 | onClick={() => |
| 322 | > |
| 323 | Cancel |
| 324 | </Button> |
| 325 | <Button type=" |
| 326 | {save.isExecuting && |
| 327 | Save |
| 328 | </Button> |
| 329 | </div> |
| 330 | </form> |
| 331 | ); |
| 332 | } |