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
114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { useTranslations } from "next-intl";
|
|
import { getCollectionsByWord } from "@/lib/api";
|
|
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
|
|
import { CollectionResult, Fetched, Language } from "@/types/api";
|
|
import { Item, ItemActions, ItemContent } from "./ui/item";
|
|
import { Skeleton } from "./ui/skeleton";
|
|
import { CollectionsListDialog } from "./collectionsListDialog";
|
|
|
|
interface CollectionsListProps {
|
|
word: string;
|
|
lang: Language;
|
|
onResult?: (result: Fetched<CollectionResult[]>) => void;
|
|
}
|
|
|
|
type ListState =
|
|
| { status: "loading" }
|
|
| { status: "error"; error: unknown }
|
|
| { status: "done"; collections: CollectionResult[] };
|
|
|
|
export function CollectionsList({ word, lang, onResult }: CollectionsListProps) {
|
|
const [byKey, setByKey] = useState<Record<string, ListState>>({});
|
|
const key = `${lang}:${word}`;
|
|
const state = byKey[key] ?? { status: "loading" };
|
|
|
|
const onResultRef = useRef(onResult);
|
|
useEffect(() => {
|
|
onResultRef.current = onResult;
|
|
});
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
|
|
getCollectionsByWord(lang, word)
|
|
.then((data) => {
|
|
if (cancelled) return;
|
|
setByKey((prev) => ({ ...prev, [key]: { status: "done", collections: data } }));
|
|
onResultRef.current?.(
|
|
Object.assign(data, { fetched_at: new Date().toISOString() })
|
|
);
|
|
})
|
|
.catch((error) => {
|
|
if (cancelled) return;
|
|
setByKey((prev) => ({ ...prev, [key]: { status: "error", error } }));
|
|
});
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [lang, word, key]);
|
|
|
|
const t = useTranslations("CollectionsList");
|
|
const tApi = useTranslations("ApiErrors");
|
|
|
|
const errorMessage =
|
|
state.status === "error"
|
|
? resolveApiErrorMessage(
|
|
state.error,
|
|
(kind) => (kind === "not-found" ? t("notFound") : tApi(kind)),
|
|
t("notFound")
|
|
)
|
|
: null;
|
|
|
|
const applyOptimistic = (collection: CollectionResult, adding: boolean) => {
|
|
if (state.status !== "done") return () => {};
|
|
const previous = state.collections;
|
|
|
|
setByKey((prev) => ({
|
|
...prev,
|
|
[key]: {
|
|
status: "done",
|
|
collections: adding
|
|
? [...previous, collection]
|
|
: previous.filter((c) => c.id !== collection.id),
|
|
},
|
|
}));
|
|
|
|
return () =>
|
|
setByKey((prev) => ({ ...prev, [key]: { status: "done", collections: previous } }));
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col gap-4">
|
|
{state.status === "loading" && (
|
|
<Item variant="outline" className="w-full">
|
|
<ItemContent>
|
|
<Skeleton className="w-4/5 h-4" />
|
|
<Skeleton className="w-full h-4" />
|
|
</ItemContent>
|
|
<ItemActions>
|
|
<Skeleton className="rounded-lg w-24 h-8" />
|
|
</ItemActions>
|
|
</Item>
|
|
)}
|
|
|
|
{state.status === "error" && (
|
|
<p className="text-muted-foreground">{errorMessage}</p>
|
|
)}
|
|
|
|
{state.status === "done" && (
|
|
<div className="flex flex-col md:w-1/5 gap-2">
|
|
<CollectionsListDialog
|
|
lang={lang}
|
|
word={word}
|
|
selectedCollections={state.collections}
|
|
onOptimisticToggle={applyOptimistic}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
} |