"use client" import { useState } from "react" import Link from "next/link" import { useRouter } from "next/navigation" import { useTranslations } from "next-intl" import { Book, MoreVertical, Edit, Trash2, BookOpen, GraduationCap, Building2 } from "lucide-react" import { Card, CardContent, CardFooter, CardHeader, } from "@/shared/components/ui/card" import { Badge } from "@/shared/components/ui/badge" import { Button } from "@/shared/components/ui/button" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/shared/components/ui/dropdown-menu" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/shared/components/ui/alert-dialog" import { cn, formatDate } from "@/shared/lib/utils" import type { Textbook } from "../types" import { getSubjectColor, getSubjectLabelKey, getGradeLabelKey } from "../constants" import { deleteTextbookAction } from "../actions" import { toast } from "sonner" interface TextbookCardProps { textbook: Textbook hrefBase?: string hideActions?: boolean } export function TextbookCard({ textbook, hrefBase, hideActions }: TextbookCardProps) { const t = useTranslations("textbooks") const router = useRouter() const base = hrefBase || "/teacher/textbooks" const colorClass = getSubjectColor(textbook.subject) const [showDeleteDialog, setShowDeleteDialog] = useState(false) const [isDeleting, setIsDeleting] = useState(false) const handleDelete = async () => { setIsDeleting(true) try { const result = await deleteTextbookAction(textbook.id) if (result.success) { toast.success(result.message) router.refresh() } else { toast.error(result.message) } } catch (e) { console.error("Failed to delete textbook", e) toast.error(t("reader.deleteFailed")) } finally { setIsDeleting(false) setShowDeleteDialog(false) } } return (
{t(`subject.${getSubjectLabelKey(textbook.subject)}`)}
{textbook.grade ? t(`grade.${getGradeLabelKey(textbook.grade)}`) : t("card.gradeNA")}

{textbook.title}

{textbook.grade ? t(`grade.${getGradeLabelKey(textbook.grade)}`) : t("card.gradeNA")}
{/* 任意值 max-w-[120px]:出版社名称截断宽度,防止卡片布局错乱 */} {textbook.publisher || t("card.publisherNA")}
{textbook._count?.chapters || 0} {t("card.chapters")}
{t("card.updated")} {formatDate(textbook.updatedAt)} {!hideActions && ( {t("card.editContent")} setShowDeleteDialog(true)} > {t("card.delete")} )}
{t("dialog.settings.deleteConfirmTitle")} {t("dialog.settings.deleteConfirmDesc")} {t("dialog.knowledge.cancel")} { e.preventDefault() handleDelete() }} disabled={isDeleting} className="bg-destructive text-destructive-foreground hover:bg-destructive/90" > {isDeleting ? t("dialog.settings.processing") : t("dialog.settings.delete")}
) }