Add shorts_yt_quota module, Shorts UI quota banner, overview tweaks (hide upcoming opens, align events panel height), Electron desktop client scaffold, and deploy scripts; update README. Co-authored-by: Cursor <cursoragent@cursor.com>
387 lines
17 KiB
TypeScript
387 lines
17 KiB
TypeScript
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<AnchorRow[]>([]);
|
||
const [selected, setSelected] = useState<string>("");
|
||
const [profile, setProfile] = useState<Profile | null>(null);
|
||
const [q, setQ] = useState("");
|
||
const [loading, setLoading] = useState(false);
|
||
const [error, setError] = useState("");
|
||
const [detail, setDetail] = useState<SessionDetail | null>(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 (
|
||
<Shell>
|
||
<div className="mb-4 flex flex-wrap items-start justify-between gap-3">
|
||
<div>
|
||
<h2 className="font-display text-2xl font-semibold">👤 主播画像</h2>
|
||
<p className="text-sm text-slate-400">按主播汇总场次、时长,并展示开播作息规律与预计下次开播。</p>
|
||
</div>
|
||
<button type="button" onClick={() => void loadList()} className="btn-glow inline-flex items-center gap-2 rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm">
|
||
{loading ? <Loader2 className="h-4 w-4 animate-spin" /> : <RefreshCw className="h-4 w-4" />}
|
||
刷新
|
||
</button>
|
||
</div>
|
||
|
||
{error ? <div className="mb-4 rounded-xl border border-rose-500/30 bg-rose-500/10 px-4 py-3 text-sm">{error}</div> : null}
|
||
|
||
{!anchors.length && !loading ? (
|
||
<EmptyState emoji="🎤" title="还没有主播数据" desc="有开播场次后,这里会自动生成主播排行与画像。" />
|
||
) : (
|
||
<div className="grid gap-4 lg:grid-cols-[20rem_1fr]">
|
||
<aside className="glass-panel max-h-[70vh] space-y-2 overflow-auto rounded-2xl p-3">
|
||
<input
|
||
value={q}
|
||
onChange={(e) => {
|
||
setQ(e.target.value);
|
||
setSelected("");
|
||
}}
|
||
placeholder="搜索主播 / 房间号…"
|
||
className="mb-2 w-full rounded-lg border border-white/10 bg-slate-900 px-3 py-2 text-sm"
|
||
/>
|
||
{filtered.map((a) => (
|
||
<button
|
||
key={`${a.anchor_name}-${a.room_id}`}
|
||
type="button"
|
||
onClick={() => setSelected(a.anchor_name)}
|
||
className={`hover-lift w-full rounded-xl border px-3 py-2 text-left text-sm ${
|
||
selected === a.anchor_name ? "border-teal-400/40 bg-teal-500/10" : "border-white/10 bg-black/20"
|
||
}`}
|
||
>
|
||
<div className="flex items-center justify-between gap-2">
|
||
<span className="truncate font-medium text-white">{a.anchor_name}</span>
|
||
{a.active ? <LivePulse label="LIVE" /> : null}
|
||
</div>
|
||
<div className="mt-1 text-[11px] text-slate-500">
|
||
{a.sessions} 场 · {Number(a.hours_total || 0).toFixed(1)}h
|
||
</div>
|
||
</button>
|
||
))}
|
||
{!filtered.length ? (
|
||
<div className="px-2 py-6 text-center text-xs text-slate-500">无匹配主播</div>
|
||
) : null}
|
||
</aside>
|
||
|
||
<section className="glass-panel rounded-2xl p-4">
|
||
{profile ? (
|
||
<>
|
||
<div className="mb-4 flex flex-wrap items-start justify-between gap-3">
|
||
<div>
|
||
<div className="flex items-center gap-2">
|
||
<UserRound className="h-5 w-5 text-teal-300" />
|
||
<h3 className="font-display text-xl font-semibold">{profile.anchor_name}</h3>
|
||
{profile.overview.active ? <LivePulse /> : null}
|
||
</div>
|
||
<p className="mt-1 font-mono text-xs text-cyan-300/80">{profile.room_id || "—"}</p>
|
||
</div>
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<RoomQuickActions
|
||
room={{
|
||
room_pk: profile.rooms?.[0]?.id,
|
||
room_id: profile.room_id || profile.rooms?.[0]?.room_id,
|
||
url: profile.rooms?.[0]?.url,
|
||
source_url: profile.rooms?.[0]?.url,
|
||
anchor_name: profile.anchor_name,
|
||
}}
|
||
onError={(m) => setError(m)}
|
||
/>
|
||
{profile.rooms?.[0]?.url ? (
|
||
<a href={profile.rooms[0].url} target="_blank" rel="noreferrer" className="rounded-xl border border-white/10 bg-white/5 px-3 py-1.5 text-xs hover:bg-white/10">
|
||
打开直播间
|
||
</a>
|
||
) : null}
|
||
</div>
|
||
</div>
|
||
|
||
<div className="mb-4 grid gap-3 sm:grid-cols-4">
|
||
<div className="rounded-xl border border-white/10 bg-black/20 px-3 py-3">
|
||
<div className="text-[11px] text-slate-500">场次</div>
|
||
<div className="font-display text-xl"><AnimatedNumber value={profile.overview.sessions} /></div>
|
||
</div>
|
||
<div className="rounded-xl border border-white/10 bg-black/20 px-3 py-3">
|
||
<div className="text-[11px] text-slate-500">累计小时</div>
|
||
<div className="font-display text-xl"><AnimatedNumber value={profile.overview.total_hours} decimals={1} /></div>
|
||
</div>
|
||
<div className="rounded-xl border border-white/10 bg-black/20 px-3 py-3">
|
||
<div className="text-[11px] text-slate-500">平均场长</div>
|
||
<div className="font-display text-xl">{fmtDuration(profile.overview.avg_duration_seconds)}</div>
|
||
</div>
|
||
<div className="rounded-xl border border-white/10 bg-black/20 px-3 py-3">
|
||
<div className="text-[11px] text-slate-500">最近开播</div>
|
||
<div className="text-sm text-slate-200">{fmtTs(profile.overview.last_started_at)}</div>
|
||
</div>
|
||
</div>
|
||
|
||
{profile.schedule ? (
|
||
<div className="mb-4 rounded-xl border border-teal-500/20 bg-teal-500/5 p-3">
|
||
<div className="mb-2 flex flex-wrap items-center justify-between gap-2">
|
||
<h4 className="text-sm font-medium text-teal-100">开播作息规律</h4>
|
||
<Link
|
||
to={`/slots?date=${todayBeijingISO()}${
|
||
profile.schedule.peak_slot != null ? `&focus=${profile.schedule.peak_slot}` : ""
|
||
}`}
|
||
className="text-[11px] text-teal-300 hover:underline"
|
||
>
|
||
去时段页看全局 →
|
||
</Link>
|
||
</div>
|
||
<p className="text-sm text-slate-300">{profile.schedule.typical_open_hint || "样本不足"}</p>
|
||
<div className="mt-3 grid gap-2 sm:grid-cols-3">
|
||
<div className="rounded-lg bg-black/20 px-3 py-2 text-xs">
|
||
<div className="text-slate-500">常开时段</div>
|
||
<div className="mt-0.5 text-white">
|
||
{profile.schedule.peak_slot_label || "—"}
|
||
{profile.schedule.peak_slot_range ? (
|
||
<span className="ml-1 text-slate-400">({profile.schedule.peak_slot_range})</span>
|
||
) : null}
|
||
</div>
|
||
</div>
|
||
<div className="rounded-lg bg-black/20 px-3 py-2 text-xs">
|
||
<div className="text-slate-500">平均开播间隔</div>
|
||
<div className="mt-0.5 text-white">
|
||
{profile.schedule.avg_gap_hours != null ? `${profile.schedule.avg_gap_hours} 小时` : "—"}
|
||
</div>
|
||
</div>
|
||
<div className="rounded-lg bg-black/20 px-3 py-2 text-xs">
|
||
<div className="text-slate-500">预计下次</div>
|
||
<div className="mt-0.5 text-white">
|
||
{profile.schedule.next_open_hint?.label || "—"}
|
||
</div>
|
||
{profile.schedule.next_open_hint?.based_on ? (
|
||
<div className="mt-0.5 text-[10px] text-slate-500">{profile.schedule.next_open_hint.based_on}</div>
|
||
) : null}
|
||
</div>
|
||
</div>
|
||
{profile.schedule.slot_counts?.length === 6 ? (
|
||
<div className="mt-3 grid grid-cols-6 gap-1">
|
||
{profile.schedule.slot_counts.map((n, i) => {
|
||
const max = Math.max(1, ...profile.schedule!.slot_counts!);
|
||
return (
|
||
<div key={SLOT_LABELS[i]} className="text-center">
|
||
<div className="mx-auto flex h-12 w-full items-end justify-center rounded-md bg-black/20 px-1">
|
||
<div
|
||
className="w-full rounded-sm bg-gradient-to-t from-teal-500/80 to-teal-300/50"
|
||
style={{ height: `${Math.max(8, (n / max) * 100)}%` }}
|
||
title={`${SLOT_LABELS[i]}: ${n}`}
|
||
/>
|
||
</div>
|
||
<div className="mt-1 text-[10px] text-slate-500">{SLOT_LABELS[i]}</div>
|
||
<div className="text-[10px] tabular-nums text-slate-400">{n}</div>
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
) : null}
|
||
</div>
|
||
) : null}
|
||
|
||
<h4 className="mb-2 text-sm font-medium text-slate-200">最近场次</h4>
|
||
<div className="max-h-[420px] space-y-2 overflow-auto">
|
||
{profile.sessions.map((s) => (
|
||
<div
|
||
key={s.id}
|
||
className="flex w-full items-center gap-3 rounded-xl border border-white/5 bg-black/20 p-2"
|
||
>
|
||
<button
|
||
type="button"
|
||
onClick={() => void openSession(s)}
|
||
className="flex min-w-0 flex-1 items-center gap-3 text-left hover:opacity-90"
|
||
>
|
||
{sessionCoverSrc(s) ? (
|
||
// eslint-disable-next-line @next/next/no-img-element
|
||
<img src={sessionCoverSrc(s)} alt="" className="h-14 w-9 rounded object-cover" />
|
||
) : (
|
||
<div className="flex h-14 w-9 items-center justify-center rounded bg-slate-900 text-[10px] text-slate-600">N/A</div>
|
||
)}
|
||
<div className="min-w-0 flex-1 text-sm">
|
||
<div className="truncate text-slate-200">{s.title || "场次"}</div>
|
||
<div className="text-xs text-slate-500">
|
||
{fmtTs(s.started_at)} · {fmtDuration(s.duration_seconds)} · {s.status}
|
||
</div>
|
||
</div>
|
||
<span className="shrink-0 text-[11px] text-teal-400/80">详情 →</span>
|
||
</button>
|
||
<RoomQuickActions
|
||
compact
|
||
room={{
|
||
room_pk: profile.rooms?.[0]?.id,
|
||
room_id: s.room_id || profile.room_id,
|
||
source_url: s.source_url || profile.rooms?.[0]?.url,
|
||
anchor_name: profile.anchor_name,
|
||
}}
|
||
onError={(m) => setError(m)}
|
||
/>
|
||
</div>
|
||
))}
|
||
</div>
|
||
|
||
<div className="mt-4 text-xs text-slate-500">
|
||
也可在 <Link to={`/sessions?q=${encodeURIComponent(profile.anchor_name)}`} className="text-teal-300 hover:underline">场次日志</Link> 中按主播名筛选。
|
||
</div>
|
||
</>
|
||
) : (
|
||
<div className="py-16 text-center text-slate-500">选择左侧主播查看画像</div>
|
||
)}
|
||
</section>
|
||
</div>
|
||
)}
|
||
|
||
{detail ? (
|
||
<SessionDetailModal session={detail} onClose={() => setDetail(null)} onUpdated={() => selected && void loadProfile(selected)} />
|
||
) : null}
|
||
</Shell>
|
||
);
|
||
}
|