191 lines · 8.1 KBRaw
| 1 | import { LABEL_COLORS, MAX_LABEL_NAME_LENGTH } from "@refs/backend/contract"; |
| 2 | import { Banner, Button, Dialog, Empty, Input, Select, Table } from "@refs/design-system"; |
| 3 | import { LayerCard } from "@refs/design-system"; |
| 4 | import { PencilSimpleIcon, TagIcon, TrashIcon } from "@refs/design-system/icons"; |
| 5 | import { useState } from "react"; |
| 6 | import { Link, useFetcher } from "react-router"; |
| 7 | import type { Route } from "./+types/labels"; |
| 8 | import type { Label, LabelColor, LabelInput, Labels, LabelWithUsage, Outcome } from "@refs/backend/contract"; |
| 9 | import { AppShell, PageHeader } from "../components/shell"; |
| 10 | import { LabelBadge } from "@refs/design-system"; |
| 11 | import { failed, formText, found, signedIn } from "../lib/session"; |
| 12 | import { useOnSuccess } from "../components/use-on-success"; |
| 13 | |
| 14 | const COLOR_NAMES: Record<LabelColor, string> = { blue: "Blue", green: "Green", orange: "Orange", purple: "Purple", red: "Red", teal: "Teal", neutral: "Grey" }; |
| 15 | |
| 16 | export const meta: Route.MetaFunction = () => [{ title: "Labels · refs" }]; |
| 17 | |
| 18 | export async function loader({ context }: Route.LoaderArgs) { |
| 19 | const labels = await signedIn(context).labels(); |
| 20 | return { labels: await labels.list() }; |
| 21 | } |
| 22 | |
| 23 | export async function action({ request, context }: Route.ActionArgs) { |
| 24 | const labels = await signedIn(context).labels(); |
| 25 | const form = await request.formData(); |
| 26 | const intent = formText(form, "intent"); |
| 27 | |
| 28 | const input = { name: formText(form, "name"), color: formText(form, "color") as LabelColor, description: formText(form, "description") }; |
| 29 | const outcome = await found(labelAction(labels, intent, formText(form, "id"), input)); |
| 30 | if (!outcome.ok) return failed(outcome.problem, { intent }); |
| 31 | return { intent, error: null }; |
| 32 | } |
| 33 | |
| 34 | function labelAction(labels: Labels, intent: string, id: string, input: LabelInput): Promise<Outcome<unknown>> { |
| 35 | switch (intent) { |
| 36 | case "create": |
| 37 | return labels.create(input); |
| 38 | case "update": |
| 39 | return labels.update(id, input); |
| 40 | case "delete": |
| 41 | return labels.delete(id); |
| 42 | default: |
| 43 | return Promise.resolve({ ok: false, problem: { code: "invalid", message: "Unknown label action." } }); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | function LabelFields({ label, intent, submit, onDone }: { label?: Label; intent: "create" | "update"; submit: string; onDone?: () => void }) { |
| 48 | const fetcher = useFetcher<typeof action>(); |
| 49 | const [name, setName] = useState(label?.name ?? ""); |
| 50 | const [color, setColor] = useState<LabelColor>(label?.color ?? "blue"); |
| 51 | const [round, setRound] = useState(0); |
| 52 | useOnSuccess(fetcher, () => { |
| 53 | if (intent === "create") { |
| 54 | setName(""); |
| 55 | setRound((value) => value + 1); |
| 56 | } |
| 57 | onDone?.(); |
| 58 | }); |
| 59 | |
| 60 | return ( |
| 61 | <fetcher.Form key={round} method="post" className="flex flex-col gap-4"> |
| 62 | <input type="hidden" name="intent" value={intent} /> |
| 63 | {label && <input type="hidden" name="id" value={label.id} />} |
| 64 | <div className="flex min-h-7 items-center gap-2 text-compact text-muted"> |
| 65 | Preview <LabelBadge label={{ name: name || "Label preview", color }} /> |
| 66 | </div> |
| 67 | <div className="grid gap-4 md:grid-cols-[minmax(0,1fr)_minmax(0,2fr)_10rem]"> |
| 68 | <Input label="Name" name="name" value={name} onChange={(event) => setName(event.target.value)} maxLength={MAX_LABEL_NAME_LENGTH} required /> |
| 69 | <Input label="Description" name="description" defaultValue={label?.description} maxLength={200} /> |
| 70 | <Select label="Color" name="color" value={color} onValueChange={(value) => setColor(value as LabelColor)} items={Object.fromEntries(LABEL_COLORS.map((item) => [item, COLOR_NAMES[item]]))} /> |
| 71 | </div> |
| 72 | {fetcher.data?.error && <Banner variant="error" description={fetcher.data.error} />} |
| 73 | <div className="flex justify-end gap-2"> |
| 74 | {onDone && ( |
| 75 | <Button type="button" variant="secondary" onClick={onDone}> |
| 76 | Cancel |
| 77 | </Button> |
| 78 | )} |
| 79 | <Button type="submit" variant="primary" loading={fetcher.state !== "idle"}> |
| 80 | {submit} |
| 81 | </Button> |
| 82 | </div> |
| 83 | </fetcher.Form> |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | function DeleteLabel({ label }: { label: Label }) { |
| 88 | const fetcher = useFetcher<typeof action>(); |
| 89 | return ( |
| 90 | <Dialog.Root> |
| 91 | <Dialog.Trigger |
| 92 | render={(props) => ( |
| 93 | <Button {...props} variant="ghost" size="sm" shape="square" className="hit-area" aria-label={`Delete ${label.name}`}> |
| 94 | <TrashIcon size={14} /> |
| 95 | </Button> |
| 96 | )} |
| 97 | /> |
| 98 | <Dialog size="sm" className="p-6"> |
| 99 | <Dialog.Title className="m-0 text-lg font-semibold">Delete {label.name}?</Dialog.Title> |
| 100 | <Dialog.Description className="mt-2 mb-5 text-muted">It's removed from every task. Past timeline entries keep its name.</Dialog.Description> |
| 101 | <fetcher.Form method="post" className="flex justify-end gap-2"> |
| 102 | <input type="hidden" name="intent" value="delete" /> |
| 103 | <input type="hidden" name="id" value={label.id} /> |
| 104 | <Dialog.Close render={(props) => <Button {...props} variant="secondary">Cancel</Button>} /> |
| 105 | <Button type="submit" variant="primary" loading={fetcher.state !== "idle"}> |
| 106 | Delete label |
| 107 | </Button> |
| 108 | </fetcher.Form> |
| 109 | </Dialog> |
| 110 | </Dialog.Root> |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | function LabelRow({ label }: { label: LabelWithUsage }) { |
| 115 | const [editing, setEditing] = useState(false); |
| 116 | if (editing) { |
| 117 | return ( |
| 118 | <Table.Row> |
| 119 | <Table.Cell colSpan={4} className="py-4"> |
| 120 | <LabelFields label={label} intent="update" submit="Save label" onDone={() => setEditing(false)} /> |
| 121 | </Table.Cell> |
| 122 | </Table.Row> |
| 123 | ); |
| 124 | } |
| 125 | return ( |
| 126 | <Table.Row> |
| 127 | <Table.Cell className="py-2"> |
| 128 | <LabelBadge label={label} href={`/tasks?label=${encodeURIComponent(label.name)}`} /> |
| 129 | </Table.Cell> |
| 130 | <Table.Cell className="hidden max-w-0 truncate py-2 text-muted md:table-cell">{label.description}</Table.Cell> |
| 131 | <Table.Cell className="py-2 whitespace-nowrap text-muted tabular-nums"> |
| 132 | <Link to={`/tasks?label=${encodeURIComponent(label.name)}`} className="hover:text-accent-text"> |
| 133 | {label.openTasks} open |
| 134 | </Link> |
| 135 | </Table.Cell> |
| 136 | <Table.Cell className="py-2"> |
| 137 | <span className="flex items-center justify-end gap-3.5"> |
| 138 | <Button variant="ghost" size="sm" shape="square" className="hit-area" aria-label={`Edit ${label.name}`} onClick={() => setEditing(true)}> |
| 139 | <PencilSimpleIcon size={14} /> |
| 140 | </Button> |
| 141 | <DeleteLabel label={label} /> |
| 142 | </span> |
| 143 | </Table.Cell> |
| 144 | </Table.Row> |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | export default function Labels({ loaderData }: Route.ComponentProps) { |
| 149 | const { labels } = loaderData; |
| 150 | return ( |
| 151 | <AppShell crumbs={[{ label: "Tasks", href: "/tasks" }]}> |
| 152 | <PageHeader title="Labels" description="Group and filter tasks across every repository." /> |
| 153 | <div className="flex max-w-5xl flex-col gap-4"> |
| 154 | <LayerCard> |
| 155 | <LayerCard.Secondary>New label</LayerCard.Secondary> |
| 156 | <LayerCard.Primary className="p-4"> |
| 157 | <LabelFields intent="create" submit="Create label" /> |
| 158 | </LayerCard.Primary> |
| 159 | </LayerCard> |
| 160 | {labels.length === 0 ? ( |
| 161 | <Empty icon={<TagIcon size={40} />} title="No labels yet" description="Create labels like bug, feature or docs to organize tasks." /> |
| 162 | ) : ( |
| 163 | <LayerCard> |
| 164 | <LayerCard.Secondary> |
| 165 | <span className="tabular-nums"> |
| 166 | {labels.length} {labels.length === 1 ? "label" : "labels"} |
| 167 | </span> |
| 168 | </LayerCard.Secondary> |
| 169 | <Table> |
| 170 | <Table.Header> |
| 171 | <Table.Row> |
| 172 | <Table.Head className="py-2">Label</Table.Head> |
| 173 | <Table.Head className="hidden py-2 md:table-cell">Description</Table.Head> |
| 174 | <Table.Head className="py-2">Tasks</Table.Head> |
| 175 | <Table.Head className="py-2"> |
| 176 | <span className="sr-only">Actions</span> |
| 177 | </Table.Head> |
| 178 | </Table.Row> |
| 179 | </Table.Header> |
| 180 | <Table.Body> |
| 181 | {labels.map((label) => ( |
| 182 | <LabelRow key={label.id} label={label} /> |
| 183 | ))} |
| 184 | </Table.Body> |
| 185 | </Table> |
| 186 | </LayerCard> |
| 187 | )} |
| 188 | </div> |
| 189 | </AppShell> |
| 190 | ); |
| 191 | } |