import { useState, type FormEvent } from "react"; import { useParams } from "react-router-dom"; import { useTranslation } from "react-i18next"; import type { components } from "../api/schema"; import { useTerms, useAddTerm } from "../api/queries"; import { LabelEditor } from "../components/label-editor"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; type LabelInput = components["schemas"]["LabelInput"]; type LabelView = components["schemas"]["LabelView"]; function labelText(labels: LabelView[], lang: string): string { return ( labels.find((l) => l.lang === lang)?.label ?? labels.find((l) => l.lang === "en")?.label ?? labels[0]?.label ?? "" ); } export function VocabularyTerms() { const { t, i18n } = useTranslation(); const { id } = useParams(); const lang = i18n.language.startsWith("sv") ? "sv" : "en"; const { data: terms } = useTerms(id); const addTerm = useAddTerm(); const [labels, setLabels] = useState([]); const [uri, setUri] = useState(""); const [error, setError] = useState(false); if (!id) return null; const onAdd = (event: FormEvent) => { event.preventDefault(); if (!labels.some((l) => l.lang === "en" && l.label)) { setError(true); return; } setError(false); addTerm.mutate( { vocabularyId: id, external_uri: uri.trim() || null, labels }, { onSuccess: () => { setLabels([]); setUri(""); } }, ); }; return (

{t("vocab.terms")}

{t("vocab.addTerm")}
setUri(e.target.value)} />
{error && (

{t("form.required")}

)} {addTerm.isError && (

{t("form.rejected")}

)}
); }