import type { LabelCreation } from "@refs/design-system"; import { useFetcher, useRouteLoaderData } from "react-router"; import type { loader as rootLoader } from "../root"; /** * Lets a label picker create a label in place. The labels page owns the action; the page that shows the picker * reloads its labels when the action ends. It is undefined for people who may not manage labels. */ export function useLabelCreation(): LabelCreation | undefined { const allowed = useRouteLoaderData("root")?.can.manageLabels ?? false; const fetcher = useFetcher<{ error: string | null }>(); if (!allowed) return undefined; return { create: (name, color) => fetcher.submit({ intent: "create", name, color, description: "" }, { method: "post", action: "/tasks/labels" }), pending: fetcher.state !== "idle", error: fetcher.data?.error ?? undefined, }; }