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>
736 lines
32 KiB
TypeScript
736 lines
32 KiB
TypeScript
"use client";
|
||
|
||
import { useCallback, useEffect, useState } from "react";
|
||
import Link from "next/link";
|
||
import { Loader2, RefreshCw, ArrowRight, ShieldOff } from "lucide-react";
|
||
|
||
import { Shell } from "@/components/Shell";
|
||
import { SessionCharts, type RelayStats } from "@/components/SessionCharts";
|
||
import { SessionDetailModal, type SessionDetail } from "@/components/SessionDetailModal";
|
||
import { AnimatedNumber, LivePulse, StatTile, PlatformBadge } from "@/components/ui";
|
||
import { YouTubeLiveBadge } from "@/components/YouTubeLiveBadge";
|
||
import { YouTubeLiveTicker } from "@/components/YouTubeLiveTicker";
|
||
import { useToast } from "@/components/Toast";
|
||
import { useConfirm } from "@/components/ConfirmDialog";
|
||
import { ImageLightbox } from "@/components/ImageLightbox";
|
||
import { RoomQuickActions, isFavorited } from "@/components/RoomQuickActions";
|
||
import { fetchJson, fmtTs, sessionCoverSrc, todayBeijingISO, beijingHourFromTs, beijingParts, detectPlatform } from "@/lib/api";
|
||
import { liveElapsed, useEventStream, useNow, type RefreshOpts } from "@/lib/realtime";
|
||
import { channelLiveRef, useYouTubeLiveCount } from "@/lib/youtubeLive";
|
||
|
||
type MonitorStatus = {
|
||
running: boolean;
|
||
monitor_enabled: boolean;
|
||
checked_total: number;
|
||
live_count: number;
|
||
room_count: number;
|
||
blacklist_count: number;
|
||
events_count?: number;
|
||
risk_paused_until: number;
|
||
last_error: string;
|
||
last_check_at: number;
|
||
quiet_hours_active?: boolean;
|
||
quiet_hours_priority_only?: boolean;
|
||
error_rooms_1h?: number;
|
||
overdue?: { count?: number; max_lag_sec?: number };
|
||
cookie?: {
|
||
has?: boolean;
|
||
length?: number;
|
||
updated_at?: number;
|
||
age_hours?: number | null;
|
||
};
|
||
config?: {
|
||
quiet_hours_enabled?: boolean;
|
||
quiet_hours_start?: number;
|
||
quiet_hours_end?: number;
|
||
has_cookie?: boolean;
|
||
has_proxy?: boolean;
|
||
};
|
||
};
|
||
|
||
type Room = {
|
||
id: string;
|
||
url: string;
|
||
room_id: string;
|
||
last_status: string;
|
||
last_anchor?: string;
|
||
tags?: string[] | string;
|
||
};
|
||
|
||
type UpcomingItem = {
|
||
anchor_name?: string;
|
||
room_id?: string;
|
||
room_pk?: string;
|
||
url?: string;
|
||
priority?: boolean;
|
||
last_status?: string;
|
||
peak_slot_label?: string;
|
||
eta_ts?: number;
|
||
next_open_hint?: { label?: string; based_on?: string };
|
||
typical_open_hint?: string;
|
||
};
|
||
|
||
type EventItem = {
|
||
id: string;
|
||
ts: number;
|
||
kind?: string;
|
||
message?: string;
|
||
anchor_name?: string;
|
||
room_id?: string;
|
||
session_id?: string;
|
||
};
|
||
|
||
type ActiveSession = {
|
||
id: string;
|
||
anchor_name?: string;
|
||
room_id?: string;
|
||
room_pk?: string;
|
||
source_url?: string;
|
||
started_at?: number;
|
||
cover_url?: string;
|
||
cover_proxy_url?: string;
|
||
};
|
||
|
||
export default function HomePage() {
|
||
const [stats, setStats] = useState<RelayStats | null>(null);
|
||
const [ytStats, setYtStats] = useState<RelayStats | null>(null);
|
||
const [status, setStatus] = useState<MonitorStatus | null>(null);
|
||
const [ytStatus, setYtStatus] = useState<Record<string, any> | null>(null);
|
||
const [rooms, setRooms] = useState<Room[]>([]);
|
||
const [events, setEvents] = useState<EventItem[]>([]);
|
||
const [active, setActive] = useState<ActiveSession[]>([]);
|
||
const [todaySlots, setTodaySlots] = useState<number[]>([0, 0, 0, 0, 0, 0]);
|
||
const [upcoming, setUpcoming] = useState<UpcomingItem[]>([]);
|
||
const [focusOnly, setFocusOnly] = useState(false);
|
||
const [loading, setLoading] = useState(false);
|
||
const [error, setError] = useState("");
|
||
const [lightbox, setLightbox] = useState<ActiveSession | null>(null);
|
||
const [detail, setDetail] = useState<SessionDetail | null>(null);
|
||
const [opsBusy, setOpsBusy] = useState("");
|
||
const toast = useToast();
|
||
const confirm = useConfirm();
|
||
const now = useNow(1000);
|
||
const { connected, last, tick } = useEventStream(true);
|
||
const ytLiveCount = useYouTubeLiveCount();
|
||
|
||
const load = useCallback(async (opts?: RefreshOpts) => {
|
||
const silent = Boolean(opts?.silent);
|
||
if (!silent) {
|
||
setLoading(true);
|
||
setError("");
|
||
}
|
||
try {
|
||
const iso = todayBeijingISO();
|
||
const [s, ytS, m, yt, r, e, a, daySessions, up] = await Promise.all([
|
||
fetchJson("/api/sessions/stats?top_anchors=25"),
|
||
fetchJson("/api/sessions/stats?platform=youtube&top_anchors=10").catch(() => null),
|
||
fetchJson("/api/monitor/status"),
|
||
fetchJson("/api/youtube-monitor/status").catch(() => null),
|
||
fetchJson("/api/rooms?limit=500"),
|
||
fetchJson("/api/events?limit=20"),
|
||
fetchJson("/api/sessions/active"),
|
||
fetchJson(`/api/sessions?date_from=${iso}&date_to=${iso}&limit=500`),
|
||
fetchJson("/api/anchors/upcoming?limit=12"),
|
||
]);
|
||
setStats(s);
|
||
setYtStats(ytS);
|
||
setStatus(m);
|
||
setYtStatus(yt);
|
||
setRooms(r.rooms || []);
|
||
setEvents(e.events || []);
|
||
setUpcoming(up.items || []);
|
||
const sessions = (a.sessions || []) as ActiveSession[];
|
||
const seen = new Set<string>();
|
||
setActive(
|
||
sessions.filter((s) => {
|
||
const key = s.room_id || s.id;
|
||
if (!key || seen.has(key)) return false;
|
||
seen.add(key);
|
||
return true;
|
||
}),
|
||
);
|
||
const buckets = [0, 0, 0, 0, 0, 0];
|
||
for (const sess of daySessions.sessions || []) {
|
||
const ts = Number(sess.started_at || 0);
|
||
if (!ts) continue;
|
||
const hour = beijingHourFromTs(ts);
|
||
buckets[Math.min(5, Math.floor(hour / 4))] += 1;
|
||
}
|
||
setTodaySlots(buckets);
|
||
} catch (err) {
|
||
if (!silent) setError((err as Error).message);
|
||
} finally {
|
||
if (!silent) setLoading(false);
|
||
}
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
void load({ silent: false });
|
||
const t = setInterval(() => void load({ silent: true }), 20000);
|
||
return () => clearInterval(t);
|
||
}, [load]);
|
||
|
||
useEffect(() => {
|
||
if (!last?.message) return;
|
||
setEvents((prev) => {
|
||
const next = [
|
||
{
|
||
...(last as EventItem),
|
||
id: last.id || `${tick}`,
|
||
session_id: last.session_id,
|
||
},
|
||
...prev.filter((x) => x.id !== last.id),
|
||
];
|
||
return next.slice(0, 30);
|
||
});
|
||
if (last.kind === "live" || last.kind === "risk" || last.kind === "offline") {
|
||
void load({ silent: true });
|
||
}
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
}, [tick]);
|
||
|
||
const priorityRoomIds = new Set(
|
||
rooms
|
||
.filter((r) => isFavorited(r.tags))
|
||
.map((r) => r.room_id),
|
||
);
|
||
const liveRooms = rooms.filter((r) => r.last_status === "live" && (!focusOnly || priorityRoomIds.has(r.room_id)));
|
||
const focusLive = active.filter((s) => {
|
||
const isYt = detectPlatform(s) === "youtube";
|
||
if (focusOnly && isYt) return false;
|
||
return !focusOnly || (s.room_id && priorityRoomIds.has(s.room_id));
|
||
});
|
||
const riskActive = !!(status?.risk_paused_until && status.risk_paused_until * 1000 > Date.now());
|
||
const missedOpens = upcoming.filter(
|
||
(u) =>
|
||
u.priority &&
|
||
Number(u.eta_ts || 0) > 0 &&
|
||
Number(u.eta_ts) * 1000 < now &&
|
||
u.last_status !== "live",
|
||
);
|
||
|
||
async function toggleFocus(roomId?: string) {
|
||
if (!roomId || roomId.startsWith("yt:")) {
|
||
toast.push("YouTube 场次请到「分组」管理", "warn");
|
||
return;
|
||
}
|
||
const room = rooms.find((r) => r.room_id === roomId);
|
||
try {
|
||
const { roomQuickAction } = await import("@/components/RoomQuickActions");
|
||
await roomQuickAction(
|
||
room && isFavorited(room.tags) ? "unfavorite" : "favorite",
|
||
{ room_pk: room?.id, room_id: roomId, url: room?.url },
|
||
);
|
||
toast.push(room && isFavorited(room.tags) ? "已取消收藏" : "已收藏", "ok");
|
||
await load();
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "warn");
|
||
}
|
||
}
|
||
|
||
async function openSessionDetail(sessionId?: string) {
|
||
if (!sessionId) return;
|
||
try {
|
||
const data = await fetchJson(`/api/sessions/${sessionId}`);
|
||
setDetail((data.session || data) as SessionDetail);
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "warn");
|
||
}
|
||
}
|
||
|
||
async function toggleMonitor(enabled: boolean) {
|
||
setOpsBusy(enabled ? "start" : "stop");
|
||
try {
|
||
await fetchJson(enabled ? "/api/monitor/start" : "/api/monitor/stop", { method: "POST" });
|
||
toast.push(enabled ? "监控已恢复" : "监控已暂停", "ok");
|
||
await load();
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "warn");
|
||
} finally {
|
||
setOpsBusy("");
|
||
}
|
||
}
|
||
|
||
async function syncCookie() {
|
||
setOpsBusy("cookie");
|
||
try {
|
||
const data = await fetchJson("/api/settings/sync-cookie", { method: "POST" });
|
||
toast.push(data.message || (data.ok ? "Cookie 已同步" : "同步完成"), "ok");
|
||
await load();
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "warn");
|
||
} finally {
|
||
setOpsBusy("");
|
||
}
|
||
}
|
||
|
||
const cookieAge = status?.cookie?.age_hours;
|
||
const cookieStale = cookieAge != null && cookieAge >= 72;
|
||
const cookieWarn = cookieAge != null && cookieAge >= 24;
|
||
const errorRooms = Number(status?.error_rooms_1h || 0);
|
||
|
||
return (
|
||
<Shell>
|
||
<section className="mb-6 flex flex-wrap items-end justify-between gap-4">
|
||
<div>
|
||
<div className="mb-2 flex flex-wrap items-center gap-2">
|
||
{status?.monitor_enabled ? <LivePulse label="MONITOR ON" /> : (
|
||
<span className="rounded-md border border-slate-500/30 bg-slate-500/10 px-2 py-1 text-[11px] text-slate-300">⏸ 已暂停</span>
|
||
)}
|
||
{liveRooms.length > 0 ? <LivePulse label={`抖音 ${liveRooms.length} 路`} /> : null}
|
||
{ytLiveCount > 0 ? (
|
||
<Link href="/groups" className="rounded-md border border-red-500/30 bg-red-500/10 px-2 py-1 text-[11px] text-red-100">
|
||
<span className="inline-flex items-center gap-1.5">
|
||
<span className="live-dot" />
|
||
YT 在播 {ytLiveCount}
|
||
</span>
|
||
</Link>
|
||
) : ytStatus?.yt_monitor_enabled !== false && ytStatus?.running ? (
|
||
<Link href="/groups" className="rounded-md border border-white/10 bg-white/5 px-2 py-1 text-[11px] text-slate-400">
|
||
▶ YT 轮询中 · {String(ytStatus?.groups?.enabled ?? "—")} 组
|
||
</Link>
|
||
) : (
|
||
<Link href="/settings" className="rounded-md border border-white/10 bg-white/5 px-2 py-1 text-[11px] text-slate-500">
|
||
▶ YT 已暂停
|
||
</Link>
|
||
)}
|
||
<span className={`rounded-md border px-2 py-1 text-[11px] ${connected ? "border-teal-500/30 bg-teal-500/10 text-teal-100" : "border-slate-500/30 bg-slate-500/10 text-slate-400"}`}>
|
||
{connected ? "🟢 SSE 已连接" : "⚪ SSE 重连中"}
|
||
</span>
|
||
{riskActive ? (
|
||
<span className="rounded-md border border-amber-500/30 bg-amber-500/10 px-2 py-1 text-[11px] text-amber-100">⚠️ 风控熔断中</span>
|
||
) : null}
|
||
{status?.quiet_hours_active ? (
|
||
<span className="rounded-md border border-sky-500/30 bg-sky-500/10 px-2 py-1 text-[11px] text-sky-100">
|
||
🌙 静默{status.quiet_hours_priority_only ? "·仅重点" : ""} {status.config?.quiet_hours_start ?? 2}–{status.config?.quiet_hours_end ?? 7}
|
||
</span>
|
||
) : status?.config?.quiet_hours_enabled ? (
|
||
<span className="rounded-md border border-white/10 bg-white/5 px-2 py-1 text-[11px] text-slate-400">静默已配置</span>
|
||
) : null}
|
||
{status?.cookie?.has ? (
|
||
cookieStale || cookieWarn ? (
|
||
<button
|
||
type="button"
|
||
disabled={opsBusy === "cookie"}
|
||
onClick={() => void syncCookie()}
|
||
className={`rounded-md border px-2 py-1 text-[11px] ${
|
||
cookieStale
|
||
? "border-rose-500/30 bg-rose-500/10 text-rose-100"
|
||
: "border-amber-500/30 bg-amber-500/10 text-amber-100"
|
||
}`}
|
||
title="点击从 sh2 同步 Cookie"
|
||
>
|
||
🍪 Cookie {cookieAge != null ? `${cookieAge}h` : "陈旧"} · 点此同步
|
||
</button>
|
||
) : (
|
||
<span
|
||
className="rounded-md border border-white/10 bg-white/5 px-2 py-1 text-[11px] text-slate-300"
|
||
title={status.cookie.updated_at ? `更新于 ${fmtTs(status.cookie.updated_at)}` : "Cookie 年龄未知"}
|
||
>
|
||
🍪 Cookie {cookieAge != null ? `${cookieAge}h` : "已配置"}
|
||
</span>
|
||
)
|
||
) : (
|
||
<Link href="/settings" className="rounded-md border border-rose-500/30 bg-rose-500/10 px-2 py-1 text-[11px] text-rose-100">
|
||
🍪 未配置 Cookie
|
||
</Link>
|
||
)}
|
||
{errorRooms > 0 ? (
|
||
<Link href="/rooms?status=error" className="rounded-md border border-orange-500/30 bg-orange-500/10 px-2 py-1 text-[11px] text-orange-100">
|
||
探测异常 {errorRooms}
|
||
</Link>
|
||
) : (
|
||
<span className="rounded-md border border-white/10 bg-white/5 px-2 py-1 text-[11px] text-slate-400">
|
||
探测正常 · {fmtTs(status?.last_check_at)}
|
||
</span>
|
||
)}
|
||
{Number(status?.overdue?.count || 0) > 0 ? (
|
||
<Link
|
||
href="/rooms?overdue=1"
|
||
className="rounded-md border border-violet-500/30 bg-violet-500/10 px-2 py-1 text-[11px] text-violet-100"
|
||
title={`最大滞后 ${Math.round(Number(status?.overdue?.max_lag_sec || 0))}s`}
|
||
>
|
||
队列积压 {status?.overdue?.count}
|
||
</Link>
|
||
) : null}
|
||
{missedOpens.length > 0 ? (
|
||
<span className="rounded-md border border-rose-500/30 bg-rose-500/10 px-2 py-1 text-[11px] text-rose-100">
|
||
重点延误 {missedOpens.length}
|
||
</span>
|
||
) : null}
|
||
{status?.last_error ? (
|
||
<span className="max-w-[220px] truncate rounded-md border border-rose-500/20 bg-rose-500/5 px-2 py-1 text-[11px] text-rose-200/90" title={status.last_error}>
|
||
最近错误:{status.last_error}
|
||
</span>
|
||
) : null}
|
||
</div>
|
||
<h2 className="font-display text-2xl font-semibold tracking-tight">监控雷达台</h2>
|
||
<p className="mt-1 text-sm text-slate-400">抖音 + YouTube 双平台开播态势 · 时段规律 · 事件回放</p>
|
||
</div>
|
||
<div className="flex flex-wrap gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => setFocusOnly((v) => !v)}
|
||
className={`rounded-xl border px-3 py-2 text-sm ${
|
||
focusOnly
|
||
? "border-amber-400/40 bg-amber-500/15 text-amber-100"
|
||
: "border-white/10 bg-white/5 text-slate-300 hover:bg-white/10"
|
||
}`}
|
||
>
|
||
{focusOnly ? "★ 收藏看板 ON" : "☆ 收藏看板"}
|
||
</button>
|
||
{status?.monitor_enabled ? (
|
||
<button
|
||
type="button"
|
||
disabled={Boolean(opsBusy)}
|
||
onClick={() => void toggleMonitor(false)}
|
||
className="rounded-xl border border-slate-500/30 bg-slate-500/10 px-3 py-2 text-sm text-slate-200"
|
||
>
|
||
{opsBusy === "stop" ? <Loader2 className="inline h-4 w-4 animate-spin" /> : "⏸"} 暂停
|
||
</button>
|
||
) : (
|
||
<button
|
||
type="button"
|
||
disabled={Boolean(opsBusy)}
|
||
onClick={() => void toggleMonitor(true)}
|
||
className="rounded-xl border border-teal-500/30 bg-teal-500/10 px-3 py-2 text-sm text-teal-100"
|
||
>
|
||
{opsBusy === "start" ? <Loader2 className="inline h-4 w-4 animate-spin" /> : "▶"} 恢复监控
|
||
</button>
|
||
)}
|
||
<button type="button" onClick={() => void load()} className="btn-glow inline-flex items-center gap-2 rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm hover:bg-white/10">
|
||
{loading ? <Loader2 className="h-4 w-4 animate-spin" /> : <RefreshCw className="h-4 w-4" />}
|
||
刷新
|
||
</button>
|
||
{riskActive ? (
|
||
<button
|
||
type="button"
|
||
onClick={async () => {
|
||
await fetchJson("/api/monitor/clear-risk", { method: "POST" });
|
||
await load();
|
||
}}
|
||
className="btn-glow inline-flex items-center gap-2 rounded-xl border border-amber-500/30 bg-amber-500/10 px-3 py-2 text-sm text-amber-100"
|
||
>
|
||
<ShieldOff className="h-4 w-4" />
|
||
解除风控
|
||
</button>
|
||
) : null}
|
||
</div>
|
||
</section>
|
||
|
||
{error ? <div className="mb-4 animate-fade-up rounded-xl border border-rose-500/30 bg-rose-500/10 px-4 py-3 text-sm">{error}</div> : null}
|
||
|
||
{missedOpens.length > 0 ? (
|
||
<section className="mb-6 rounded-2xl border border-rose-500/30 bg-rose-500/10 p-4">
|
||
<div className="mb-3 flex items-center justify-between gap-2">
|
||
<h3 className="font-display text-sm font-medium text-rose-100">⏰ 重点延误开播(已过预计时间仍未开播)</h3>
|
||
<span className="text-xs text-rose-200/80">{missedOpens.length} 个</span>
|
||
</div>
|
||
<div className="grid gap-2 sm:grid-cols-2 lg:grid-cols-3">
|
||
{missedOpens.slice(0, 9).map((u) => {
|
||
const lateMin = Math.max(1, Math.round((now / 1000 - Number(u.eta_ts || 0)) / 60));
|
||
return (
|
||
<div key={`miss-${u.room_id}-${u.anchor_name}`} className="flex items-center gap-2 rounded-xl border border-rose-500/20 bg-black/25 px-3 py-2">
|
||
<Link
|
||
href={`/anchors?q=${encodeURIComponent(u.anchor_name || u.room_id || "")}`}
|
||
className="min-w-0 flex-1"
|
||
>
|
||
<div className="truncate text-sm font-medium text-white">{u.anchor_name}</div>
|
||
<div className="text-[11px] text-rose-200/90">延误约 {lateMin} 分钟 · {u.next_open_hint?.label || "—"}</div>
|
||
</Link>
|
||
{u.room_pk ? (
|
||
<button
|
||
type="button"
|
||
onClick={async () => {
|
||
try {
|
||
await fetchJson(`/api/monitor/check/${u.room_pk}`, { method: "POST" });
|
||
toast.push(`已检查 ${u.anchor_name || u.room_id}`, "ok");
|
||
await load();
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "warn");
|
||
}
|
||
}}
|
||
className="shrink-0 rounded-lg border border-white/10 bg-white/5 px-2 py-1 text-xs hover:bg-white/10"
|
||
>
|
||
立刻检查
|
||
</button>
|
||
) : null}
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
</section>
|
||
) : null}
|
||
|
||
<div className="stagger mb-6 grid gap-3 sm:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-6">
|
||
<StatTile emoji="🏠" label="抖音直播间" value={<AnimatedNumber value={status?.room_count || 0} />} hint="已纳入监控队列" accent="sky" />
|
||
<StatTile emoji="🔴" label="抖音在播" value={<AnimatedNumber value={status?.live_count || 0} />} hint="正在直播中的房间" accent="rose" />
|
||
<StatTile emoji="▶️" label="YT 在播" value={<AnimatedNumber value={ytLiveCount} />} hint={`启用分组 ${String(ytStatus?.groups?.enabled ?? "—")}`} accent="amber" />
|
||
<StatTile
|
||
emoji="📺"
|
||
label="YT 场次/时长"
|
||
value={
|
||
<span className="tabular-nums">
|
||
<AnimatedNumber value={Number(ytStats?.overview?.sessions || 0)} />
|
||
<span className="mx-1 text-base text-slate-500">/</span>
|
||
<AnimatedNumber value={Number(ytStats?.overview?.total_hours || 0)} decimals={1} suffix="h" />
|
||
</span>
|
||
}
|
||
hint="YouTube 历史场次合计"
|
||
accent="teal"
|
||
/>
|
||
<StatTile emoji="⏱️" label="全平台时长" value={<AnimatedNumber value={Number(stats?.overview?.total_hours || 0)} decimals={1} suffix="h" />} hint="抖音+YouTube" accent="teal" />
|
||
<StatTile emoji="🎯" label="检查次数" value={<AnimatedNumber value={status?.checked_total || 0} />} hint={`上次 ${fmtTs(status?.last_check_at)}`} accent="amber" />
|
||
</div>
|
||
|
||
<section className="mb-6 glass-panel rounded-2xl p-4">
|
||
<div className="mb-3 flex items-center justify-between gap-2">
|
||
<h3 className="font-display text-sm font-medium text-slate-100">🕐 今日开播六段分布</h3>
|
||
<Link href="/slots" className="text-xs text-teal-300 hover:underline">
|
||
看时段规律 →
|
||
</Link>
|
||
</div>
|
||
<div className="grid grid-cols-3 gap-2 sm:grid-cols-6">
|
||
{["凌晨", "清晨", "上午", "午后", "傍晚", "夜间"].map((label, i) => {
|
||
const n = todaySlots[i] || 0;
|
||
const max = Math.max(1, ...todaySlots);
|
||
const nowSlot = Math.min(5, Math.floor(beijingParts().hour / 4));
|
||
return (
|
||
<Link
|
||
key={label}
|
||
href={`/slots?focus=${i}`}
|
||
className={`rounded-xl border px-2 py-2 text-center ${
|
||
i === nowSlot ? "border-teal-400/40 bg-teal-500/10" : "border-white/10 bg-black/20"
|
||
}`}
|
||
>
|
||
<div className="text-[11px] text-slate-400">{label}</div>
|
||
<div className="font-display text-lg text-white">{n}</div>
|
||
<div className="mx-auto mt-1 h-1 w-full overflow-hidden rounded-full bg-white/5">
|
||
<div className="h-full bg-gradient-to-r from-teal-400/80 to-rose-400/60" style={{ width: `${(n / max) * 100}%` }} />
|
||
</div>
|
||
</Link>
|
||
);
|
||
})}
|
||
</div>
|
||
</section>
|
||
|
||
{focusLive.length > 0 ? (
|
||
<section className="mb-6 glass-panel rounded-2xl p-4">
|
||
<div className="mb-3 flex items-center justify-between">
|
||
<h3 className="font-display text-sm font-medium text-slate-100">
|
||
🔴 进行中场次(实时计时){focusOnly ? " · 仅重点" : ""}
|
||
</h3>
|
||
<span className="text-xs text-slate-500">{focusLive.length} 路</span>
|
||
</div>
|
||
<div className="grid gap-2 sm:grid-cols-2 lg:grid-cols-3">
|
||
{focusLive.map((s) => (
|
||
<div
|
||
key={s.id}
|
||
className="hover-lift flex items-center gap-3 rounded-xl border border-rose-500/20 bg-rose-500/5 px-3 py-2"
|
||
>
|
||
{sessionCoverSrc(s) ? (
|
||
<button
|
||
type="button"
|
||
onClick={() => setLightbox(s)}
|
||
className="shrink-0 overflow-hidden rounded ring-1 ring-white/10 transition hover:ring-rose-300/50"
|
||
title="点击放大"
|
||
>
|
||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||
<img src={sessionCoverSrc(s)} alt="" className="h-14 w-10 object-cover" />
|
||
</button>
|
||
) : (
|
||
<div className="flex h-14 w-10 shrink-0 items-center justify-center rounded bg-slate-900 text-[10px] text-slate-600">LIVE</div>
|
||
)}
|
||
<button
|
||
type="button"
|
||
onClick={() => void openSessionDetail(s.id)}
|
||
className="min-w-0 flex-1 text-left"
|
||
title="查看场次详情"
|
||
>
|
||
<div className="truncate text-sm font-medium text-white hover:underline">
|
||
<span className="mr-1.5 inline-block align-middle">
|
||
<PlatformBadge platform={detectPlatform(s)} />
|
||
</span>
|
||
{s.anchor_name || s.room_id}
|
||
{detectPlatform(s) === "youtube" && s.room_id?.startsWith("yt:UC") ? (
|
||
<span className="ml-1.5 inline-block align-middle">
|
||
<YouTubeLiveBadge refKey={channelLiveRef(s.room_id.slice(3))} compact />
|
||
</span>
|
||
) : null}
|
||
</div>
|
||
<div className="font-mono text-xs text-rose-200">{liveElapsed(s.started_at, now)}</div>
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={async () => {
|
||
const ok = await confirm({
|
||
title: "结束场次",
|
||
message: `结束「${s.anchor_name || s.room_id}」这场?`,
|
||
confirmLabel: "结束",
|
||
destructive: true,
|
||
});
|
||
if (!ok) return;
|
||
try {
|
||
await fetchJson(`/api/sessions/${s.id}/end`, { method: "POST" });
|
||
toast.push("已结束场次", "ok");
|
||
await load();
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "warn");
|
||
}
|
||
}}
|
||
className="shrink-0 rounded-lg border border-amber-500/30 bg-amber-500/10 px-2 py-1 text-xs text-amber-100 hover:bg-amber-500/20"
|
||
title="手动结束场次"
|
||
>
|
||
⏹
|
||
</button>
|
||
{detectPlatform(s) === "youtube" ? (
|
||
<Link
|
||
href="/groups"
|
||
className="shrink-0 rounded-lg border border-white/10 bg-black/20 px-2 py-1 text-xs text-slate-400 hover:text-red-100"
|
||
title="前往 YouTube 分组"
|
||
>
|
||
分组
|
||
</Link>
|
||
) : (
|
||
<RoomQuickActions
|
||
compact
|
||
room={{
|
||
room_pk: s.room_pk || rooms.find((r) => r.room_id === s.room_id)?.id,
|
||
room_id: s.room_id,
|
||
source_url: s.source_url,
|
||
anchor_name: s.anchor_name,
|
||
}}
|
||
favorited={Boolean(s.room_id && priorityRoomIds.has(s.room_id))}
|
||
onDone={() => void load()}
|
||
onError={(m) => toast.push(m, "warn")}
|
||
/>
|
||
)}
|
||
</div>
|
||
))}
|
||
</div>
|
||
</section>
|
||
) : null}
|
||
|
||
{lightbox && sessionCoverSrc(lightbox) ? (
|
||
<ImageLightbox
|
||
src={sessionCoverSrc(lightbox)}
|
||
title={lightbox.anchor_name || lightbox.room_id || "封面"}
|
||
subtitle={lightbox.source_url}
|
||
onClose={() => setLightbox(null)}
|
||
/>
|
||
) : null}
|
||
|
||
{detail ? (
|
||
<SessionDetailModal
|
||
session={detail}
|
||
onClose={() => setDetail(null)}
|
||
onUpdated={() => void load()}
|
||
/>
|
||
) : null}
|
||
|
||
{(liveRooms.length > 0 || events.length > 0) ? (
|
||
<div className="mb-6 overflow-hidden rounded-2xl border border-rose-500/20 bg-rose-500/5">
|
||
<div className="flex items-center gap-2 border-b border-white/5 px-4 py-2 text-xs text-rose-100/80">
|
||
<span className="live-dot" />
|
||
实时动态条
|
||
</div>
|
||
<div className="relative overflow-hidden py-3">
|
||
<div className="ticker-track gap-8 px-4 text-sm text-slate-200">
|
||
{[...liveRooms, ...liveRooms].length ? (
|
||
[...liveRooms, ...liveRooms, ...events.slice(0, 8), ...events.slice(0, 8)].map((item, idx) => {
|
||
if ("last_status" in item) {
|
||
return (
|
||
<a key={`live-${item.id}-${idx}`} href={item.url} target="_blank" rel="noreferrer" className="inline-flex items-center gap-2 whitespace-nowrap hover:text-white">
|
||
<span>🔴</span>
|
||
<span className="font-medium">{item.last_anchor || item.room_id}</span>
|
||
<span className="text-slate-500">在播中</span>
|
||
</a>
|
||
);
|
||
}
|
||
return (
|
||
<span key={`ev-${item.id}-${idx}`} className="inline-flex items-center gap-2 whitespace-nowrap text-slate-300">
|
||
<span>{item.kind === "live" ? "🎬" : item.kind === "risk" ? "⚠️" : "⚡"}</span>
|
||
<span>{item.message}</span>
|
||
</span>
|
||
);
|
||
})
|
||
) : (
|
||
<span className="text-slate-500">等待开播事件…</span>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
) : null}
|
||
|
||
<div className="mb-6 grid gap-4 lg:grid-cols-[1.4fr_1fr] lg:items-stretch">
|
||
<section className="glass-panel rounded-2xl p-4">
|
||
<div className="mb-3 flex flex-wrap items-center justify-between gap-2">
|
||
<h3 className="font-display text-sm font-medium text-slate-100">📊 可视化分析(全平台)</h3>
|
||
<div className="flex flex-wrap gap-3 text-xs">
|
||
<Link href="/sessions" className="inline-flex items-center gap-1 text-teal-300 hover:underline">
|
||
场次明细 <ArrowRight className="h-3 w-3" />
|
||
</Link>
|
||
<Link href="/groups?tab=analytics" className="inline-flex items-center gap-1 text-amber-300 hover:underline">
|
||
YouTube 分析 <ArrowRight className="h-3 w-3" />
|
||
</Link>
|
||
</div>
|
||
</div>
|
||
<SessionCharts stats={stats} />
|
||
{ytStats?.overview ? (
|
||
<div className="mt-4 rounded-xl border border-amber-500/20 bg-amber-500/5 px-3 py-2 text-xs text-amber-100/90">
|
||
YouTube 单独:场次 {ytStats.overview.sessions ?? 0} · 在播 {ytStats.overview.active_sessions ?? 0} · 累计{" "}
|
||
{Number(ytStats.overview.total_hours || 0).toFixed(1)}h · 频道 {ytStats.overview.unique_anchors ?? 0}
|
||
<Link href="/groups?tab=analytics" className="ml-2 text-teal-300 hover:underline">
|
||
打开图表 →
|
||
</Link>
|
||
</div>
|
||
) : null}
|
||
</section>
|
||
|
||
<section className="glass-panel flex h-full min-h-0 flex-col rounded-2xl p-4">
|
||
<div className="mb-3 flex shrink-0 items-center justify-between">
|
||
<h3 className="font-display text-sm font-medium text-slate-100">⚡ 最近事件</h3>
|
||
<Link href="/events" className="inline-flex items-center gap-1 text-xs text-teal-300 hover:underline">
|
||
全部 <ArrowRight className="h-3 w-3" />
|
||
</Link>
|
||
</div>
|
||
<div className="timeline-rail min-h-0 flex-1 space-y-3 overflow-y-auto pl-1">
|
||
{events.map((ev) => (
|
||
<button
|
||
key={ev.id}
|
||
type="button"
|
||
disabled={!ev.session_id}
|
||
onClick={() => void openSessionDetail(ev.session_id)}
|
||
className={`relative ml-1 w-full rounded-xl border border-white/5 bg-black/20 px-3 py-2 pl-8 text-left text-sm hover-lift ${
|
||
ev.session_id ? "cursor-pointer hover:border-teal-400/30" : "cursor-default"
|
||
}`}
|
||
>
|
||
<span className="absolute left-[6px] top-3 h-3 w-3 rounded-full bg-teal-400/80 ring-4 ring-teal-400/10" />
|
||
<div className="flex flex-wrap items-center gap-2 text-[11px] text-slate-500">
|
||
<span>{fmtTs(ev.ts)}</span>
|
||
<span className="uppercase text-teal-300/80">{ev.kind}</span>
|
||
{ev.session_id ? <span className="text-teal-400/70">详情 →</span> : null}
|
||
</div>
|
||
<div className="mt-1 text-slate-200">{ev.message}</div>
|
||
</button>
|
||
))}
|
||
{!events.length ? (
|
||
<div className="rounded-xl border border-dashed border-white/10 px-3 py-8 text-center text-sm text-slate-500">
|
||
暂无事件,开播后会出现在这里
|
||
</div>
|
||
) : null}
|
||
</div>
|
||
</section>
|
||
</div>
|
||
|
||
<div className="flex flex-wrap gap-3 text-sm">
|
||
<Link href="/slots" className="hover-lift glass-panel rounded-xl px-4 py-3 text-rose-200">🕐 开播时段规律</Link>
|
||
<Link href="/rooms" className="hover-lift glass-panel rounded-xl px-4 py-3 text-teal-200">🏠 管理直播间</Link>
|
||
<Link href="/anchors" className="hover-lift glass-panel rounded-xl px-4 py-3 text-fuchsia-200">👤 主播画像</Link>
|
||
<Link href="/covers" className="hover-lift glass-panel rounded-xl px-4 py-3 text-sky-200">🖼️ 浏览截图库</Link>
|
||
<Link href="/settings" className="hover-lift glass-panel rounded-xl px-4 py-3 text-amber-200">⚙️ 防风控设置</Link>
|
||
</div>
|
||
</Shell>
|
||
);
|
||
}
|