| 1 | "use client"; |
| 2 | |
| 3 | import { Pencil, PlusIcon, Trash2 } from "lucide-react"; |
| 4 | import Link from "next/link |
| 5 | import { useRouter } |
| 6 | import { useAction } |
| 7 | import { useState } |
| 8 | import { toast } |
| 9 | |
| 10 | import { |
| 11 | AlertDialog, |
| 12 | AlertDialogCancel, |
| 13 | AlertDialogContent, |
| 14 | AlertDialogDescription, |
| 15 | AlertDialogFooter, |
| 16 | AlertDialogHeader, |
| 17 | AlertDialogTitle, |
| 18 | } from "@/components/ui/alert-dialog"; |
| 19 | import { Button } |
| 20 | import { Spinner } |
| 21 | import type { IssueLabel |
| 22 | import { createLabel, deleteLabel |
| 23 | import { LabelBadge } |
| 24 | import { LabelForm } |
| 25 | |
| 26 | export function LabelManager({ |
| 27 | username, |
| 28 | repo, |
| 29 | labels, |
| 30 | canEdit, |
| 31 | }: { |
| 32 | username: string |
| 33 | repo: string |
| 34 | labels: IssueLabel |
| 35 | canEdit: boolean |
| 36 | }) { |
| 37 | const router = |
| 38 | const [creating, |
| 39 | const [editingId, |
| 40 | const [pendingDelete, |
| 41 | |
| 42 | function reportError(fallback |
| 43 | return ({ error |
| 44 | toast.error(error |
| 45 | } |
| 46 | |
| 47 | const create = |
| 48 | onSuccess: () => |
| 49 | setCreating(false); |
| 50 | router.refresh(); |
| 51 | }, |
| 52 | onError: reportError("Could not create this label. |
| 53 | }); |
| 54 | |
| 55 | const update = |
| 56 | onSuccess: () => |
| 57 | setEditingId(null); |
| 58 | router.refresh(); |
| 59 | }, |
| 60 | onError: reportError("Could not save this label. |
| 61 | }); |
| 62 | |
| 63 | const remove = |
| 64 | onSuccess: () => |
| 65 | setPendingDelete(null); |
| 66 | router.refresh(); |
| 67 | }, |
| 68 | onError: reportError("Could not delete this label. |
| 69 | }); |
| 70 | |
| 71 | return ( |
| 72 | <div className="flex flex-col gap-4 |
| 73 | <div className="flex flex-wrap items-center gap-2 |
| 74 | <h1 className="text-lg font-semibold |
| 75 | Labels |
| 76 | <span className="ml-2 text-sm font-normal text-muted-foreground tabular-nums |
| 77 | {labels.length} |
| 78 | </span> |
| 79 | </h1> |
| 80 | {canEdit && !creating |
| 81 | <Button |
| 82 | size="sm" |
| 83 | className="ml-auto" |
| 84 | onClick={() => |
| 85 | setEditingId(null); |
| 86 | setCreating(true); |
| 87 | }} |
| 88 | > |
| 89 | <PlusIcon data-icon=" |
| 90 | New label |
| 91 | </Button> |
| 92 | )} |
| 93 | </div> |
| 94 | |
| 95 | {creating && ( |
| 96 | <div className="rounded-lg border bg-muted/20 px-4 py-3 |
| 97 | <LabelForm |
| 98 | id="new-label" |
| 99 | submitText="Create label" |
| 100 | pending={create.isExecuting |
| 101 | onCancel={() => |
| 102 | onSubmit={(values) |
| 103 | /> |
| 104 | </div> |
| 105 | )} |
| 106 | |
| 107 | <div className="overflow-hidden rounded-lg border |
| 108 | {labels.length === |
| 109 | <p className="py-16 text-center text-sm text-muted-foreground |
| 110 | This repository has no labels yet. |
| 111 | </p> |
| 112 | ) : ( |
| 113 | <ul className="divide-y |
| 114 | {labels.map(( |
| 115 | label.id === |
| 116 | <li key={label |
| 117 | <LabelForm |
| 118 | id={`label-${label |
| 119 | label={label} |
| 120 | submitText="Save" |
| 121 | pending={update.isExecuting |
| 122 | onCancel={() => |
| 123 | onSubmit={({ name |
| 124 | update.execute({ |
| 125 | username, |
| 126 | repo, |
| 127 | labelId: label.id |
| 128 | name, |
| 129 | // An emptied field clears the description. |
| 130 | description: description?.trim |
| 131 | color, |
| 132 | }) |
| 133 | } |
| 134 | /> |
| 135 | </li> |
| 136 | ) : ( |
| 137 | <li |
| 138 | key={label.id |
| 139 | className="flex flex-wrap items-center gap-x-3 gap-y-1 px-4 py-3 text-sm" |
| 140 | > |
| 141 | <Link |
| 142 | href={`/${username |
| 143 | title={`Show issues labelled ${label |
| 144 | > |
| 145 | <LabelBadge label={ |
| 146 | </Link> |
| 147 | <span className="min-w-0 flex-1 truncate text-xs text-muted-foreground |
| 148 | {label.description} |
| 149 | </span> |
| 150 | {canEdit && ( |
| 151 | <div className="flex gap-1 |
| 152 | <Button |
| 153 | variant="ghost" |
| 154 | size="sm" |
| 155 | onClick={() => |
| 156 | setCreating(false); |
| 157 | setEditingId(label.id |
| 158 | }} |
| 159 | > |
| 160 | <Pencil data-icon=" |
| 161 | Edit |
| 162 | </Button> |
| 163 | <Button |
| 164 | variant="ghost" |
| 165 | size="sm" |
| 166 | onClick={() => |
| 167 | > |
| 168 | <Trash2 data-icon=" |
| 169 | Delete |
| 170 | </Button> |
| 171 | </div> |
| 172 | )} |
| 173 | </li> |
| 174 | ), |
| 175 | )} |
| 176 | </ul> |
| 177 | )} |
| 178 | </div> |
| 179 | |
| 180 | <AlertDialog |
| 181 | open={pendingDelete !== |
| 182 | onOpenChange={(open) |
| 183 | > |
| 184 | <AlertDialogContent> |
| 185 | <AlertDialogHeader> |
| 186 | <AlertDialogTitle> |
| 187 | Delete the “{pendingDelete?.name}” label? |
| 188 | </AlertDialogTitle> |
| 189 | <AlertDialogDescription> |
| 190 | It is removed from every issue that carries it. This cannot be |
| 191 | undone. |
| 192 | </AlertDialogDescription> |
| 193 | </AlertDialogHeader> |
| 194 | <AlertDialogFooter> |
| 195 | <AlertDialogCancel disabled={ |
| 196 | Cancel |
| 197 | </AlertDialogCancel> |
| 198 | <Button |
| 199 | type="button" |
| 200 | variant="destructive" |
| 201 | disabled={remove.isExecuting |
| 202 | onClick={() => |
| 203 | pendingDelete && |
| 204 | remove.execute({ |
| 205 | username, |
| 206 | repo, |
| 207 | labelId: pendingDelete.id |
| 208 | }) |
| 209 | } |
| 210 | > |
| 211 | {remove.isExecuting && |
| 212 | Delete label |
| 213 | </Button> |
| 214 | </AlertDialogFooter> |
| 215 | </AlertDialogContent> |
| 216 | </AlertDialog> |
| 217 | </div> |
| 218 | ); |
| 219 | } |