18 lines · 885 BRaw
| 1 | import type { LabelCreation } from "@refs/design-system"; |
| 2 | import { useFetcher, useRouteLoaderData } from "react-router"; |
| 3 | import type { loader as rootLoader } from "../root"; |
| 4 | |
| 5 | /** |
| 6 | * Lets a label picker create a label in place. The labels page owns the action; the page that shows the picker |
| 7 | * reloads its labels when the action ends. It is undefined for people who may not manage labels. |
| 8 | */ |
| 9 | export function useLabelCreation(): LabelCreation | undefined { |
| 10 | const allowed = useRouteLoaderData<typeof rootLoader>("root")?.can.manageLabels ?? false; |
| 11 | const fetcher = useFetcher<{ error: string | null }>(); |
| 12 | if (!allowed) return undefined; |
| 13 | return { |
| 14 | create: (name, color) => fetcher.submit({ intent: "create", name, color, description: "" }, { method: "post", action: "/tasks/labels" }), |
| 15 | pending: fetcher.state !== "idle", |
| 16 | error: fetcher.data?.error ?? undefined, |
| 17 | }; |
| 18 | } |