import { useCallback, useEffect, useState } from 'react' import { Link, useNavigate } from 'react-router-dom' import { Clock, Film, Loader2, Star, Tv } from 'lucide-react' import { ARTWORK, imageURL } from '../api/client' import { historyAPI } from '../api/history' import type { HistoryStats } from '../types/history' import { PageHeader } from '../components/PageHeader' import { usePermission } from '../hooks/usePermission' // WatchStatsPage 展示个人观看统计。 // // 只消费已有数据(PlaybackHistory 聚合),因此这里没有任何写操作;图表也用 // 纯 CSS 柱状呈现,不引入图表库 —— 30 根柱子的信息量不值得多一个依赖。 export function WatchStatsPage() { const navigate = useNavigate() const canViewHistory = usePermission('can_view_history') const [stats, setStats] = useState(null) const [loading, setLoading] = useState(true) const [failed, setFailed] = useState(false) // Redirect denied users before issuing any API calls. useEffect(() => { if (!canViewHistory) { navigate('/', { replace: true }) } }, [canViewHistory, navigate]) const load = useCallback(async () => { try { const data = await historyAPI.stats() setStats(data) setFailed(false) } catch { setFailed(true) } finally { setLoading(false) } }, []) useEffect(() => { void load() }, [load]) if (loading) { return (
) } if (failed || !stats) { return (

统计加载失败,请稍后重试。

) } const daily = stats.daily ?? [] const byType = stats.by_library_type ?? [] const recent = stats.recent ?? [] const hasAnyHistory = stats.total > 0 return (
{!hasAnyHistory ? (

还没有观看记录。播放任意内容后,这里会展示观看时长与类型分布。

) : ( <>
} label="累计观看" value={formatHours(stats.watched_hours)} hint={`共 ${stats.total} 条播放记录`} /> } label="已看完" value={String(stats.completed)} hint={ stats.last_watched ? `最近观看 ${formatDate(stats.last_watched)}` : '尚无观看时间' } /> } label="正在看" value={String(stats.in_progress ?? 0)} hint="未标记看完的条目" />

近 30 天观看时长

{daily.length > 0 ? `${daily.length} 天有记录` : '暂无记录'}
{daily.length === 0 ? (

近 30 天没有观看记录

) : ( )}

类型分布

{byType.length === 0 ? (

暂无数据

) : (
    {byType.map((row) => { const max = Math.max(...byType.map((item) => item.watch_ms), 1) const ratio = Math.max(2, Math.round((row.watch_ms / max) * 100)) return (
  • {typeIcon(row.type)} {typeLabel(row.type)} {formatHours(row.watch_ms / 1000 / 3600)} · {row.count} 条
  • ) })}
)}
{recent.length > 0 && (

最近看过

    {recent.map((row) => (
  • {row.media?.poster_url && ( )} {row.media?.title ?? '未知媒体'} {formatDate(row.history.watched_at)} {row.history.completed ? ' · 已看完' : ' · 在看'}
  • ))}
)} )}
) } function StatCard({ icon, label, value, hint, }: { icon: React.ReactNode label: string value: string hint: string }) { return (
{icon} {label}
{value}
{hint}
) } // DailyBars 用纯 CSS 画 30 天柱状图。柱高按区间内最大值归一化,最小值留 4% // 高度,否则「有记录但很少」的那天在视觉上会消失。 function DailyBars({ daily, }: { daily: Array<{ day: string; watch_ms: number; plays: number }> }) { const max = Math.max(...daily.map((item) => item.watch_ms), 1) return (
{daily.map((item) => { const ratio = Math.max(4, Math.round((item.watch_ms / max) * 100)) return (
) })}
) } function formatHours(hours: number): string { if (!Number.isFinite(hours) || hours <= 0) return '0 小时' if (hours < 1) { const minutes = Math.round(hours * 60) return `${minutes} 分钟` } return `${hours.toFixed(hours >= 10 ? 0 : 1)} 小时` } function formatDate(value: string): string { const date = new Date(value) if (Number.isNaN(date.getTime())) return '' return date.toLocaleDateString() } function typeLabel(type: string): string { const labels: Record = { movie: '电影', tv: '剧集', anime: '动漫', variety: '综艺', music: '音乐', adult: 'Adult', } return labels[type] ?? '其他' } function typeIcon(type: string) { if (type === 'tv' || type === 'anime' || type === 'variety') { return } return }