Files
fancy/fancy-frontend/components/collectionsListDialog.tsx
T
Ivan Rezchikov 628dd6384b
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
version 1
2026-09-18 00:45:51 +03:00

188 lines
6.2 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { useTranslations } from "next-intl";
import { Button } from "@/components/ui/button";
import { ChevronRightIcon, PlusIcon, XIcon } from "lucide-react";
import { Dialog, DialogContent, DialogFooter, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Skeleton } from "@/components/ui/skeleton";
import { CollectionResult, Language } from "@/types/api";
import { addWordsToCollection, deleteWordFromCollection, getCollections } from "@/lib/api";
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
import { Item, ItemActions, ItemContent, ItemMedia, ItemTitle } from "./ui/item";
interface CollectionsListDialogProps {
lang: Language;
word: string;
selectedCollections: CollectionResult[];
onOptimisticToggle: (collection: CollectionResult, adding: boolean) => () => void;
}
type AllCollectionsState =
| { status: "loading" }
| { status: "error"; error: unknown }
| { status: "done"; collections: CollectionResult[] };
export function CollectionsListDialog({
lang,
word,
selectedCollections,
onOptimisticToggle,
}: CollectionsListDialogProps) {
const [open, setOpen] = useState(false);
const [state, setState] = useState<AllCollectionsState>({ status: "loading" });
const [rowErrors, setRowErrors] = useState<Record<string, string>>({});
const t = useTranslations("CollectionsList");
const tc = useTranslations("Common");
const tApi = useTranslations("ApiErrors");
const handleOpenChange = (next: boolean) => {
setOpen(next);
if (next) setState({ status: "loading" });
};
useEffect(() => {
if (!open) return;
let cancelled = false;
getCollections()
.then((all) => {
if (cancelled) return;
setState({
status: "done",
collections: all.filter((c) => c.lang === lang && c.type === "user"),
});
})
.catch((error) => {
if (!cancelled) setState({ status: "error", error });
});
return () => {
cancelled = true;
};
}, [open, lang]);
const selectedIds = new Set(selectedCollections.map((c) => c.id));
const toggle = (collection: CollectionResult) => {
const alreadyIn = selectedIds.has(collection.id);
setRowErrors((prev) => ({ ...prev, [collection.id]: "" }));
const rollback = onOptimisticToggle(collection, !alreadyIn);
const request = alreadyIn
? deleteWordFromCollection(collection.id, word)
: addWordsToCollection(collection.id, [word]);
request.catch((error) => {
rollback();
setRowErrors((prev) => ({
...prev,
[collection.id]: resolveApiErrorMessage(error, (kind) => tApi(kind), t("toggleFailed")),
}));
});
};
const errorMessage =
state.status === "error"
? resolveApiErrorMessage(state.error, (kind) => tApi(kind), t("loadFailed"))
: null;
return (
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogTrigger>
<Item variant="muted" size="sm" className="cursor-pointer">
<ItemMedia>
<PlusIcon className="size-5" />
</ItemMedia>
<ItemContent>
<ItemTitle>{t("trigger")}</ItemTitle>
</ItemContent>
<ItemActions>
<ChevronRightIcon className="size-4" />
</ItemActions>
</Item>
</DialogTrigger>
<DialogContent>
<DialogTitle>{t("dialogTitle", { word })}</DialogTitle>
{state.status === "loading" && (
<div className="flex flex-col gap-2">
<Item variant="muted">
<ItemContent className="flex flex-row items-center justify-between">
<Skeleton className="w-1/2 h-4" />
<Skeleton className="w-1/4 h-8" />
</ItemContent>
</Item>
<Item variant="muted">
<ItemContent className="flex flex-row items-center justify-between">
<Skeleton className="w-1/2 h-4" />
<Skeleton className="w-1/4 h-8" />
</ItemContent>
</Item>
<Item variant="muted">
<ItemContent className="flex flex-row items-center justify-between">
<Skeleton className="w-1/2 h-4" />
<Skeleton className="w-1/4 h-8" />
</ItemContent>
</Item>
</div>
)}
{state.status === "error" && (
<Alert variant="destructive">
<AlertDescription>{errorMessage}</AlertDescription>
</Alert>
)}
{state.status === "done" && (
<div className="flex flex-col gap-2">
{state.collections.length === 0 && (
<p className="text-muted-foreground text-sm">{t("empty")}</p>
)}
{state.collections.map((collection) => {
const checked = selectedIds.has(collection.id);
const rowError = rowErrors[collection.id];
return (
<div key={collection.id} className="flex flex-col gap-2">
<Item variant="muted">
<ItemContent>
<ItemTitle>{collection.name}</ItemTitle>
</ItemContent>
<ItemActions>
<Button
variant={checked ? "outline" : "default"}
size="sm"
onClick={() => toggle(collection)}
>
{checked ? (
<>
<XIcon className="size-4" /> {t("removeAction")}
</>
) : (
<>
<PlusIcon className="size-4" /> {t("addAction")}
</>
)}
</Button>
</ItemActions>
</Item>
{rowError && <h5 className="text-muted-foreground">{rowError}</h5>}
</div>
);
})}
</div>
)}
<DialogFooter>
<Button variant="outline" onClick={() => setOpen(false)}>
{tc("close")}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}