Prefer location.assign for endscreen leave, and ship hub analytics/charts plus host deploy helpers. Co-authored-by: Cursor <cursoragent@cursor.com>
504 lines
21 KiB
TypeScript
504 lines
21 KiB
TypeScript
"use client";
|
||
|
||
import { ExternalLink, Loader2, RefreshCw, Search } from "lucide-react";
|
||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||
|
||
import { LiveYoutubeRelayLogCharts, type RelayStats } from "@/components/live-product/LiveYoutubeRelayLogCharts";
|
||
import { fetchJsonResponse } from "@/lib/fetchJson";
|
||
|
||
type Lang = "zh" | "en";
|
||
|
||
type RelaySession = {
|
||
id: number;
|
||
session_uuid?: string;
|
||
process?: string;
|
||
channel_name?: string;
|
||
anchor_name?: string;
|
||
source_url?: string;
|
||
source_room_id?: string;
|
||
stream_title?: string;
|
||
youtube_key_suffix?: string;
|
||
started_at?: number;
|
||
ended_at?: number | null;
|
||
duration_seconds?: number;
|
||
push_date?: string;
|
||
cover_url?: string;
|
||
status?: string;
|
||
end_reason?: string;
|
||
metadata?: Record<string, unknown>;
|
||
};
|
||
|
||
function tr(lang: Lang, zh: string, en: string) {
|
||
return lang === "zh" ? zh : en;
|
||
}
|
||
|
||
function fmtDuration(seconds: number | null | undefined, lang: Lang): string {
|
||
const total = Math.max(0, Math.floor(Number(seconds) || 0));
|
||
const h = Math.floor(total / 3600);
|
||
const m = Math.floor((total % 3600) / 60);
|
||
const s = total % 60;
|
||
if (h > 0) return `${h}${tr(lang, "小时", "h")}${m}${tr(lang, "分", "m")}${s}${tr(lang, "秒", "s")}`;
|
||
if (m > 0) return `${m}${tr(lang, "分", "m")}${s}${tr(lang, "秒", "s")}`;
|
||
return `${s}${tr(lang, "秒", "s")}`;
|
||
}
|
||
|
||
function fmtTs(ts: number | null | undefined, lang: Lang): string {
|
||
if (!ts) return "—";
|
||
try {
|
||
return new Date(ts * 1000).toLocaleString(lang === "zh" ? "zh-CN" : "en-US", {
|
||
year: "numeric",
|
||
month: "2-digit",
|
||
day: "2-digit",
|
||
hour: "2-digit",
|
||
minute: "2-digit",
|
||
second: "2-digit",
|
||
hour12: false,
|
||
});
|
||
} catch {
|
||
return String(ts);
|
||
}
|
||
}
|
||
|
||
function douyinUrl(session: RelaySession): string {
|
||
if (session.source_url?.includes("live.douyin.com")) return session.source_url;
|
||
if (session.source_room_id) return `https://live.douyin.com/${session.source_room_id}`;
|
||
return session.source_url || "";
|
||
}
|
||
|
||
export function LiveYoutubeRelayLogView() {
|
||
const [lang] = useState<Lang>(() => {
|
||
if (typeof window === "undefined") return "zh";
|
||
return window.localStorage.getItem("live.lang") === "en" ? "en" : "zh";
|
||
});
|
||
const initialProcess =
|
||
typeof window !== "undefined"
|
||
? new URLSearchParams(window.location.search).get("process") || ""
|
||
: "";
|
||
|
||
const [process, setProcess] = useState(initialProcess);
|
||
const [q, setQ] = useState("");
|
||
const [dateFrom, setDateFrom] = useState("");
|
||
const [dateTo, setDateTo] = useState("");
|
||
const [status, setStatus] = useState("");
|
||
const [sessions, setSessions] = useState<RelaySession[]>([]);
|
||
const [stats, setStats] = useState<RelayStats | null>(null);
|
||
const [total, setTotal] = useState(0);
|
||
const [loading, setLoading] = useState(false);
|
||
const [importing, setImporting] = useState(false);
|
||
const [error, setError] = useState("");
|
||
const [selected, setSelected] = useState<RelaySession | null>(null);
|
||
const [offset, setOffset] = useState(0);
|
||
const [tab, setTab] = useState<"charts" | "list">("charts");
|
||
const limit = 50;
|
||
|
||
const t = useCallback((zh: string, en: string) => tr(lang, zh, en), [lang]);
|
||
|
||
const filterParams = useMemo(() => {
|
||
const params = new URLSearchParams();
|
||
if (process.trim()) params.set("process", process.trim());
|
||
if (q.trim()) params.set("q", q.trim());
|
||
if (dateFrom) params.set("date_from", dateFrom);
|
||
if (dateTo) params.set("date_to", dateTo);
|
||
if (status) params.set("status", status);
|
||
return params;
|
||
}, [process, q, dateFrom, dateTo, status]);
|
||
|
||
const queryString = useMemo(() => {
|
||
const params = new URLSearchParams(filterParams);
|
||
params.set("limit", String(limit));
|
||
params.set("offset", String(offset));
|
||
return params.toString();
|
||
}, [filterParams, offset]);
|
||
|
||
const statsQuery = useMemo(() => {
|
||
const params = new URLSearchParams(filterParams);
|
||
params.set("top_anchors", "25");
|
||
return params.toString();
|
||
}, [filterParams]);
|
||
|
||
const loadSessions = useCallback(async () => {
|
||
setLoading(true);
|
||
setError("");
|
||
try {
|
||
const [listData, statsData] = await Promise.all([
|
||
fetchJsonResponse<{ sessions?: RelaySession[]; total?: number }>(
|
||
`/hub/youtube/relay/sessions?${queryString}`,
|
||
),
|
||
fetchJsonResponse<RelayStats>(`/hub/youtube/relay/sessions/stats?${statsQuery}`),
|
||
]);
|
||
setSessions(listData.sessions || []);
|
||
setTotal(Number(listData.total) || 0);
|
||
setStats(statsData || null);
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
}, [queryString, statsQuery]);
|
||
|
||
useEffect(() => {
|
||
void loadSessions();
|
||
}, [loadSessions]);
|
||
|
||
const importLogs = useCallback(async () => {
|
||
setImporting(true);
|
||
setError("");
|
||
try {
|
||
await fetchJsonResponse("/hub/youtube/relay/sessions/import-log", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({
|
||
process: process.trim() || undefined,
|
||
replace_imported: true,
|
||
}),
|
||
});
|
||
setOffset(0);
|
||
await loadSessions();
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
} finally {
|
||
setImporting(false);
|
||
}
|
||
}, [loadSessions, process]);
|
||
|
||
const pageCount = Math.max(1, Math.ceil(total / limit));
|
||
const pageIndex = Math.floor(offset / limit) + 1;
|
||
|
||
return (
|
||
<div className="min-h-screen bg-zinc-950 text-zinc-100">
|
||
<div className="mx-auto max-w-7xl px-4 py-6 sm:px-6">
|
||
<div className="mb-6 flex flex-wrap items-start justify-between gap-4">
|
||
<div>
|
||
<h1 className="text-2xl font-semibold tracking-tight">{t("YouTube 推流日志", "YouTube relay log")}</h1>
|
||
<p className="mt-1 max-w-3xl text-sm text-zinc-400">
|
||
{t(
|
||
"图表展示每位主播的开播频率、时段规律与累计时长;列表保留逐场明细与截图,便于版权追溯。",
|
||
"Charts show per-anchor frequency, time-of-day patterns, and hours; the table keeps per-session detail and covers for tracing.",
|
||
)}
|
||
</p>
|
||
</div>
|
||
<div className="flex flex-wrap gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => void loadSessions()}
|
||
disabled={loading}
|
||
className="inline-flex items-center gap-2 rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm hover:bg-white/10 disabled:opacity-50"
|
||
>
|
||
{loading ? <Loader2 className="h-4 w-4 animate-spin" /> : <RefreshCw className="h-4 w-4" />}
|
||
{t("刷新", "Refresh")}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => void importLogs()}
|
||
disabled={importing}
|
||
className="inline-flex items-center gap-2 rounded-lg border border-amber-500/30 bg-amber-500/10 px-3 py-2 text-sm text-amber-100 hover:bg-amber-500/20 disabled:opacity-50"
|
||
>
|
||
{importing ? <Loader2 className="h-4 w-4 animate-spin" /> : null}
|
||
{t("从 PM2 日志导入历史", "Import history from PM2 log")}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="mb-4 grid gap-3 rounded-xl border border-white/10 bg-black/30 p-4 md:grid-cols-2 xl:grid-cols-6">
|
||
<label className="block text-xs text-zinc-400 xl:col-span-1">
|
||
{t("线路 / 进程", "Lane / process")}
|
||
<input
|
||
value={process}
|
||
onChange={(e) => {
|
||
setOffset(0);
|
||
setProcess(e.target.value);
|
||
}}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-zinc-900 px-3 py-2 text-sm"
|
||
placeholder={t("全部线路(留空)", "All lanes (empty)")}
|
||
/>
|
||
</label>
|
||
<label className="block text-xs text-zinc-400 xl:col-span-2">
|
||
{t("检索(昵称 / URL / 房间号)", "Search nickname / URL / room id")}
|
||
<div className="relative mt-1">
|
||
<Search className="pointer-events-none absolute left-3 top-2.5 h-4 w-4 text-zinc-500" />
|
||
<input
|
||
value={q}
|
||
onChange={(e) => {
|
||
setOffset(0);
|
||
setQ(e.target.value);
|
||
}}
|
||
className="w-full rounded-lg border border-white/10 bg-zinc-900 py-2 pl-9 pr-3 text-sm"
|
||
placeholder={t("例如:柠檬茶、745277409780", "e.g. lemon tea, room id")}
|
||
/>
|
||
</div>
|
||
</label>
|
||
<label className="block text-xs text-zinc-400">
|
||
{t("开始日期", "From date")}
|
||
<input
|
||
type="date"
|
||
value={dateFrom}
|
||
onChange={(e) => {
|
||
setOffset(0);
|
||
setDateFrom(e.target.value);
|
||
}}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-zinc-900 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="block text-xs text-zinc-400">
|
||
{t("结束日期", "To date")}
|
||
<input
|
||
type="date"
|
||
value={dateTo}
|
||
onChange={(e) => {
|
||
setOffset(0);
|
||
setDateTo(e.target.value);
|
||
}}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-zinc-900 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="block text-xs text-zinc-400">
|
||
{t("状态", "Status")}
|
||
<select
|
||
value={status}
|
||
onChange={(e) => {
|
||
setOffset(0);
|
||
setStatus(e.target.value);
|
||
}}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-zinc-900 px-3 py-2 text-sm"
|
||
>
|
||
<option value="">{t("全部", "All")}</option>
|
||
<option value="active">{t("进行中", "Active")}</option>
|
||
<option value="ended">{t("已结束", "Ended")}</option>
|
||
</select>
|
||
</label>
|
||
</div>
|
||
|
||
{error ? (
|
||
<div className="mb-4 rounded-lg border border-rose-500/30 bg-rose-500/10 px-4 py-3 text-sm text-rose-100">{error}</div>
|
||
) : null}
|
||
|
||
<div className="mb-4 flex gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => setTab("charts")}
|
||
className={`rounded-lg px-3 py-1.5 text-sm ${
|
||
tab === "charts" ? "bg-white/15 text-white" : "bg-white/5 text-zinc-400 hover:bg-white/10"
|
||
}`}
|
||
>
|
||
{t("可视化分析", "Analytics")}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => setTab("list")}
|
||
className={`rounded-lg px-3 py-1.5 text-sm ${
|
||
tab === "list" ? "bg-white/15 text-white" : "bg-white/5 text-zinc-400 hover:bg-white/10"
|
||
}`}
|
||
>
|
||
{t("场次列表", "Session list")} ({total})
|
||
</button>
|
||
</div>
|
||
|
||
{tab === "charts" ? <LiveYoutubeRelayLogCharts stats={stats} lang={lang} t={t} /> : null}
|
||
|
||
{tab === "list" ? (
|
||
<>
|
||
<div className="overflow-hidden rounded-xl border border-white/10 bg-black/20">
|
||
<div className="overflow-x-auto">
|
||
<table className="min-w-full text-left text-sm">
|
||
<thead className="bg-white/5 text-xs uppercase tracking-wide text-zinc-400">
|
||
<tr>
|
||
<th className="px-3 py-3">{t("截图", "Cover")}</th>
|
||
<th className="px-3 py-3">{t("主播昵称", "Anchor")}</th>
|
||
<th className="px-3 py-3">{t("直播间 URL", "Room URL")}</th>
|
||
<th className="px-3 py-3">{t("推流开始", "Started")}</th>
|
||
<th className="px-3 py-3">{t("推流结束", "Ended")}</th>
|
||
<th className="px-3 py-3">{t("总时长", "Duration")}</th>
|
||
<th className="px-3 py-3">{t("推流日期", "Date")}</th>
|
||
<th className="px-3 py-3">{t("YT 密钥", "YT key")}</th>
|
||
<th className="px-3 py-3">{t("线路", "Lane")}</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{sessions.map((session) => {
|
||
const url = douyinUrl(session);
|
||
return (
|
||
<tr
|
||
key={session.id}
|
||
className="cursor-pointer border-t border-white/5 hover:bg-white/[0.03]"
|
||
onClick={() => setSelected(session)}
|
||
>
|
||
<td className="px-3 py-3">
|
||
{session.cover_url ? (
|
||
// eslint-disable-next-line @next/next/no-img-element
|
||
<img
|
||
src={`${session.cover_url}?ts=${session.started_at || 0}`}
|
||
alt={session.anchor_name || "cover"}
|
||
className="h-16 w-10 rounded object-cover ring-1 ring-white/10"
|
||
/>
|
||
) : (
|
||
<div className="flex h-16 w-10 items-center justify-center rounded bg-zinc-900 text-[10px] text-zinc-500 ring-1 ring-white/10">
|
||
N/A
|
||
</div>
|
||
)}
|
||
</td>
|
||
<td className="px-3 py-3 font-medium text-zinc-100">{session.anchor_name || "—"}</td>
|
||
<td className="max-w-[14rem] truncate px-3 py-3 font-mono text-xs text-cyan-300/90">
|
||
{url ? (
|
||
<a
|
||
href={url}
|
||
target="_blank"
|
||
rel="noreferrer"
|
||
className="hover:underline"
|
||
onClick={(e) => e.stopPropagation()}
|
||
>
|
||
{url.replace(/^https?:\/\//, "")}
|
||
</a>
|
||
) : (
|
||
"—"
|
||
)}
|
||
</td>
|
||
<td className="whitespace-nowrap px-3 py-3 text-zinc-300">{fmtTs(session.started_at, lang)}</td>
|
||
<td className="whitespace-nowrap px-3 py-3 text-zinc-300">
|
||
{session.ended_at ? fmtTs(session.ended_at, lang) : t("进行中", "Active")}
|
||
</td>
|
||
<td className="whitespace-nowrap px-3 py-3 font-mono text-zinc-200">
|
||
{fmtDuration(session.duration_seconds, lang)}
|
||
</td>
|
||
<td className="whitespace-nowrap px-3 py-3">{session.push_date || "—"}</td>
|
||
<td className="whitespace-nowrap px-3 py-3 font-mono text-xs text-amber-200/90">
|
||
{session.youtube_key_suffix ? `…${session.youtube_key_suffix}` : "—"}
|
||
</td>
|
||
<td className="px-3 py-3 text-xs text-zinc-400">
|
||
{session.channel_name || session.process || "—"}
|
||
</td>
|
||
</tr>
|
||
);
|
||
})}
|
||
{!loading && sessions.length === 0 ? (
|
||
<tr>
|
||
<td colSpan={9} className="px-3 py-10 text-center text-zinc-500">
|
||
{t(
|
||
"暂无推流记录。可先点击「从 PM2 日志导入历史」。",
|
||
"No relay sessions yet. Try importing PM2 logs first.",
|
||
)}
|
||
</td>
|
||
</tr>
|
||
) : null}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="mt-4 flex items-center justify-between text-sm text-zinc-400">
|
||
<span>
|
||
{t("共", "Total")} {total} {t("条", "records")} · {t("第", "Page")} {pageIndex}/{pageCount}
|
||
</span>
|
||
<div className="flex gap-2">
|
||
<button
|
||
type="button"
|
||
disabled={offset <= 0 || loading}
|
||
onClick={() => setOffset(Math.max(0, offset - limit))}
|
||
className="rounded-lg border border-white/10 px-3 py-1.5 disabled:opacity-40"
|
||
>
|
||
{t("上一页", "Prev")}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
disabled={offset + limit >= total || loading}
|
||
onClick={() => setOffset(offset + limit)}
|
||
className="rounded-lg border border-white/10 px-3 py-1.5 disabled:opacity-40"
|
||
>
|
||
{t("下一页", "Next")}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</>
|
||
) : null}
|
||
</div>
|
||
|
||
{selected ? (
|
||
<div
|
||
className="fixed inset-0 z-50 flex items-center justify-center bg-black/70 p-4"
|
||
onClick={() => setSelected(null)}
|
||
>
|
||
<div
|
||
className="max-h-[90vh] w-full max-w-2xl overflow-y-auto rounded-2xl border border-white/10 bg-zinc-950 p-5 shadow-2xl"
|
||
onClick={(e) => e.stopPropagation()}
|
||
>
|
||
<div className="flex items-start justify-between gap-3">
|
||
<div>
|
||
<h2 className="text-lg font-semibold">{selected.anchor_name || t("推流详情", "Relay detail")}</h2>
|
||
<p className="mt-1 text-sm text-zinc-400">{selected.stream_title || "—"}</p>
|
||
</div>
|
||
<button type="button" className="text-zinc-400 hover:text-zinc-200" onClick={() => setSelected(null)}>
|
||
✕
|
||
</button>
|
||
</div>
|
||
<div className="mt-4 grid gap-4 md:grid-cols-[8rem_1fr]">
|
||
{selected.cover_url ? (
|
||
// eslint-disable-next-line @next/next/no-img-element
|
||
<img
|
||
src={`${selected.cover_url}?ts=${selected.started_at || 0}`}
|
||
alt={selected.anchor_name || "cover"}
|
||
className="h-36 w-full rounded-lg object-cover ring-1 ring-white/10"
|
||
/>
|
||
) : null}
|
||
<dl className="grid gap-2 text-sm">
|
||
<div>
|
||
<dt className="text-zinc-500">{t("直播间 URL", "Room URL")}</dt>
|
||
<dd className="break-all font-mono text-cyan-300">
|
||
{douyinUrl(selected) ? (
|
||
<a
|
||
href={douyinUrl(selected)}
|
||
target="_blank"
|
||
rel="noreferrer"
|
||
className="inline-flex items-center gap-1 hover:underline"
|
||
>
|
||
{douyinUrl(selected)} <ExternalLink className="h-3.5 w-3.5" />
|
||
</a>
|
||
) : (
|
||
"—"
|
||
)}
|
||
</dd>
|
||
</div>
|
||
<div>
|
||
<dt className="text-zinc-500">{t("房间号", "Room id")}</dt>
|
||
<dd>{selected.source_room_id || "—"}</dd>
|
||
</div>
|
||
<div>
|
||
<dt className="text-zinc-500">{t("推流开始", "Started")}</dt>
|
||
<dd>{fmtTs(selected.started_at, lang)}</dd>
|
||
</div>
|
||
<div>
|
||
<dt className="text-zinc-500">{t("推流结束", "Ended")}</dt>
|
||
<dd>{selected.ended_at ? fmtTs(selected.ended_at, lang) : t("进行中", "Active")}</dd>
|
||
</div>
|
||
<div>
|
||
<dt className="text-zinc-500">{t("总时长", "Duration")}</dt>
|
||
<dd>{fmtDuration(selected.duration_seconds, lang)}</dd>
|
||
</div>
|
||
<div>
|
||
<dt className="text-zinc-500">{t("推流日期", "Push date")}</dt>
|
||
<dd>{selected.push_date || "—"}</dd>
|
||
</div>
|
||
<div>
|
||
<dt className="text-zinc-500">{t("线路 / 进程", "Lane / process")}</dt>
|
||
<dd>
|
||
{selected.channel_name ? `${selected.channel_name} · ` : ""}
|
||
{selected.process || "—"}
|
||
</dd>
|
||
</div>
|
||
<div>
|
||
<dt className="text-zinc-500">{t("结束原因", "End reason")}</dt>
|
||
<dd>{selected.end_reason || "—"}</dd>
|
||
</div>
|
||
<div>
|
||
<dt className="text-zinc-500">{t("YouTube 推流密钥(后6位)", "YouTube stream key (last 6)")}</dt>
|
||
<dd className="font-mono">{selected.youtube_key_suffix ? `…${selected.youtube_key_suffix}` : "—"}</dd>
|
||
</div>
|
||
</dl>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
) : null}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export default LiveYoutubeRelayLogView;
|