"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { useTranslations } from "next-intl"; import { Button } from "@/components/ui/button"; import { InfoIcon, MenuIcon } from "lucide-react"; import { Dialog, DialogClose, DialogContent, DialogFooter, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Field, FieldGroup, FieldLabel, FieldSet, } from "@/components/ui/field"; import { CollectionResult } from "@/types/api"; import { formatDate } from "@/lib/formatDate"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { deleteCollection, updateCollection } from "@/lib/api"; import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage"; import { Spinner } from "@/components/ui/spinner"; export function MenuDialog({ collection }: { collection: CollectionResult }) { const router = useRouter(); const [open, setOpen] = useState(false); const [name, setName] = useState(collection.name); const [error, setError] = useState(null); const [isDeleting, startDelete] = useTransition(); const [isUpdating, startUpdate] = useTransition(); const tt = useTranslations("Time"); const t = useTranslations("CollectionMenu"); const tc = useTranslations("Common"); const tl = useTranslations("Languages"); const tApi = useTranslations("ApiErrors"); const isAuto = collection.type === "auto"; const isBusy = isDeleting || isUpdating; const handleDelete = () => { setError(null); startDelete(async () => { try { await deleteCollection(collection.id); router.push("/collections"); } catch (err) { setError(resolveApiErrorMessage(err, (kind) => tApi(kind), t("deleteFailed"))); } }); }; const handleUpdate = () => { setError(null); startUpdate(async () => { try { await updateCollection(collection.id, name); router.refresh(); setOpen(false); } catch (err) { setError(resolveApiErrorMessage(err, (kind) => tApi(kind), t("updateFailed"))); } }); }; return ( } /> {t("title")} {isAuto && ( {t("autoTitle")} {t("autoDescription")} )} {error && ( {error} )}
{t("nameLabel")} setName(e.target.value)} disabled={isAuto || isBusy} />
{t("language")}
{tl(collection.lang)}
{t("wordCount")}
{collection.words.length}
{t("lastUpdate")}
{formatDate(collection.updated_at, tt)}
{t("created")}
{formatDate(collection.created_at, tt)}
} > {tc("cancel")}
); }