import { useState, type FormEvent } from "react"; import { NavLink } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { useVocabularies, useCreateVocabulary, useRenameVocabulary, useDeleteVocabulary } from "../api/queries"; import { useLang } from "../lib/use-lang"; import { rowStateClass } from "../lib/class-recipes"; import { byKey } from "../lib/sort"; import { DeleteConfirmDialog } from "../components/delete-confirm-dialog"; import { MutationError } from "../components/mutation-error"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { ListSkeleton } from "@/components/ui/skeletons"; export function VocabularyList() { const { t } = useTranslation(); const lang = useLang(); const { data, isLoading, isError } = useVocabularies(); const create = useCreateVocabulary(); const renameVocabulary = useRenameVocabulary(); const deleteVocabulary = useDeleteVocabulary(); const [key, setKey] = useState(""); const [filter, setFilter] = useState(""); const [editingId, setEditingId] = useState(null); const [draftKey, setDraftKey] = useState(""); const onCreate = (event: FormEvent) => { event.preventDefault(); if (!key.trim()) return; create.mutate({ key: key.trim() }, { onSuccess: () => setKey("") }); }; const q = filter.trim().toLowerCase(); const rows = [...(data ?? [])] .filter((v) => !q || v.key.toLowerCase().includes(q)) .sort(byKey(lang)); return (
{t("vocab.newVocabulary")}
setKey(e.target.value)} />
setFilter(e.target.value)} />
{isLoading ? ( ) : (
    {isError && (
  • {t("vocab.loadError")}
  • )} {data?.length === 0 && (
  • {t("vocab.empty")}
  • )} {data && data.length > 0 && rows.length === 0 && (
  • {t("common.noMatches")}
  • )} {rows.map((v) => (
  • {editingId === v.id ? (
    { e.preventDefault(); if (!draftKey.trim()) return; renameVocabulary.mutate( { id: v.id, key: draftKey.trim() }, { onSuccess: () => setEditingId(null) }, ); }} > setDraftKey(e.target.value)} /> ) : ( <> `block flex-1 px-3 py-2 text-sm ${rowStateClass(isActive)}` } > {v.key} deleteVocabulary.mutateAsync(v.id)} /> )}
  • ))}
)}
); }