1356 lines
56 KiB
TypeScript
1356 lines
56 KiB
TypeScript
import { useCallback, useEffect, useMemo, useState } from "react";
|
||
import { Link } from "react-router-dom";
|
||
import {
|
||
Clock3,
|
||
Eye,
|
||
KeyRound,
|
||
Loader2,
|
||
Plus,
|
||
RefreshCw,
|
||
RotateCcw,
|
||
Trash2,
|
||
Upload,
|
||
Video,
|
||
Wrench,
|
||
} from "lucide-react";
|
||
|
||
import { Shell } from "@/components/Shell";
|
||
import { useConfirm } from "@/components/ConfirmDialog";
|
||
import { useToast } from "@/components/Toast";
|
||
import { GoogleOauthSetupWizard } from "@/components/GoogleOauthSetupWizard";
|
||
import { ShortsJobDetailModal } from "@/components/ShortsJobDetailModal";
|
||
import {
|
||
ShortsUploadConfirmModal,
|
||
ShortsUpcomingUploadRow,
|
||
useShortsUpload,
|
||
type ShortsUploadTarget,
|
||
} from "@/components/ShortsUploadUx";
|
||
import { YouTubeLiveBadge } from "@/components/YouTubeLiveBadge";
|
||
import { SessionCharts, type RelayStats } from "@/components/SessionCharts";
|
||
import { PageLoader, TableSkeleton } from "@/components/ui";
|
||
import { fetchJson, fmtDuration, fmtTs, todayBeijingISO, shiftBeijingISO } from "@/lib/api";
|
||
import { shortsLiveRef } from "@/lib/youtubeLive";
|
||
|
||
type Channel = {
|
||
id: string;
|
||
name: string;
|
||
youtube_channel_id?: string;
|
||
enabled?: boolean;
|
||
daily_min?: number;
|
||
daily_max?: number;
|
||
title_template?: string;
|
||
description_template?: string;
|
||
privacy?: string;
|
||
note?: string;
|
||
authorized?: boolean;
|
||
has_refresh_token?: boolean;
|
||
};
|
||
|
||
type BoardChannel = {
|
||
id: string;
|
||
name?: string;
|
||
youtube_channel_id?: string;
|
||
enabled?: boolean;
|
||
daily_min: number;
|
||
daily_max: number;
|
||
authorized?: boolean;
|
||
used: number;
|
||
scheduled: number;
|
||
uploaded: number;
|
||
pending: number;
|
||
gap?: number;
|
||
min_met?: boolean;
|
||
max_met?: boolean;
|
||
behind_pace?: boolean;
|
||
};
|
||
|
||
type Job = {
|
||
id: string;
|
||
status: string;
|
||
anchor_name?: string;
|
||
room_id?: string;
|
||
push_date?: string;
|
||
channel_pk?: string;
|
||
clip_path?: string;
|
||
clip_ready?: boolean;
|
||
youtube_url?: string;
|
||
error?: string;
|
||
created_at?: number;
|
||
upload_at?: number;
|
||
};
|
||
|
||
const emptyForm = {
|
||
name: "",
|
||
enabled: true,
|
||
daily_min: 9,
|
||
daily_max: 9,
|
||
title_template: "{anchor} 直播瞬间 #Shorts",
|
||
description_template: "{anchor}\n{url}\n#Shorts #直播",
|
||
privacy: "public",
|
||
note: "",
|
||
};
|
||
|
||
const STATUS_STYLE: Record<string, string> = {
|
||
pending: "bg-slate-500/20 text-slate-200",
|
||
recording: "bg-sky-500/20 text-sky-100",
|
||
recorded: "bg-indigo-500/20 text-indigo-100",
|
||
scheduled: "bg-amber-500/20 text-amber-100",
|
||
uploading: "bg-violet-500/20 text-violet-100",
|
||
uploaded: "bg-teal-500/20 text-teal-100",
|
||
failed: "bg-rose-500/20 text-rose-100",
|
||
skipped: "bg-white/10 text-slate-400",
|
||
};
|
||
|
||
const JOB_PAGE = 50;
|
||
|
||
export default function ShortsPage() {
|
||
const confirm = useConfirm();
|
||
const toast = useToast();
|
||
const [pageTab, setPageTab] = useState<"ops" | "live">("ops");
|
||
const [status, setStatus] = useState<Record<string, any> | null>(null);
|
||
const [channels, setChannels] = useState<Channel[]>([]);
|
||
const [jobs, setJobs] = useState<Job[]>([]);
|
||
const [jobTotal, setJobTotal] = useState(0);
|
||
const [form, setForm] = useState({ ...emptyForm });
|
||
const [editId, setEditId] = useState("");
|
||
const [busy, setBusy] = useState(false);
|
||
const [msg, setMsg] = useState("");
|
||
const [error, setError] = useState("");
|
||
const [jobFilter, setJobFilter] = useState("");
|
||
const [jobChannel, setJobChannel] = useState("");
|
||
const [liveStats, setLiveStats] = useState<RelayStats | null>(null);
|
||
const [liveChannels, setLiveChannels] = useState<any[]>([]);
|
||
const [liveRecent, setLiveRecent] = useState<any[]>([]);
|
||
const [liveNow, setLiveNow] = useState(0);
|
||
const [liveBusy, setLiveBusy] = useState(false);
|
||
const [liveFrom, setLiveFrom] = useState(() => shiftBeijingISO(todayBeijingISO(), -29));
|
||
const [liveTo, setLiveTo] = useState(() => todayBeijingISO());
|
||
const [jobDate, setJobDate] = useState("");
|
||
const [selectedIds, setSelectedIds] = useState<Record<string, boolean>>({});
|
||
const [detailJobId, setDetailJobId] = useState<string | null>(null);
|
||
const [showWizard, setShowWizard] = useState(false);
|
||
const [showForm, setShowForm] = useState(false);
|
||
const [loading, setLoading] = useState(true);
|
||
const [loadingMoreJobs, setLoadingMoreJobs] = useState(false);
|
||
|
||
const load = useCallback(
|
||
async (opts?: { appendJobs?: boolean; jobOffset?: number }) => {
|
||
const appendJobs = Boolean(opts?.appendJobs);
|
||
if (appendJobs) setLoadingMoreJobs(true);
|
||
else setLoading(true);
|
||
try {
|
||
const off = appendJobs ? Number(opts?.jobOffset ?? 0) : 0;
|
||
const jobQs = new URLSearchParams({ limit: String(JOB_PAGE), offset: String(off) });
|
||
if (jobFilter) jobQs.set("status", jobFilter);
|
||
if (jobChannel) jobQs.set("channel_pk", jobChannel);
|
||
if (jobDate) jobQs.set("push_date", jobDate);
|
||
if (appendJobs) {
|
||
const jb = await fetchJson(`/api/shorts/jobs?${jobQs}`);
|
||
const rows = jb.jobs || [];
|
||
setJobs((prev) => [...prev, ...rows]);
|
||
setJobTotal(jb.total || 0);
|
||
} else {
|
||
const [st, ch, jb] = await Promise.all([
|
||
fetchJson("/api/shorts/status"),
|
||
fetchJson("/api/shorts/channels"),
|
||
fetchJson(`/api/shorts/jobs?${jobQs}`),
|
||
]);
|
||
setStatus(st);
|
||
setChannels(ch.channels || []);
|
||
setJobs(jb.jobs || []);
|
||
setJobTotal(jb.total || 0);
|
||
setSelectedIds({});
|
||
if (!st?.oauth_configured) setShowWizard(true);
|
||
}
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
toast.push((e as Error).message, "err");
|
||
} finally {
|
||
setLoading(false);
|
||
setLoadingMoreJobs(false);
|
||
}
|
||
},
|
||
[jobChannel, jobDate, jobFilter, toast],
|
||
);
|
||
|
||
const uploadUx = useShortsUpload(load);
|
||
|
||
const loadLiveAnalytics = useCallback(async () => {
|
||
setLiveBusy(true);
|
||
try {
|
||
const p = new URLSearchParams({ top_anchors: "25" });
|
||
if (liveFrom) p.set("date_from", liveFrom);
|
||
if (liveTo) p.set("date_to", liveTo);
|
||
const data = await fetchJson(`/api/shorts/live-analytics?${p}`);
|
||
setLiveStats((data.stats || null) as RelayStats | null);
|
||
setLiveChannels(data.channels || []);
|
||
setLiveRecent(data.recent_sessions || []);
|
||
setLiveNow(Number(data.live_now || 0));
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
} finally {
|
||
setLiveBusy(false);
|
||
}
|
||
}, [liveFrom, liveTo]);
|
||
|
||
useEffect(() => {
|
||
void load();
|
||
const t = setInterval(() => {
|
||
if (pageTab === "ops") void load();
|
||
}, 10000);
|
||
return () => clearInterval(t);
|
||
}, [load, pageTab]);
|
||
|
||
useEffect(() => {
|
||
if (pageTab !== "live") return;
|
||
void loadLiveAnalytics();
|
||
const t = setInterval(() => void loadLiveAnalytics(), 20000);
|
||
return () => clearInterval(t);
|
||
}, [pageTab, loadLiveAnalytics]);
|
||
|
||
useEffect(() => {
|
||
if (typeof window === "undefined") return;
|
||
const sp = new URLSearchParams(window.location.search);
|
||
if (sp.get("tab") === "live") setPageTab("live");
|
||
const oauth = sp.get("oauth");
|
||
if (!oauth) return;
|
||
if (oauth === "ok") {
|
||
setMsg(`Google 授权成功${sp.get("title") ? `:${sp.get("title")}` : ""}`);
|
||
void load();
|
||
} else if (oauth === "error") {
|
||
setError(sp.get("msg") || "授权失败");
|
||
}
|
||
window.history.replaceState({}, "", "/shorts");
|
||
}, [load]);
|
||
|
||
const boardChannels: BoardChannel[] = status?.board?.channels || [];
|
||
const upcoming = status?.board?.upcoming || [];
|
||
const beijingWindow = status?.board?.beijing_window || status?.stats?.push_date || "";
|
||
const quotaGap = Number(status?.quota_gap ?? status?.board?.total_gap ?? 0);
|
||
const minUnmet = Number(status?.board?.min_unmet_channels ?? 0);
|
||
const secondsLeft = Number(status?.board?.seconds_left ?? 0);
|
||
const stats = status?.stats?.by_status || {};
|
||
const channelName = useMemo(() => {
|
||
const m: Record<string, string> = {};
|
||
for (const c of channels) m[c.id] = c.name;
|
||
for (const c of boardChannels) if (c.name) m[c.id] = c.name;
|
||
return m;
|
||
}, [channels, boardChannels]);
|
||
|
||
const totals = useMemo(() => {
|
||
const uploaded = boardChannels.reduce((s, c) => s + (c.uploaded || 0), 0);
|
||
const scheduled = boardChannels.reduce((s, c) => s + (c.scheduled || 0), 0);
|
||
const used = boardChannels.reduce((s, c) => s + (c.used || 0), 0);
|
||
const cap = boardChannels.reduce((s, c) => s + (c.daily_max || 9), 0);
|
||
const minTarget = boardChannels.filter((c) => c.enabled !== false).reduce((s, c) => s + (c.daily_min || 9), 0);
|
||
const gap = boardChannels.filter((c) => c.enabled !== false).reduce((s, c) => s + (c.gap ?? Math.max(0, (c.daily_max || 9) - (c.used || 0))), 0);
|
||
return { uploaded, scheduled, used, cap, minTarget, gap };
|
||
}, [boardChannels]);
|
||
|
||
const hoursLeft = secondsLeft > 0 ? `${Math.floor(secondsLeft / 3600)}h${Math.floor((secondsLeft % 3600) / 60)}m` : "—";
|
||
const behindChannels = boardChannels.filter((c) => c.enabled !== false && c.behind_pace);
|
||
const oauthIssues = (status?.oauth_issues || []) as Array<{
|
||
id: string;
|
||
name?: string;
|
||
oauth_ok?: number;
|
||
oauth_error?: string;
|
||
}>;
|
||
|
||
async function probeOauth() {
|
||
setBusy(true);
|
||
try {
|
||
const r = await fetchJson("/api/shorts/oauth/probe", { method: "POST" });
|
||
const failed = Number(r.failed ?? 0);
|
||
toast.push(
|
||
failed > 0 ? `OAuth 探测:${failed} 个频道异常` : `OAuth 探测完成:${r.checked ?? 0} 个频道正常`,
|
||
failed > 0 ? "warn" : "ok",
|
||
);
|
||
await load();
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "err");
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
async function toggle(enabled: boolean) {
|
||
setBusy(true);
|
||
setMsg("");
|
||
try {
|
||
await fetchJson(enabled ? "/api/shorts/start" : "/api/shorts/stop", { method: "POST" });
|
||
setMsg(enabled ? "已启用 Shorts(监控业务不变)" : "已关闭 Shorts");
|
||
await load();
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
function goAuthorize(channelId: string) {
|
||
if (!status?.oauth_configured) {
|
||
setShowWizard(true);
|
||
setError("请先配置 Google OAuth Client");
|
||
return;
|
||
}
|
||
const start = `/api/shorts/oauth/start?channel_pk=${encodeURIComponent(channelId)}`;
|
||
if (window.desktop?.openExternal) {
|
||
const origin = window.location.origin;
|
||
void window.desktop.openExternal(`${origin}${start}`);
|
||
} else {
|
||
window.location.href = start;
|
||
}
|
||
}
|
||
|
||
async function saveChannel(andAuth: boolean) {
|
||
setBusy(true);
|
||
setMsg("");
|
||
setError("");
|
||
try {
|
||
const body = { ...form, daily_min: 9, daily_max: 9 };
|
||
if (!body.name.trim()) body.name = "未命名频道";
|
||
let id = editId;
|
||
if (editId) {
|
||
await fetchJson(`/api/shorts/channels/${editId}`, { method: "PATCH", body: JSON.stringify(body) });
|
||
setMsg("频道已更新");
|
||
} else {
|
||
const created = await fetchJson("/api/shorts/channels", { method: "POST", body: JSON.stringify(body) });
|
||
id = created.channel?.id || "";
|
||
setMsg("频道已创建");
|
||
}
|
||
setForm({ ...emptyForm });
|
||
setEditId("");
|
||
setShowForm(false);
|
||
await load();
|
||
if (andAuth && id) {
|
||
goAuthorize(id);
|
||
return;
|
||
}
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
function startEdit(ch: Channel) {
|
||
setEditId(ch.id);
|
||
setShowForm(true);
|
||
setForm({
|
||
name: ch.name || "",
|
||
enabled: ch.enabled !== false,
|
||
daily_min: 9,
|
||
daily_max: 9,
|
||
title_template: ch.title_template || "{anchor} 直播瞬间 #Shorts",
|
||
description_template: ch.description_template || "",
|
||
privacy: ch.privacy || "public",
|
||
note: ch.note || "",
|
||
});
|
||
}
|
||
|
||
async function removeChannel(id: string) {
|
||
const ok = await confirm({
|
||
title: "删除 Shorts 频道",
|
||
message: "确定删除该频道?已排程任务不会自动恢复。",
|
||
confirmLabel: "删除",
|
||
destructive: true,
|
||
});
|
||
if (!ok) return;
|
||
await fetchJson(`/api/shorts/channels/${id}`, { method: "DELETE" });
|
||
toast.push("频道已删除", "ok");
|
||
await load();
|
||
}
|
||
|
||
async function retryJob(id: string) {
|
||
await fetchJson(`/api/shorts/jobs/${id}/retry`, { method: "POST" });
|
||
toast.push("已重新入队", "ok");
|
||
await load();
|
||
}
|
||
|
||
async function runReconcile() {
|
||
setBusy(true);
|
||
try {
|
||
const r = await fetchJson("/api/shorts/reconcile", { method: "POST" });
|
||
toast.push(
|
||
`修复完成:丢失成片 ${r.missing_clips ?? 0} · 误标失败 ${r.false_failed ?? 0} · 卡死上传 ${r.stale_uploads ?? 0}`,
|
||
"ok",
|
||
);
|
||
await load();
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "err");
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
async function bulkAction(action: "retry" | "upload_now") {
|
||
const ids = Object.keys(selectedIds).filter((id) => selectedIds[id]);
|
||
if (!ids.length) {
|
||
toast.push("请先勾选任务", "warn");
|
||
return;
|
||
}
|
||
if (action === "upload_now") {
|
||
const ok = await confirm({
|
||
title: "批量立即上传",
|
||
message: `将对 ${ids.length} 条已成片任务跳过错峰立即上传(最多 30 条)。`,
|
||
confirmLabel: "开始上传",
|
||
});
|
||
if (!ok) return;
|
||
}
|
||
setBusy(true);
|
||
try {
|
||
const r = await fetchJson("/api/shorts/jobs/bulk", {
|
||
method: "POST",
|
||
body: JSON.stringify({ job_ids: ids, action }),
|
||
});
|
||
toast.push(`完成 ${r.ok_count ?? 0}/${r.total ?? ids.length}`, r.ok_count ? "ok" : "warn");
|
||
setSelectedIds({});
|
||
await load();
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "err");
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
const selectedCount = useMemo(() => Object.values(selectedIds).filter(Boolean).length, [selectedIds]);
|
||
|
||
function toggleSelect(id: string) {
|
||
setSelectedIds((prev) => ({ ...prev, [id]: !prev[id] }));
|
||
}
|
||
|
||
function toggleSelectAll() {
|
||
const allOn = jobs.length > 0 && jobs.every((j) => selectedIds[j.id]);
|
||
if (allOn) {
|
||
setSelectedIds({});
|
||
return;
|
||
}
|
||
const next: Record<string, boolean> = {};
|
||
for (const j of jobs) next[j.id] = true;
|
||
setSelectedIds(next);
|
||
}
|
||
|
||
function toUploadTarget(u: Record<string, unknown>, channelMap: Record<string, string>): ShortsUploadTarget {
|
||
return {
|
||
id: String(u.id || ""),
|
||
anchor_name: String(u.anchor_name || ""),
|
||
room_id: String(u.room_id || ""),
|
||
channel_name: String(u.channel_name || channelMap[String(u.channel_pk || "")] || ""),
|
||
channel_pk: String(u.channel_pk || ""),
|
||
upload_at: Number(u.upload_at || 0) || undefined,
|
||
clip_ready: u.clip_ready !== false,
|
||
status: String(u.status || ""),
|
||
};
|
||
}
|
||
|
||
const uploadBusy = Object.values(uploadUx.tracks).some((t) => t.phase === "uploading");
|
||
|
||
return (
|
||
<Shell>
|
||
<ShortsUploadConfirmModal
|
||
target={uploadUx.confirmTarget}
|
||
onCancel={uploadUx.cancelConfirm}
|
||
onConfirm={uploadUx.confirmUpload}
|
||
busy={uploadBusy}
|
||
/>
|
||
<ShortsJobDetailModal jobId={detailJobId} onClose={() => setDetailJobId(null)} />
|
||
<div className="mb-5 flex flex-wrap items-end justify-between gap-3">
|
||
<div>
|
||
<p className="font-display text-xs uppercase tracking-[0.28em] text-amber-300/80">Shorts Pipeline</p>
|
||
<h2 className="font-display text-3xl font-semibold tracking-tight">📱 Shorts 分发台</h2>
|
||
<p className="mt-1 max-w-2xl text-sm text-slate-400">
|
||
在播放样 → 错峰上传;同时轮询各上传频道自身 YouTube 开播,写入场次做数据分析。
|
||
</p>
|
||
</div>
|
||
<div className="flex flex-wrap gap-2">
|
||
<Link to="/sessions?platform=shorts" className="rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm hover:bg-white/10">
|
||
开播场次
|
||
</Link>
|
||
<Link to="/settings" className="rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm hover:bg-white/10">
|
||
设置
|
||
</Link>
|
||
<button
|
||
type="button"
|
||
onClick={() => setShowWizard((v) => !v)}
|
||
className="rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm hover:bg-white/10"
|
||
>
|
||
OAuth {showWizard ? "收起" : "引导"}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => void load()}
|
||
className="inline-flex items-center gap-1 rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm hover:bg-white/10"
|
||
>
|
||
<RefreshCw className="h-3.5 w-3.5" />
|
||
刷新
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="mb-4 flex flex-wrap gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => setPageTab("ops")}
|
||
className={`rounded-xl px-4 py-2 text-sm ${
|
||
pageTab === "ops" ? "bg-amber-500/20 text-amber-50 ring-1 ring-amber-400/30" : "bg-white/5 text-slate-400"
|
||
}`}
|
||
>
|
||
分发作业
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => setPageTab("live")}
|
||
className={`rounded-xl px-4 py-2 text-sm ${
|
||
pageTab === "live" ? "bg-rose-500/20 text-rose-50 ring-1 ring-rose-400/30" : "bg-white/5 text-slate-400"
|
||
}`}
|
||
>
|
||
频道直播分析 {liveNow > 0 ? `(${liveNow} 在播)` : ""}
|
||
</button>
|
||
</div>
|
||
|
||
{showWizard || !status?.oauth_configured ? (
|
||
<GoogleOauthSetupWizard
|
||
initialProjectId={String(status?.gcp_project_id || "live-473104")}
|
||
redirectUri={String(status?.oauth_redirect_uri || "https://dylive.nomadro.com/api/shorts/oauth/callback")}
|
||
configured={Boolean(status?.oauth_configured)}
|
||
onSaved={() => void load()}
|
||
/>
|
||
) : null}
|
||
|
||
{pageTab === "live" ? (
|
||
<div className="space-y-4">
|
||
<div className="flex flex-wrap items-end gap-2 rounded-xl border border-white/10 bg-black/25 p-3">
|
||
<label className="text-xs text-slate-400">
|
||
起
|
||
<input
|
||
type="date"
|
||
value={liveFrom}
|
||
onChange={(e) => setLiveFrom(e.target.value)}
|
||
className="mt-1 block rounded-lg border border-white/10 bg-slate-900 px-2 py-1.5 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="text-xs text-slate-400">
|
||
止
|
||
<input
|
||
type="date"
|
||
value={liveTo}
|
||
onChange={(e) => setLiveTo(e.target.value)}
|
||
className="mt-1 block rounded-lg border border-white/10 bg-slate-900 px-2 py-1.5 text-sm"
|
||
/>
|
||
</label>
|
||
<button
|
||
type="button"
|
||
onClick={() => void loadLiveAnalytics()}
|
||
className="rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm"
|
||
>
|
||
{liveBusy ? <Loader2 className="inline h-3.5 w-3.5 animate-spin" /> : null} 刷新
|
||
</button>
|
||
<span className="text-xs text-slate-500">
|
||
基于 live-map 轮询写入的 Shorts 频道开播场次(room_id=shorts:…),与抖音场次同一套图表口径
|
||
</span>
|
||
</div>
|
||
|
||
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
|
||
{[
|
||
["当前在播", String(liveNow), "上传频道自身开播"],
|
||
["场次", String(liveStats?.overview?.sessions ?? 0), "区间内开播次数"],
|
||
["累计小时", Number(liveStats?.overview?.total_hours || 0).toFixed(1), "含进行中"],
|
||
["有记录频道", String(liveStats?.overview?.unique_anchors ?? liveChannels.length), "开过播的频道数"],
|
||
].map(([label, value, hint]) => (
|
||
<div key={label as string} className="rounded-2xl border border-white/10 bg-black/30 px-4 py-3">
|
||
<div className="text-xs text-slate-500">{label}</div>
|
||
<div className="mt-1 font-display text-2xl text-white">{value}</div>
|
||
<div className="mt-1 text-[11px] text-slate-500">{hint}</div>
|
||
</div>
|
||
))}
|
||
</div>
|
||
|
||
<section className="rounded-2xl border border-white/10 bg-black/25 p-4">
|
||
<h3 className="mb-3 text-sm font-medium text-slate-100">可视化分析</h3>
|
||
<SessionCharts stats={liveStats} />
|
||
</section>
|
||
|
||
<section className="overflow-hidden rounded-2xl border border-white/10 bg-black/25">
|
||
<div className="border-b border-white/5 px-4 py-3 text-sm font-medium text-slate-100">频道开播排行</div>
|
||
<div className="overflow-x-auto">
|
||
<table className="min-w-full text-left text-sm">
|
||
<thead className="bg-white/5 text-xs uppercase text-slate-400">
|
||
<tr>
|
||
<th className="px-3 py-2">频道</th>
|
||
<th className="px-3 py-2">状态</th>
|
||
<th className="px-3 py-2">场次</th>
|
||
<th className="px-3 py-2">小时</th>
|
||
<th className="px-3 py-2">最近开播</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{liveChannels.map((ch) => (
|
||
<tr key={String(ch.id || ch.room_id)} className="border-t border-white/5">
|
||
<td className="px-3 py-2">
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<span className="font-medium text-slate-100">{ch.name || "—"}</span>
|
||
{ch.id ? <YouTubeLiveBadge refKey={shortsLiveRef(ch.id)} compact showOffline /> : null}
|
||
</div>
|
||
<div className="font-mono text-[10px] text-slate-500">{ch.youtube_channel_id || "—"}</div>
|
||
{ch.last_title ? <div className="mt-0.5 max-w-[280px] truncate text-[11px] text-slate-500">{ch.last_title}</div> : null}
|
||
</td>
|
||
<td className="px-3 py-2">
|
||
<span className={Number(ch.active) > 0 || ch.last_status === "live" ? "text-rose-300" : "text-slate-500"}>
|
||
{Number(ch.active) > 0 || ch.last_status === "live" ? "LIVE" : ch.last_status || "offline"}
|
||
</span>
|
||
</td>
|
||
<td className="px-3 py-2 tabular-nums">{ch.sessions ?? 0}</td>
|
||
<td className="px-3 py-2 tabular-nums">{Number(ch.hours_total || 0).toFixed(1)}</td>
|
||
<td className="px-3 py-2 text-slate-400">{ch.last_started_at ? fmtTs(ch.last_started_at) : "—"}</td>
|
||
</tr>
|
||
))}
|
||
{!liveChannels.length ? (
|
||
<tr>
|
||
<td colSpan={5} className="px-3 py-8 text-center text-slate-500">
|
||
暂无开播记录。轮询检测到频道 YouTube 开播后会自动入库。
|
||
</td>
|
||
</tr>
|
||
) : null}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</section>
|
||
|
||
<section className="overflow-hidden rounded-2xl border border-white/10 bg-black/25">
|
||
<div className="flex items-center justify-between border-b border-white/5 px-4 py-3">
|
||
<span className="text-sm font-medium text-slate-100">最近开播场次</span>
|
||
<Link to="/sessions?platform=shorts" className="text-xs text-teal-300 hover:underline">
|
||
全部 →
|
||
</Link>
|
||
</div>
|
||
<div className="overflow-x-auto">
|
||
<table className="min-w-full text-left text-sm">
|
||
<thead className="bg-white/5 text-xs uppercase text-slate-400">
|
||
<tr>
|
||
<th className="px-3 py-2">频道</th>
|
||
<th className="px-3 py-2">标题</th>
|
||
<th className="px-3 py-2">开始</th>
|
||
<th className="px-3 py-2">时长</th>
|
||
<th className="px-3 py-2">状态</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{liveRecent.map((s) => (
|
||
<tr key={s.id} className="border-t border-white/5">
|
||
<td className="px-3 py-2 text-slate-100">{s.anchor_name || s.room_id || "—"}</td>
|
||
<td className="max-w-[240px] truncate px-3 py-2 text-slate-400">{s.title || "—"}</td>
|
||
<td className="px-3 py-2 text-slate-400">{fmtTs(s.started_at)}</td>
|
||
<td className="px-3 py-2 tabular-nums">{fmtDuration(s.duration_seconds)}</td>
|
||
<td className="px-3 py-2">{s.ended_at ? "已结束" : <span className="text-rose-300">在播</span>}</td>
|
||
</tr>
|
||
))}
|
||
{!liveRecent.length ? (
|
||
<tr>
|
||
<td colSpan={5} className="px-3 py-8 text-center text-slate-500">
|
||
暂无场次
|
||
</td>
|
||
</tr>
|
||
) : null}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
) : null}
|
||
|
||
{pageTab === "ops" ? (
|
||
<>
|
||
<div className="mb-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-5">
|
||
{[
|
||
["北京日配额", `${totals.used} / ${totals.cap || "—"}`, beijingWindow ? `${beijingWindow}(北京时间)` : "每频道强制 9 条/日"],
|
||
["当日缺口", String(totals.gap || quotaGap), minUnmet > 0 ? `${minUnmet} 个频道未达 9 条` : "各频道已达 9 条"],
|
||
["已上传", String(totals.uploaded), `排队 ${totals.scheduled} · 剩余 ${hoursLeft}`],
|
||
[
|
||
"抖音在播源",
|
||
String(status?.douyin_live_count ?? "—"),
|
||
status?.live_sample === false ? "在播放样已关" : "取样来自监控在播直播间",
|
||
],
|
||
[
|
||
"错峰间隔",
|
||
`≥${Math.round((status?.upload_min_gap_sec || 720) / 60)}分`,
|
||
`同频道 ≥${Math.round((status?.channel_min_gap_sec || 2400) / 60)}分 · 落后自动加速`,
|
||
],
|
||
].map(([label, value, hint]) => (
|
||
<div key={label as string} className="rounded-2xl border border-white/10 bg-black/30 px-4 py-3">
|
||
<div className="text-xs text-slate-500">{label}</div>
|
||
<div className="mt-1 font-display text-2xl text-white">{value}</div>
|
||
<div className="mt-1 text-[11px] text-slate-500">{hint}</div>
|
||
</div>
|
||
))}
|
||
</div>
|
||
|
||
{Array.isArray(status?.douyin_live_rooms) && status.douyin_live_rooms.length > 0 ? (
|
||
<div className="mb-4 rounded-2xl border border-teal-500/20 bg-teal-500/5 px-4 py-3">
|
||
<div className="mb-2 flex flex-wrap items-center gap-2 text-xs text-teal-100/90">
|
||
<span className="font-medium">当前可取样直播间({status.douyin_live_count ?? status.douyin_live_rooms.length})</span>
|
||
<span className="text-slate-500">来自监控 last_status=live,非 YouTube 上传频道开播</span>
|
||
</div>
|
||
<div className="flex flex-wrap gap-2">
|
||
{status.douyin_live_rooms.map((r: { room_id?: string; anchor?: string }) => (
|
||
<span
|
||
key={String(r.room_id)}
|
||
className="rounded-lg border border-white/10 bg-black/30 px-2 py-1 text-[11px] text-slate-200"
|
||
title={String(r.room_id || "")}
|
||
>
|
||
{r.anchor || r.room_id}
|
||
</span>
|
||
))}
|
||
</div>
|
||
</div>
|
||
) : status?.enabled && status?.live_sample !== false ? (
|
||
<div className="mb-4 rounded-2xl border border-amber-500/20 bg-amber-500/5 px-4 py-3 text-xs text-amber-100/90">
|
||
暂无抖音在播源可取样。Shorts 频道卡上的 LIVE/离线是 YouTube 上传频道自身开播状态(多数情况为离线属正常)。
|
||
</div>
|
||
) : null}
|
||
|
||
<div className="mb-4 flex flex-wrap items-center gap-2 rounded-2xl border border-white/10 bg-black/35 p-3 text-sm">
|
||
<span className="text-slate-400">开关</span>
|
||
<button
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => void toggle(true)}
|
||
className={`rounded-lg px-3 py-1.5 ${status?.enabled ? "bg-amber-500/25 text-amber-50" : "bg-white/5 text-slate-300"}`}
|
||
>
|
||
启用
|
||
</button>
|
||
<button
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => void toggle(false)}
|
||
className={`rounded-lg px-3 py-1.5 ${!status?.enabled ? "bg-rose-500/25 text-rose-50" : "bg-white/5 text-slate-300"}`}
|
||
>
|
||
关闭
|
||
</button>
|
||
<span className="text-xs text-slate-500">
|
||
worker {status?.running ? "运行中" : "未运行"}
|
||
{status?.busy ? " · 忙碌" : ""} · 处理 {status?.processed ?? 0} · 取样 {status?.sampled ?? 0}
|
||
· 补量 {status?.quota_filled ?? 0}
|
||
{status?.auto_retry_failed !== false ? ` · 自动重试 ${status?.auto_retried ?? 0}` : ""}
|
||
{status?.upload_spread !== false ? " · 24h 错峰开" : " · 错峰关"}
|
||
</span>
|
||
<button
|
||
type="button"
|
||
disabled={busy || !status?.enabled}
|
||
onClick={async () => {
|
||
setBusy(true);
|
||
try {
|
||
const r = await fetchJson("/api/shorts/fill-quota", { method: "POST" });
|
||
setMsg(`配额补量入队 ${r.just_quota_filled ?? 0}(北京日剩余 ${r.quota_gap ?? "—"} 槽)`);
|
||
await load();
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}}
|
||
className="ml-auto inline-flex items-center gap-1 rounded-lg border border-teal-500/30 bg-teal-500/10 px-3 py-1.5 text-xs text-teal-100"
|
||
>
|
||
<Upload className="h-3.5 w-3.5" />
|
||
补满配额
|
||
</button>
|
||
<button
|
||
type="button"
|
||
disabled={busy || !status?.enabled}
|
||
onClick={async () => {
|
||
setBusy(true);
|
||
try {
|
||
const r = await fetchJson("/api/shorts/sample-now", { method: "POST" });
|
||
setMsg(`取样入队 ${r.just_enqueued ?? 0}(录完后自动错峰排程)`);
|
||
await load();
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}}
|
||
className="ml-auto inline-flex items-center gap-1 rounded-lg border border-amber-500/30 bg-amber-500/10 px-3 py-1.5 text-xs text-amber-100"
|
||
>
|
||
<Video className="h-3.5 w-3.5" />
|
||
立即取样
|
||
</button>
|
||
<button
|
||
type="button"
|
||
disabled={busy || !status?.enabled}
|
||
onClick={() => void runReconcile()}
|
||
className="inline-flex items-center gap-1 rounded-lg border border-white/10 bg-white/5 px-3 py-1.5 text-xs text-slate-200"
|
||
>
|
||
<Wrench className="h-3.5 w-3.5" />
|
||
修复异常
|
||
</button>
|
||
<button
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => void probeOauth()}
|
||
className="inline-flex items-center gap-1 rounded-lg border border-white/10 bg-white/5 px-3 py-1.5 text-xs text-slate-200"
|
||
>
|
||
<KeyRound className="h-3.5 w-3.5" />
|
||
探测 OAuth
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => {
|
||
setShowForm(true);
|
||
setEditId("");
|
||
setForm({ ...emptyForm });
|
||
}}
|
||
className="inline-flex items-center gap-1 rounded-lg bg-[#ff2d55] px-3 py-1.5 text-xs text-white"
|
||
>
|
||
<Plus className="h-3.5 w-3.5" />
|
||
新频道
|
||
</button>
|
||
</div>
|
||
|
||
{msg ? <div className="mb-3 text-sm text-teal-300">{msg}</div> : null}
|
||
{error ? <div className="mb-3 text-sm text-rose-300">{error}</div> : null}
|
||
{status?.last_error ? (
|
||
<div className="mb-3 rounded-xl border border-amber-500/25 bg-amber-500/10 px-3 py-2 text-xs text-amber-100">
|
||
最近错误:{status.last_error}
|
||
</div>
|
||
) : null}
|
||
{behindChannels.length > 0 ? (
|
||
<div className="mb-3 flex flex-wrap items-center justify-between gap-2 rounded-xl border border-amber-500/30 bg-amber-500/10 px-3 py-2.5 text-xs text-amber-50">
|
||
<span>
|
||
配额落后:{behindChannels.map((c) => c.name || "频道").slice(0, 4).join("、")}
|
||
{behindChannels.length > 4 ? ` 等 ${behindChannels.length} 个` : ""} · 北京日进度{" "}
|
||
{Math.round(Number(status?.board?.day_progress || 0) * 100)}%
|
||
</span>
|
||
<button
|
||
type="button"
|
||
disabled={busy || !status?.enabled}
|
||
onClick={async () => {
|
||
setBusy(true);
|
||
try {
|
||
const r = await fetchJson("/api/shorts/fill-quota", { method: "POST" });
|
||
toast.push(`补量入队 ${r.just_quota_filled ?? 0}`, "ok");
|
||
await load();
|
||
} catch (e) {
|
||
toast.push((e as Error).message, "err");
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}}
|
||
className="rounded-lg bg-amber-500/25 px-2.5 py-1 text-amber-50 hover:bg-amber-500/35"
|
||
>
|
||
立即补量
|
||
</button>
|
||
</div>
|
||
) : null}
|
||
{oauthIssues.length > 0 ? (
|
||
<div className="mb-3 flex flex-wrap items-center justify-between gap-2 rounded-xl border border-rose-500/30 bg-rose-500/10 px-3 py-2.5 text-xs text-rose-50">
|
||
<span>
|
||
OAuth 异常:{oauthIssues.map((c) => c.name || "频道").slice(0, 4).join("、")}
|
||
{oauthIssues.length > 4 ? ` 等 ${oauthIssues.length} 个` : ""}
|
||
{oauthIssues[0]?.oauth_error ? ` · ${String(oauthIssues[0].oauth_error).slice(0, 60)}` : ""}
|
||
</span>
|
||
<div className="flex gap-1.5">
|
||
<button
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => void probeOauth()}
|
||
className="rounded-lg bg-rose-500/25 px-2.5 py-1 hover:bg-rose-500/35"
|
||
>
|
||
重新探测
|
||
</button>
|
||
{oauthIssues[0]?.id ? (
|
||
<button
|
||
type="button"
|
||
onClick={() => goAuthorize(oauthIssues[0].id)}
|
||
className="rounded-lg bg-amber-500/25 px-2.5 py-1 text-amber-50 hover:bg-amber-500/35"
|
||
>
|
||
去授权
|
||
</button>
|
||
) : null}
|
||
</div>
|
||
</div>
|
||
) : null}
|
||
|
||
{loading && !status ? <PageLoader label="加载 Shorts 分发台…" /> : null}
|
||
|
||
{!loading || status ? (
|
||
<>
|
||
<div className="mb-6 grid gap-4 xl:grid-cols-[1.4fr_1fr] xl:items-stretch">
|
||
<section className="rounded-2xl border border-white/10 bg-black/25 p-4">
|
||
<div className="mb-3 flex items-center justify-between gap-2">
|
||
<h3 className="text-sm font-medium text-slate-100">频道配额({channels.length})</h3>
|
||
<span className="text-[11px] text-slate-500">
|
||
北京日 {beijingWindow || "—"} · 每频道强制 9 条/日
|
||
</span>
|
||
</div>
|
||
<div className="grid gap-3 sm:grid-cols-2">
|
||
{(boardChannels.length ? boardChannels : channels.map((c) => ({
|
||
id: c.id,
|
||
name: c.name,
|
||
youtube_channel_id: c.youtube_channel_id,
|
||
enabled: c.enabled,
|
||
daily_min: Number(c.daily_min ?? 9),
|
||
daily_max: Number(c.daily_max ?? 9),
|
||
authorized: Boolean(c.authorized || c.has_refresh_token),
|
||
used: 0,
|
||
scheduled: 0,
|
||
uploaded: 0,
|
||
pending: 0,
|
||
gap: Number(c.daily_max ?? 9),
|
||
min_met: false,
|
||
max_met: false,
|
||
behind_pace: false,
|
||
}))).map((ch) => {
|
||
const pct = Math.min(100, Math.round((ch.used / Math.max(1, ch.daily_max)) * 100));
|
||
const minPct = Math.round((ch.daily_min / Math.max(1, ch.daily_max)) * 100);
|
||
const full = channels.find((x) => x.id === ch.id);
|
||
const gap = ch.gap ?? Math.max(0, ch.daily_max - ch.used);
|
||
const warn = ch.enabled !== false && (ch.behind_pace || (!ch.min_met && gap > 0));
|
||
return (
|
||
<div
|
||
key={ch.id}
|
||
className={`rounded-xl border bg-gradient-to-br from-white/[0.04] to-transparent p-3 ${
|
||
warn ? "border-amber-500/35" : ch.max_met ? "border-teal-500/30" : "border-white/10"
|
||
}`}
|
||
>
|
||
<div className="flex items-start justify-between gap-2">
|
||
<div className="min-w-0">
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<div className="truncate font-medium text-slate-100">{ch.name || "未命名"}</div>
|
||
<YouTubeLiveBadge refKey={shortsLiveRef(ch.id)} compact showOffline />
|
||
</div>
|
||
<div className="mt-0.5 truncate font-mono text-[10px] text-slate-500">
|
||
{ch.youtube_channel_id || "—"}
|
||
</div>
|
||
</div>
|
||
<div className="flex shrink-0 flex-col items-end gap-1 text-[10px]">
|
||
<span className={ch.enabled === false ? "text-slate-500" : "text-teal-300"}>
|
||
{ch.enabled === false ? "停用" : "启用"}
|
||
</span>
|
||
<span
|
||
className={
|
||
oauthIssues.some((x) => x.id === ch.id)
|
||
? "text-rose-300"
|
||
: ch.authorized
|
||
? "text-teal-300"
|
||
: "text-amber-300"
|
||
}
|
||
>
|
||
{oauthIssues.some((x) => x.id === ch.id)
|
||
? "OAuth 异常"
|
||
: ch.authorized
|
||
? "已授权"
|
||
: "未授权"}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
<div className="relative mt-3 h-1.5 overflow-hidden rounded-full bg-white/10">
|
||
<div
|
||
className="pointer-events-none absolute inset-y-0 w-0.5 bg-amber-400/60"
|
||
style={{ left: `${minPct}%` }}
|
||
/>
|
||
<div
|
||
className={`h-full rounded-full ${pct >= 100 ? "bg-teal-400" : pct >= minPct ? "bg-amber-400" : "bg-rose-400"}`}
|
||
style={{ width: `${pct}%` }}
|
||
/>
|
||
</div>
|
||
<div className="mt-2 flex flex-wrap gap-x-3 gap-y-1 text-[11px] text-slate-400">
|
||
<span>
|
||
{ch.used}/{ch.daily_max || 9}
|
||
<span className="text-slate-600">(强制 9)</span>
|
||
</span>
|
||
{gap > 0 ? <span className="text-amber-300">缺 {gap}</span> : <span className="text-teal-300">已满</span>}
|
||
<span>上传 {ch.uploaded}</span>
|
||
<span>排队 {ch.scheduled}</span>
|
||
<span>待录 {ch.pending}</span>
|
||
</div>
|
||
<div className="mt-3 flex flex-wrap gap-1.5">
|
||
<button
|
||
type="button"
|
||
onClick={() => goAuthorize(ch.id)}
|
||
className="inline-flex items-center gap-1 rounded-lg bg-amber-500/15 px-2 py-1 text-[11px] text-amber-100"
|
||
>
|
||
<KeyRound className="h-3 w-3" />
|
||
{ch.authorized ? "重授权" : "授权"}
|
||
</button>
|
||
{full ? (
|
||
<button type="button" onClick={() => startEdit(full)} className="rounded-lg bg-white/5 px-2 py-1 text-[11px]">
|
||
编辑
|
||
</button>
|
||
) : null}
|
||
<button
|
||
type="button"
|
||
onClick={() => void removeChannel(ch.id)}
|
||
className="inline-flex items-center gap-1 rounded-lg bg-rose-500/10 px-2 py-1 text-[11px] text-rose-200"
|
||
>
|
||
<Trash2 className="h-3 w-3" />
|
||
删
|
||
</button>
|
||
</div>
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
</section>
|
||
|
||
<section className="flex min-h-[24rem] flex-col rounded-2xl border border-white/10 bg-black/25 p-4 xl:h-full xl:min-h-0">
|
||
<div className="mb-3 flex items-center justify-between gap-3">
|
||
<div className="flex items-center gap-2">
|
||
<Clock3 className="h-4 w-4 text-amber-300" />
|
||
<h3 className="text-sm font-medium text-slate-100">即将上传(错峰日程)</h3>
|
||
</div>
|
||
<span className="rounded-full border border-amber-500/25 bg-amber-500/10 px-2 py-0.5 text-xs text-amber-200">
|
||
素材 {upcoming.length}
|
||
</span>
|
||
</div>
|
||
<p className="mb-3 shrink-0 text-[11px] text-slate-500">
|
||
北京 {status?.board?.beijing_hint || "—"} · 点击「立即上传」确认后发布 · 上传过程有进度提示
|
||
</p>
|
||
<div className="min-h-0 flex-1 space-y-2 overflow-y-auto pr-1">
|
||
{upcoming.length === 0 ? (
|
||
<div className="rounded-xl border border-dashed border-white/10 px-3 py-8 text-center text-sm text-slate-500">
|
||
暂无排程。取样或开播入队后,成片会自动分散到今日各时段。
|
||
</div>
|
||
) : (
|
||
upcoming.map((u: any) => (
|
||
<ShortsUpcomingUploadRow
|
||
key={u.id}
|
||
item={toUploadTarget(u, channelName)}
|
||
track={uploadUx.tracks[u.id]}
|
||
onRequestUpload={uploadUx.openConfirm}
|
||
onRetry={uploadUx.retryUpload}
|
||
/>
|
||
))
|
||
)}
|
||
</div>
|
||
</section>
|
||
</div>
|
||
|
||
{showForm ? (
|
||
<section className="mb-6 rounded-2xl border border-white/10 bg-black/30 p-4">
|
||
<h3 className="mb-3 text-sm font-medium text-slate-100">{editId ? "编辑频道" : "新增频道"}</h3>
|
||
<div className="grid gap-2 sm:grid-cols-2 lg:grid-cols-4">
|
||
<label className="block text-xs text-slate-400 sm:col-span-2">
|
||
显示名称
|
||
<input
|
||
value={form.name}
|
||
onChange={(e) => setForm((p) => ({ ...p, name: e.target.value }))}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-slate-900 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="block text-xs text-slate-400">
|
||
每日配额
|
||
<input
|
||
type="number"
|
||
value={9}
|
||
disabled
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-slate-900/60 px-3 py-2 text-sm text-slate-400"
|
||
/>
|
||
<span className="mt-1 block text-[10px] text-amber-300/80">强制每频道每天 9 条(不可改)</span>
|
||
</label>
|
||
<label className="block text-xs text-slate-400 sm:col-span-2">
|
||
标题模板
|
||
<input
|
||
value={form.title_template}
|
||
onChange={(e) => setForm((p) => ({ ...p, title_template: e.target.value }))}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-slate-900 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="block text-xs text-slate-400 sm:col-span-2">
|
||
描述模板
|
||
<input
|
||
value={form.description_template}
|
||
onChange={(e) => setForm((p) => ({ ...p, description_template: e.target.value }))}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-slate-900 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="block text-xs text-slate-400">
|
||
公开范围
|
||
<select
|
||
value={form.privacy}
|
||
onChange={(e) => setForm((p) => ({ ...p, privacy: e.target.value }))}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-slate-900 px-3 py-2 text-sm"
|
||
>
|
||
<option value="public">公开 public</option>
|
||
<option value="unlisted">不公开列出 unlisted</option>
|
||
<option value="private">私密 private</option>
|
||
</select>
|
||
</label>
|
||
<label className="block text-xs text-slate-400">
|
||
备注
|
||
<input
|
||
value={form.note}
|
||
onChange={(e) => setForm((p) => ({ ...p, note: e.target.value }))}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-slate-900 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
</div>
|
||
<div className="mt-3 flex flex-wrap gap-2">
|
||
<button
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => void saveChannel(true)}
|
||
className="inline-flex items-center gap-1 rounded-lg bg-[#ff2d55] px-3 py-2 text-sm text-white"
|
||
>
|
||
{busy ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <KeyRound className="h-3.5 w-3.5" />}
|
||
保存并授权
|
||
</button>
|
||
<button
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => void saveChannel(false)}
|
||
className="rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm"
|
||
>
|
||
仅保存
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => {
|
||
setShowForm(false);
|
||
setEditId("");
|
||
}}
|
||
className="rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm"
|
||
>
|
||
取消
|
||
</button>
|
||
</div>
|
||
</section>
|
||
) : null}
|
||
|
||
<section className="rounded-2xl border border-white/10 bg-black/25 p-4">
|
||
<div className="mb-3 flex flex-wrap items-center justify-between gap-2">
|
||
<h3 className="text-sm font-medium text-slate-100">
|
||
任务流水({jobTotal})
|
||
<span className="ml-2 text-[11px] font-normal text-slate-500">
|
||
pending {stats.pending || 0} · scheduled {stats.scheduled || 0} · uploaded {stats.uploaded || 0} · failed{" "}
|
||
{stats.failed || 0}
|
||
</span>
|
||
</h3>
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => setJobFilter("failed")}
|
||
className={`rounded-lg px-2.5 py-1 text-[11px] ${
|
||
jobFilter === "failed" ? "bg-rose-500/25 text-rose-100" : "bg-white/5 text-slate-400"
|
||
}`}
|
||
>
|
||
仅看失败
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => setJobDate(todayBeijingISO())}
|
||
className={`rounded-lg px-2.5 py-1 text-[11px] ${
|
||
jobDate === todayBeijingISO() ? "bg-amber-500/20 text-amber-100" : "bg-white/5 text-slate-400"
|
||
}`}
|
||
>
|
||
今日
|
||
</button>
|
||
<select
|
||
value={jobChannel}
|
||
onChange={(e) => setJobChannel(e.target.value)}
|
||
className="rounded-lg border border-white/10 bg-slate-900 px-2 py-1.5 text-xs"
|
||
>
|
||
<option value="">全部频道</option>
|
||
{channels.map((c) => (
|
||
<option key={c.id} value={c.id}>
|
||
{c.name}
|
||
</option>
|
||
))}
|
||
</select>
|
||
<input
|
||
type="date"
|
||
value={jobDate}
|
||
onChange={(e) => setJobDate(e.target.value)}
|
||
className="rounded-lg border border-white/10 bg-slate-900 px-2 py-1.5 text-xs"
|
||
/>
|
||
<select
|
||
value={jobFilter}
|
||
onChange={(e) => setJobFilter(e.target.value)}
|
||
className="rounded-lg border border-white/10 bg-slate-900 px-3 py-1.5 text-xs"
|
||
>
|
||
<option value="">全部状态</option>
|
||
{["pending", "recording", "recorded", "scheduled", "uploading", "uploaded", "failed"].map((s) => (
|
||
<option key={s} value={s}>
|
||
{s}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</div>
|
||
</div>
|
||
{selectedCount > 0 ? (
|
||
<div className="mb-3 flex flex-wrap items-center gap-2 rounded-xl border border-teal-500/20 bg-teal-500/5 px-3 py-2 text-xs">
|
||
<span className="text-teal-200">已选 {selectedCount}</span>
|
||
<button
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => void bulkAction("retry")}
|
||
className="rounded-lg bg-white/10 px-2.5 py-1 text-slate-100 hover:bg-white/15"
|
||
>
|
||
批量重排
|
||
</button>
|
||
<button
|
||
type="button"
|
||
disabled={busy || uploadBusy}
|
||
onClick={() => void bulkAction("upload_now")}
|
||
className="rounded-lg bg-teal-500/20 px-2.5 py-1 text-teal-100 hover:bg-teal-500/30"
|
||
>
|
||
批量立即上传
|
||
</button>
|
||
<button type="button" onClick={() => setSelectedIds({})} className="text-slate-500 hover:text-slate-300">
|
||
取消选择
|
||
</button>
|
||
</div>
|
||
) : null}
|
||
<div className="overflow-x-auto">
|
||
{loading && jobs.length === 0 ? (
|
||
<TableSkeleton rows={6} cols={5} />
|
||
) : (
|
||
<table className="w-full min-w-[920px] text-left text-sm">
|
||
<thead className="text-xs text-slate-500">
|
||
<tr>
|
||
<th className="pb-2 pr-2">
|
||
<input
|
||
type="checkbox"
|
||
checked={jobs.length > 0 && jobs.every((j) => selectedIds[j.id])}
|
||
onChange={toggleSelectAll}
|
||
className="rounded border-white/20"
|
||
/>
|
||
</th>
|
||
<th className="pb-2 pr-2">状态</th>
|
||
<th className="pb-2 pr-2">主播</th>
|
||
<th className="pb-2 pr-2">频道</th>
|
||
<th className="pb-2 pr-2">排程上传</th>
|
||
<th className="pb-2 pr-2">创建</th>
|
||
<th className="pb-2 pr-2">结果</th>
|
||
<th className="pb-2">操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{jobs.map((j) => (
|
||
<tr key={j.id} className="border-t border-white/5 align-top">
|
||
<td className="py-2.5 pr-2">
|
||
<input
|
||
type="checkbox"
|
||
checked={Boolean(selectedIds[j.id])}
|
||
onChange={() => toggleSelect(j.id)}
|
||
className="rounded border-white/20"
|
||
/>
|
||
</td>
|
||
<td className="py-2.5 pr-2">
|
||
<span className={`rounded-md px-2 py-0.5 text-[11px] ${STATUS_STYLE[j.status] || "bg-white/10"}`}>
|
||
{j.status}
|
||
</span>
|
||
</td>
|
||
<td className="py-2.5 pr-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => setDetailJobId(j.id)}
|
||
className="text-left hover:text-teal-200"
|
||
>
|
||
<div>{j.anchor_name || "—"}</div>
|
||
<div className="font-mono text-[10px] text-slate-500">{j.room_id || ""}</div>
|
||
</button>
|
||
</td>
|
||
<td className="py-2.5 pr-2 text-xs text-slate-400">
|
||
{channelName[String(j.channel_pk || "")] || "—"}
|
||
</td>
|
||
<td className="py-2.5 pr-2 text-xs text-slate-400">
|
||
{j.upload_at ? fmtTs(Number(j.upload_at)) : "—"}
|
||
</td>
|
||
<td className="py-2.5 pr-2 text-xs text-slate-500">{fmtTs(Number(j.created_at || 0))}</td>
|
||
<td className="py-2.5 pr-2 text-xs">
|
||
{j.youtube_url ? (
|
||
<a href={j.youtube_url} target="_blank" rel="noreferrer" className="text-teal-300 hover:underline">
|
||
已上传
|
||
</a>
|
||
) : j.error ? (
|
||
<span className="text-rose-300">{j.error}</span>
|
||
) : j.clip_ready ? (
|
||
<span className="text-slate-400">成片待传</span>
|
||
) : j.clip_path ? (
|
||
<span className="text-amber-300">成片丢失</span>
|
||
) : (
|
||
"—"
|
||
)}
|
||
</td>
|
||
<td className="py-2.5">
|
||
<div className="flex flex-wrap gap-1">
|
||
<button
|
||
type="button"
|
||
onClick={() => setDetailJobId(j.id)}
|
||
className="inline-flex items-center gap-1 rounded bg-white/5 px-2 py-1 text-xs"
|
||
>
|
||
<Eye className="h-3 w-3" />
|
||
详情
|
||
</button>
|
||
{["scheduled", "recorded"].includes(j.status) && j.clip_ready ? (
|
||
<button
|
||
type="button"
|
||
disabled={uploadBusy || uploadUx.tracks[j.id]?.phase === "uploading"}
|
||
onClick={() =>
|
||
uploadUx.openConfirm(
|
||
toUploadTarget(
|
||
{
|
||
id: j.id,
|
||
anchor_name: j.anchor_name,
|
||
room_id: j.room_id,
|
||
channel_pk: j.channel_pk,
|
||
upload_at: j.upload_at,
|
||
clip_ready: true,
|
||
status: j.status,
|
||
},
|
||
channelName,
|
||
),
|
||
)
|
||
}
|
||
className="inline-flex items-center gap-1 rounded bg-teal-500/15 px-2 py-1 text-xs text-teal-100 hover:bg-teal-500/25"
|
||
>
|
||
<Upload className="h-3 w-3" />
|
||
立即上传
|
||
</button>
|
||
) : null}
|
||
{["failed", "skipped", "recorded", "scheduled"].includes(j.status) ? (
|
||
<button
|
||
type="button"
|
||
onClick={() => void retryJob(j.id)}
|
||
className="inline-flex items-center gap-1 rounded bg-white/5 px-2 py-1 text-xs"
|
||
>
|
||
<RotateCcw className="h-3 w-3" />
|
||
重排
|
||
</button>
|
||
) : null}
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
))}
|
||
{jobs.length === 0 ? (
|
||
<tr>
|
||
<td colSpan={8} className="py-8 text-center text-slate-500">
|
||
暂无任务
|
||
</td>
|
||
</tr>
|
||
) : null}
|
||
</tbody>
|
||
</table>
|
||
)}
|
||
</div>
|
||
{jobs.length > 0 && jobs.length < jobTotal ? (
|
||
<div className="mt-4 flex justify-center">
|
||
<button
|
||
type="button"
|
||
disabled={loadingMoreJobs}
|
||
onClick={() => void load({ appendJobs: true, jobOffset: jobs.length })}
|
||
className="inline-flex items-center gap-2 rounded-xl border border-white/10 bg-white/5 px-5 py-2 text-sm text-slate-200 hover:bg-white/10 disabled:opacity-50"
|
||
>
|
||
{loadingMoreJobs ? <Loader2 className="h-4 w-4 animate-spin" /> : null}
|
||
加载更多任务({jobs.length}/{jobTotal})
|
||
</button>
|
||
</div>
|
||
) : null}
|
||
</section>
|
||
</>
|
||
) : null}
|
||
</>
|
||
) : null}
|
||
</Shell>
|
||
);
|
||
}
|