"use client"
import { useMemo, useState } from "react"
import Link from "next/link"
import { ChevronLeft, ChevronRight, Star } from "lucide-react"
import { Button } from "@/components/ui/button"
type RecommendationNovel = {
id: string
slug: string
title: string
authorName: string
coverUrl: string | null
rating: number
}
type TopRecommendationItem = {
novel: RecommendationNovel
recommendCount: number
}
type EditorRecommendationItem = {
novel: RecommendationNovel
editorName: string
recommendCount: number
}
interface HomeRecommendationBoardsProps {
topItems: TopRecommendationItem[]
editorItems: EditorRecommendationItem[]
pageSize?: number
}
function BoardHeader({
title,
page,
totalPages,
onPrev,
onNext,
}: {
title: string
page: number
totalPages: number
onPrev: () => void
onNext: () => void
}) {
return (
{title}
)
}
export function HomeRecommendationBoards({ topItems, editorItems, pageSize = 5 }: HomeRecommendationBoardsProps) {
const [topPage, setTopPage] = useState(0)
const [editorPage, setEditorPage] = useState(0)
const sortedTopItems = useMemo(() => {
return [...topItems].sort((a, b) => b.recommendCount - a.recommendCount)
}, [topItems])
const sortedEditorItems = useMemo(() => {
return [...editorItems].sort((a, b) => {
if (b.recommendCount !== a.recommendCount) return b.recommendCount - a.recommendCount
return a.editorName.localeCompare(b.editorName, "vi")
})
}, [editorItems])
const totalTopPages = Math.max(1, Math.ceil(sortedTopItems.length / pageSize))
const totalEditorPages = Math.max(1, Math.ceil(sortedEditorItems.length / pageSize))
const topPageStart = topPage * pageSize
const editorPageStart = editorPage * pageSize
const visibleTopItems = sortedTopItems.slice(topPageStart, topPageStart + pageSize)
const visibleEditorItems = sortedEditorItems.slice(editorPageStart, editorPageStart + pageSize)
return (
setTopPage((prev) => Math.max(0, prev - 1))}
onNext={() => setTopPage((prev) => Math.min(totalTopPages - 1, prev + 1))}
/>
{visibleTopItems.length > 0 ? visibleTopItems.map((item, index) => (
{topPageStart + index + 1}
{item.novel.title}
{item.novel.authorName}
{item.recommendCount} đề cử
{item.novel.rating.toFixed(1)}
)) : (
Chưa có truyện đề cử.
)}
setEditorPage((prev) => Math.max(0, prev - 1))}
onNext={() => setEditorPage((prev) => Math.min(totalEditorPages - 1, prev + 1))}
/>
{visibleEditorItems.length > 0 ? visibleEditorItems.map((item, index) => (
{editorPageStart + index + 1}
{item.novel.title}
{item.novel.authorName}
Biên tập viên: {item.editorName}
{item.recommendCount} đề cử
{item.novel.rating.toFixed(1)}
)) : (
Chưa có đề cử từ biên tập viên.
)}
)
}