"use client"
import { useState } from "react"
import Link from "next/link"
import { BookOpen, BookMarked, Star, Trash2, CheckCircle2 } from "lucide-react"
import { Button } from "@/components/ui/button"
import { useAuth } from "@/lib/auth-context"
import { useBookmarks } from "@/lib/bookmark-context"
import { useRecommendations } from "@/lib/recommendation-context"
import { getNovelStatusBadgeClass } from "@/lib/novel-status"
import { cn } from "@/lib/utils"
type Tab = "dang-doc" | "danh-dau" | "da-doc" | "de-cu"
const TABS: { id: Tab; label: string; icon: React.ReactNode }[] = [
{ id: "dang-doc", label: "Đang đọc", icon: },
{ id: "danh-dau", label: "Đánh dấu", icon: },
{ id: "da-doc", label: "Đã đọc", icon: },
{ id: "de-cu", label: "Đề cử", icon: },
]
function NovelRow({
coverUrl,
title,
slug,
authorName,
status,
extra,
readLink,
readLabel,
onRemove,
removeLabel,
}: {
coverUrl?: string
title: string
slug: string
authorName: string
status: string
extra?: React.ReactNode
readLink: string
readLabel: string
onRemove: () => void
removeLabel: string
}) {
return (
{title}
{authorName}
{status}
{extra}
)
}
function EmptyState({ message }: { message: string }) {
return (
)
}
export default function BookshelfPage() {
const { user } = useAuth()
const { bookmarks, toggleBookmark } = useBookmarks()
const { recommendations, toggleRecommendation } = useRecommendations()
const [activeTab, setActiveTab] = useState("dang-doc")
if (!user) {
return (
Tủ Sách
Đăng nhập để xem danh sách truyện đã lưu
)
}
const withNovel = bookmarks.filter((b) => b.novel).map((b) => ({ novel: b.novel as any, bookmark: b }))
const dangDocList = withNovel.filter(({ bookmark, novel }) => bookmark.lastChapterNumber && bookmark.lastChapterNumber < (novel.totalChapters ?? Infinity))
const daDanhDauList = withNovel
const daDocList = withNovel.filter(({ bookmark, novel }) => bookmark.lastChapterNumber && bookmark.lastChapterNumber >= (novel.totalChapters ?? 0) && novel.totalChapters > 0)
const deCuList = recommendations.filter((r) => r.novel)
const counts: Record = {
"dang-doc": dangDocList.length,
"danh-dau": daDanhDauList.length,
"da-doc": daDocList.length,
"de-cu": deCuList.length,
}
return (
Tủ Sách
{/* Sidebar */}
{/* Mobile tab bar */}
{TABS.map((tab) => (
))}
{/* Content */}
{activeTab === "dang-doc" && (
dangDocList.length === 0 ? (
) : (
{dangDocList.map(({ novel, bookmark }) => (
Chương {bookmark.lastChapterNumber} / {novel.totalChapters}
}
onRemove={() => toggleBookmark(novel.id)}
removeLabel="Xóa khỏi tủ sách"
/>
))}
)
)}
{activeTab === "danh-dau" && (
daDanhDauList.length === 0 ? (
) : (
{daDanhDauList.map(({ novel, bookmark }) => {
const readLink = bookmark.lastChapterNumber
? `/truyen/${novel.slug}/${bookmark.lastChapterNumber}`
: `/truyen/${novel.slug}/1`
return (
Chương {bookmark.lastChapterNumber} / {novel.totalChapters}
) : undefined
}
onRemove={() => toggleBookmark(novel.id)}
removeLabel="Xóa khỏi tủ sách"
/>
)
})}
)
)}
{activeTab === "da-doc" && (
daDocList.length === 0 ? (
) : (
{daDocList.map(({ novel, bookmark }) => (
Đã đọc {novel.totalChapters} chương
}
onRemove={() => toggleBookmark(novel.id)}
removeLabel="Xóa khỏi tủ sách"
/>
))}
)
)}
{activeTab === "de-cu" && (
deCuList.length === 0 ? (
) : (
{deCuList.map((item) => (
void toggleRecommendation(item.novelId)}
removeLabel="Bỏ đề cử"
/>
))}
)
)}
)
}