import { useState, type FormEvent } from "react"; import { NavLink, Navigate, useParams } from "react-router-dom"; import { useTranslation } from "react-i18next"; import type { components } from "../api/schema"; import { useAuthorities, useCreateAuthority } from "../api/queries"; import { LabelEditor } from "../components/label-editor"; import { Button } from "@/components/ui/button"; import { labelText } from "../lib/labels"; type LabelInput = components["schemas"]["LabelInput"]; const KINDS = ["person", "organisation", "place"] as const; export function AuthoritiesPage() { const { t, i18n } = useTranslation(); const { kind } = useParams(); const lang = i18n.language.startsWith("sv") ? "sv" : "en"; const isValidKind = (KINDS as readonly string[]).includes(kind ?? ""); const { data: authorities } = useAuthorities(isValidKind ? (kind as string) : "person"); const create = useCreateAuthority(); const [labels, setLabels] = useState([]); const [error, setError] = useState(false); if (!isValidKind) return ; const onCreate = (event: FormEvent) => { event.preventDefault(); if (!labels.some((l) => l.lang === "en" && l.label)) { setError(true); return; } setError(false); create.mutate( { kind: kind as string, external_uri: null, labels }, { onSuccess: () => setLabels([]) }, ); }; return (
{KINDS.map((k) => ( `rounded px-3 py-1 text-sm ${isActive ? "bg-neutral-800 text-white" : "border"}` } > {t(`authorities.${k}`)} ))}
    {authorities?.length === 0 && (
  • {t("authorities.empty")}
  • )} {authorities?.map((a) => (
  • {labelText(a.labels, lang)}
  • ))}
{t("authorities.new")} ยท {t(`authorities.${kind}`)}
{error && (

{t("form.required")}

)} {create.isError && (

{t("form.rejected")}

)}
); }