douyinWEB/desktop/src/components/Shell.tsx
eric 5b45fc0aca Add real-time Douyin live status map and badges across card UIs.
Expose /api/douyin/live-map from monitor DB with SSE-triggered client refresh, and show live/offline badges on assign, rooms, anchors, slots, and overview cards.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-22 03:33:46 -05:00

199 lines
8.6 KiB
TypeScript

import { useEffect, useState } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import { BrandMark, LivePulse } from "@/components/ui";
import { GlobalEventAlerts } from "@/components/GlobalEventAlerts";
import { fetchJson } from "@/lib/api";
import { DouyinLiveContextProvider, useDouyinLiveCount, useDouyinLiveProviderValue } from "@/lib/douyinLive";
import { YouTubeLiveContextProvider, useYouTubeLiveCount, useYouTubeLiveMap, useYouTubeLiveProviderValue } from "@/lib/youtubeLive";
import { Link } from "react-router-dom";
const NAV = [
{ href: "/", label: "概览", emoji: "📡" },
{ href: "/slots", label: "时段", emoji: "🕐" },
{ href: "/sessions", label: "场次", emoji: "🎬" },
{ href: "/anchors", label: "主播", emoji: "👤" },
{ href: "/covers", label: "截图", emoji: "🖼️" },
{ href: "/events", label: "事件", emoji: "⚡" },
{ href: "/rooms", label: "直播间", emoji: "🏠" },
{ href: "/groups", label: "分组", emoji: "📺" },
{ href: "/youtube", label: "YouTube", emoji: "▶️" },
{ href: "/shorts", label: "Shorts", emoji: "📱" },
{ href: "/blacklist", label: "黑名单", emoji: "🚫" },
{ href: "/settings", label: "设置", emoji: "⚙️" },
];
const MOBILE_PRIMARY = ["/", "/groups", "/shorts", "/youtube", "/settings"];
export function Shell({ children }: { children: React.ReactNode }) {
const ytCtx = useYouTubeLiveProviderValue();
const dyCtx = useDouyinLiveProviderValue();
return (
<YouTubeLiveContextProvider value={ytCtx}>
<DouyinLiveContextProvider value={dyCtx}>
<ShellFrame>{children}</ShellFrame>
</DouyinLiveContextProvider>
</YouTubeLiveContextProvider>
);
}
function ShellFrame({ children }: { children: React.ReactNode }) {
const pathname = useLocation().pathname;
const navigate = useNavigate();
const ytLiveCount = useYouTubeLiveCount();
const dyLiveCount = useDouyinLiveCount();
const { map: ytLiveMap } = useYouTubeLiveMap();
const [username, setUsername] = useState("");
useEffect(() => {
void fetchJson("/api/auth/status")
.then((d) => setUsername(String(d.username || "")))
.catch(() => setUsername(""));
}, [pathname]);
async function logout() {
try {
await fetchJson("/api/auth/logout", { method: "POST" });
} catch {
/* ignore */
}
navigate("/login");
}
return (
<div className="relative min-h-screen">
<GlobalEventAlerts />
<div className="ambient-orb a" />
<div className="ambient-orb b" />
<div className="ambient-orb c" />
<div className="relative z-10 mx-auto flex min-h-screen max-w-7xl flex-col px-4 py-5 sm:px-6">
<header className="mb-6 animate-fade-up border-b border-white/10 pb-5">
<div className="flex flex-wrap items-end justify-between gap-4">
<div className="flex items-start gap-3">
<BrandMark className="mt-1 h-12 w-12" />
<div>
<p className="font-display text-xs uppercase tracking-[0.28em] text-teal-300/90">Live Radar</p>
<h1 className="font-display text-3xl font-bold tracking-tight text-white sm:text-4xl">
直播监控中心
</h1>
<p className="mt-1 max-w-2xl text-sm text-slate-400">
抖音 + YouTube · 开播捕捉 · 停播结场 · 时长统计 · 截图存证 · Windows 客户端
</p>
{(dyLiveCount > 0 || ytLiveCount > 0) ? (
<div className="mt-2 flex flex-wrap items-center gap-2">
{dyLiveCount > 0 ? (
<Link to="/rooms">
<LivePulse label={`抖音 ${dyLiveCount} 路在播`} />
</Link>
) : null}
{ytLiveCount > 0 ? (
<Link to="/groups">
<LivePulse label={`YouTube ${ytLiveCount} 路在播`} />
</Link>
) : null}
{ytLiveMap?.checked_at ? (
<span className="text-[10px] text-slate-500">
轮询 {Math.max(0, Math.round(Date.now() / 1000 - Number(ytLiveMap.checked_at)))}s 前
</span>
) : null}
</div>
) : (
<div className="mt-2 flex flex-wrap items-center gap-2 text-[11px] text-slate-500">
<span>抖音 / YouTube 直播轮询中</span>
{ytLiveMap?.checked_at ? (
<span className="text-[10px]">
· {Math.max(0, Math.round(Date.now() / 1000 - Number(ytLiveMap.checked_at)))}s 前刷新
</span>
) : null}
</div>
)}
</div>
</div>
<div className="flex flex-wrap items-center gap-2">
{username ? (
<span className="rounded-xl border border-white/10 bg-white/5 px-3 py-1.5 text-xs text-slate-300">
{username}
</span>
) : null}
<button
type="button"
onClick={() => void logout()}
className="rounded-xl border border-white/10 bg-white/5 px-3 py-1.5 text-xs text-slate-400 hover:bg-white/10 hover:text-white"
>
退出
</button>
</div>
<nav className="hidden max-w-full flex-wrap gap-2 md:flex">
{NAV.map((item) => {
const active = pathname === item.href || (item.href !== "/" && pathname.startsWith(item.href));
return (
<button
key={item.href}
type="button"
onClick={() => navigate(item.href)}
className={`nav-chip rounded-xl px-3 py-1.5 text-sm ${
active
? "active bg-white/15 text-white ring-1 ring-teal-400/30"
: "bg-white/5 text-slate-400 hover:bg-white/10 hover:text-white"
}`}
>
<span className="mr-1.5">{item.emoji}</span>
{item.label}
</button>
);
})}
</nav>
</div>
{/* 手机:顶部横向可滑导航 */}
<nav className="mt-4 flex gap-2 overflow-x-auto pb-1 md:hidden [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
{NAV.map((item) => {
const active = pathname === item.href || (item.href !== "/" && pathname.startsWith(item.href));
return (
<button
key={item.href}
type="button"
onClick={() => navigate(item.href)}
className={`nav-chip shrink-0 rounded-xl px-3 py-1.5 text-sm ${
active
? "active bg-white/15 text-white ring-1 ring-teal-400/30"
: "bg-white/5 text-slate-400"
}`}
>
<span className="mr-1">{item.emoji}</span>
{item.label}
</button>
);
})}
</nav>
</header>
<main className="page-enter flex-1 pb-24 md:pb-10">{children}</main>
<footer className="hidden border-t border-white/5 py-4 text-center text-[11px] text-slate-600 md:block">
Douyin + YouTube Monitor · realtime radar
</footer>
</div>
{/* 手机底部快捷栏 */}
<nav className="fixed inset-x-0 bottom-0 z-40 border-t border-white/10 bg-[#071018]/95 px-2 pb-[env(safe-area-inset-bottom)] pt-1 backdrop-blur-md md:hidden">
<div className="mx-auto flex max-w-lg items-stretch justify-between gap-1">
{NAV.filter((n) => MOBILE_PRIMARY.includes(n.href)).map((item) => {
const active = pathname === item.href || (item.href !== "/" && pathname.startsWith(item.href));
return (
<button
key={item.href}
type="button"
onClick={() => navigate(item.href)}
className={`flex min-w-0 flex-1 flex-col items-center gap-0.5 rounded-lg px-1 py-2 text-[10px] ${
active ? "text-teal-200" : "text-slate-500"
}`}
>
<span className="text-base leading-none">{item.emoji}</span>
<span className="truncate">{item.label}</span>
</button>
);
})}
</div>
</nav>
</div>
);
}