import { useCallback, useEffect, useMemo, useState } from "react"; import { Link } from "react-router-dom"; import { Loader2, RefreshCw, UserRound } from "lucide-react"; import { Shell } from "@/components/Shell"; import { SessionDetailModal, type SessionDetail } from "@/components/SessionDetailModal"; import { RoomQuickActions } from "@/components/RoomQuickActions"; import { AnimatedNumber, EmptyState, LivePulse } from "@/components/ui"; import { fetchJson, fmtDuration, fmtTs, sessionCoverSrc, todayBeijingISO } from "@/lib/api"; import { useAutoRefresh, type RefreshOpts } from "@/lib/realtime"; type AnchorRow = { anchor_name: string; room_id?: string; sessions: number; hours_total: number; last_started_at?: number; active?: number; }; type Profile = { anchor_name: string; room_id?: string; overview: { sessions: number; total_hours: number; avg_duration_seconds: number; active: number; last_started_at?: number; }; schedule?: { peak_hour?: number | null; peak_weekday_label?: string | null; peak_slot?: number | null; peak_slot_label?: string; peak_slot_range?: string; slot_counts?: number[]; avg_gap_hours?: number | null; sessions_per_active_day?: number | null; typical_open_hint?: string; next_open_hint?: { eta_ts: number; label: string; based_on: string } | null; }; sessions: Array<{ id: string; started_at?: number; ended_at?: number | null; duration_seconds?: number; cover_url?: string; cover_proxy_url?: string; title?: string; status?: string; source_url?: string; room_id?: string; anchor_name?: string; }>; rooms: Array<{ id: string; url: string; room_id: string; last_status?: string; enabled?: boolean }>; }; const SLOT_LABELS = ["凌晨", "清晨", "上午", "午后", "傍晚", "夜间"]; export default function AnchorsPage() { const [anchors, setAnchors] = useState([]); const [selected, setSelected] = useState(""); const [profile, setProfile] = useState(null); const [q, setQ] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [detail, setDetail] = useState(null); const [bootstrapped, setBootstrapped] = useState(false); useEffect(() => { if (typeof window === "undefined") return; const p = new URLSearchParams(window.location.search); const query = p.get("q") || p.get("name") || ""; if (query) setQ(query); setBootstrapped(true); }, []); const loadList = useCallback(async (opts?: RefreshOpts) => { const silent = Boolean(opts?.silent); if (!silent) { setLoading(true); setError(""); } try { const data = await fetchJson("/api/anchors?limit=80"); const list = (data.anchors || []) as AnchorRow[]; setAnchors(list); } catch (e) { if (!silent) setError((e as Error).message); } finally { if (!silent) setLoading(false); } }, []); const loadProfile = useCallback(async (key: string) => { if (!key) return; try { const data = await fetchJson(`/api/anchors/${encodeURIComponent(key)}`); setProfile(data); } catch (e) { setError((e as Error).message); } }, []); const refreshAll = useCallback( async (opts?: RefreshOpts) => { await loadList(opts); if (selected) void loadProfile(selected); }, [loadList, loadProfile, selected], ); useAutoRefresh(refreshAll, { intervalMs: 30000 }); const filtered = useMemo(() => { const needle = q.trim().toLowerCase(); if (!needle) return anchors; return anchors.filter((a) => `${a.anchor_name} ${a.room_id || ""}`.toLowerCase().includes(needle), ); }, [anchors, q]); useEffect(() => { if (!bootstrapped || !anchors.length) return; if (selected) return; const needle = q.trim().toLowerCase(); if (needle) { const hit = anchors.find((a) => `${a.anchor_name} ${a.room_id || ""}`.toLowerCase().includes(needle), ); if (hit) { setSelected(hit.anchor_name); return; } } setSelected(anchors[0].anchor_name); }, [bootstrapped, anchors, q, selected]); useEffect(() => { if (selected) void loadProfile(selected); }, [selected, loadProfile]); async function openSession(s: Profile["sessions"][number]) { try { const data = await fetchJson(`/api/sessions/${s.id}`); setDetail((data.session || { ...s, anchor_name: profile?.anchor_name }) as SessionDetail); } catch { setDetail({ id: s.id, anchor_name: profile?.anchor_name || s.anchor_name, room_id: s.room_id || profile?.room_id, title: s.title, started_at: s.started_at, ended_at: s.ended_at, duration_seconds: s.duration_seconds, cover_url: s.cover_url, cover_proxy_url: s.cover_proxy_url, status: s.status, source_url: s.source_url, }); } } return (

👤 主播画像

按主播汇总场次、时长,并展示开播作息规律与预计下次开播。

{error ?
{error}
: null} {!anchors.length && !loading ? ( ) : (
{profile ? ( <>

{profile.anchor_name}

{profile.overview.active ? : null}

{profile.room_id || "—"}

setError(m)} /> {profile.rooms?.[0]?.url ? ( 打开直播间 ) : null}
场次
累计小时
平均场长
{fmtDuration(profile.overview.avg_duration_seconds)}
最近开播
{fmtTs(profile.overview.last_started_at)}
{profile.schedule ? (

开播作息规律

去时段页看全局 →

{profile.schedule.typical_open_hint || "样本不足"}

常开时段
{profile.schedule.peak_slot_label || "—"} {profile.schedule.peak_slot_range ? ( ({profile.schedule.peak_slot_range}) ) : null}
平均开播间隔
{profile.schedule.avg_gap_hours != null ? `${profile.schedule.avg_gap_hours} 小时` : "—"}
预计下次
{profile.schedule.next_open_hint?.label || "—"}
{profile.schedule.next_open_hint?.based_on ? (
{profile.schedule.next_open_hint.based_on}
) : null}
{profile.schedule.slot_counts?.length === 6 ? (
{profile.schedule.slot_counts.map((n, i) => { const max = Math.max(1, ...profile.schedule!.slot_counts!); return (
{SLOT_LABELS[i]}
{n}
); })}
) : null}
) : null}

最近场次

{profile.sessions.map((s) => (
setError(m)} />
))}
也可在 场次日志 中按主播名筛选。
) : (
选择左侧主播查看画像
)}
)} {detail ? ( setDetail(null)} onUpdated={() => selected && void loadProfile(selected)} /> ) : null}
); }