"use client"; import { ArrowLeft, Plus } from "lucide-react"; import { useEffect, useState } from "react"; import { redirect, useRouter } from "next/navigation"; import { AddCardModal } from "./AddCardModal"; import { CardItem } from "./CardItem"; import { useTranslations } from "next-intl"; import { PageLayout } from "@/components/ui/PageLayout"; import { PrimaryButton, CircleButton, LinkButton } from "@/design-system/base/button"; import { CardList } from "@/components/ui/CardList"; import { actionGetCardsByDeckIdWithNotes, actionDeleteCard } from "@/modules/card/card-action"; import type { ActionOutputCardWithNote } from "@/modules/card/card-action-dto"; import { toast } from "sonner"; export function InDeck({ deckId, isReadOnly }: { deckId: number; isReadOnly: boolean; }) { const [cards, setCards] = useState([]); const [loading, setLoading] = useState(true); const [openAddModal, setAddModal] = useState(false); const router = useRouter(); const t = useTranslations("deck_id"); useEffect(() => { const fetchCards = async () => { setLoading(true); await actionGetCardsByDeckIdWithNotes({ deckId }) .then(result => { if (!result.success || !result.data) { throw new Error(result.message || "Failed to load cards"); } return result.data; }).then(setCards) .catch((error) => { toast.error(error instanceof Error ? error.message : "Unknown error"); }) .finally(() => { setLoading(false); }); }; fetchCards(); }, [deckId]); const refreshCards = async () => { await actionGetCardsByDeckIdWithNotes({ deckId }) .then(result => { if (!result.success || !result.data) { throw new Error(result.message || "Failed to refresh cards"); } return result.data; }).then(setCards) .catch((error) => { toast.error(error instanceof Error ? error.message : "Unknown error"); }); }; return (
{t("back")}

{t("cards")}

{t("itemsCount", { count: cards.length })}

{ redirect(`/memorize?deck_id=${deckId}`); }} > {t("memorize")} {!isReadOnly && ( { setAddModal(true); }} > )}
{loading ? (

{t("loadingCards")}

) : cards.length === 0 ? (

{t("noCards")}

) : (
{cards .toSorted((a, b) => Number(BigInt(a.id) - BigInt(b.id))) .map((card) => ( { actionDeleteCard({ cardId: BigInt(card.id) }) .then(result => { if (!result.success) throw new Error(result.message || "Delete failed"); }).then(refreshCards) .catch((error) => { toast.error(error instanceof Error ? error.message : "Unknown error"); }); }} refreshCards={refreshCards} /> ))}
)}
setAddModal(false)} deckId={deckId} onAdded={refreshCards} />
); };