"use client" import { useEffect, useMemo, useState } from "react" import Link from "next/link" import { ChevronLeft, ChevronRight, Flame, Star } from "lucide-react" import { getNovelStatusBadgeClass } from "@/lib/novel-status" import { formatViews } from "@/lib/utils" export type HotCarouselItem = { id: string slug: string title: string authorName: string description: string coverUrl: string | null totalChapters: number rating: number views: number status: string hotSource: "week" | "month" | "all" } function sourceLabel(source: HotCarouselItem["hotSource"]) { if (source === "week") return "Top tuần" if (source === "month") return "Top tháng" return "Top tổng" } function sourceClass(source: HotCarouselItem["hotSource"]) { if (source === "week") return "border-emerald-400/30 bg-emerald-500/20 text-emerald-300" if (source === "month") return "border-orange-400/30 bg-orange-500/20 text-orange-300" return "border-primary/30 bg-primary/20 text-primary" } function compactLine(text: string, max = 180) { const normalized = text.replace(/\s+/g, " ").trim() if (normalized.length <= max) return normalized return `${normalized.slice(0, max).trim()}...` } export function HomeHotCarousel({ items }: { items: HotCarouselItem[] }) { const [activeIndex, setActiveIndex] = useState(0) const total = items.length useEffect(() => { if (total <= 1) return const timer = window.setInterval(() => { setActiveIndex((current) => (current + 1) % total) }, 6500) return () => { window.clearInterval(timer) } }, [total]) useEffect(() => { if (activeIndex >= total) { setActiveIndex(0) } }, [activeIndex, total]) const current = useMemo(() => items[activeIndex], [items, activeIndex]) if (!current) return null return (
{items.map((item, index) => (
{item.title}
{sourceLabel(item.hotSource)}

{item.title}

Tác giả: {item.authorName}

{compactLine(item.description)}

{item.totalChapters} chương {formatViews(item.views)} lượt đọc {item.rating.toFixed(1)} {item.status}
))}
{total > 1 && ( <> )}
{total > 1 && (
{items.map((item, index) => (
)}
) }