Keep vertical long-form by default (16:9 opt-in off), reuse masters via ffmpeg segments for other platforms, and rush-or-delete clips older than 48h. Co-authored-by: Cursor <cursoragent@cursor.com>
960 lines
40 KiB
TypeScript
960 lines
40 KiB
TypeScript
"use client";
|
||
|
||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||
import Link from "next/link";
|
||
import {
|
||
Clock3,
|
||
Download,
|
||
Loader2,
|
||
Plus,
|
||
RefreshCw,
|
||
RotateCcw,
|
||
Trash2,
|
||
Upload,
|
||
Video,
|
||
} from "lucide-react";
|
||
|
||
import { Shell } from "@/components/Shell";
|
||
import { useConfirm } from "@/components/ConfirmDialog";
|
||
import { useToast } from "@/components/Toast";
|
||
import { OauthClientBadge, normalizeOauthProfileId } from "@/components/OauthClientBadge";
|
||
import { PageLoader, TableSkeleton } from "@/components/ui";
|
||
import { fetchJson, fmtTs, todayBeijingISO } from "@/lib/api";
|
||
|
||
type Channel = {
|
||
id: string;
|
||
name: string;
|
||
youtube_channel_id?: string;
|
||
enabled?: boolean;
|
||
daily_max?: number;
|
||
title_template?: string;
|
||
description_template?: string;
|
||
privacy?: string;
|
||
note?: string;
|
||
authorized?: boolean;
|
||
has_refresh_token?: boolean;
|
||
oauth_profile_id?: string;
|
||
oauth_error?: string;
|
||
};
|
||
|
||
type BoardChannel = {
|
||
id: string;
|
||
name?: string;
|
||
youtube_channel_id?: string;
|
||
oauth_profile_id?: string;
|
||
enabled?: boolean;
|
||
daily_max: number;
|
||
authorized?: boolean;
|
||
used: number;
|
||
scheduled: number;
|
||
uploaded: number;
|
||
pending: number;
|
||
failed?: number;
|
||
gap?: number;
|
||
};
|
||
|
||
type Job = {
|
||
id: string;
|
||
status: string;
|
||
anchor_name?: string;
|
||
room_id?: string;
|
||
push_date?: string;
|
||
channel_pk?: string;
|
||
clip_path?: string;
|
||
clip_ready?: boolean;
|
||
clip_seconds?: number;
|
||
youtube_url?: string;
|
||
title?: string;
|
||
error?: string;
|
||
created_at?: number;
|
||
upload_at?: number;
|
||
};
|
||
|
||
const emptyForm = {
|
||
name: "",
|
||
enabled: true,
|
||
daily_max: 3,
|
||
title_template: "",
|
||
description_template: "",
|
||
privacy: "public",
|
||
note: "",
|
||
oauth_profile_id: "A",
|
||
};
|
||
|
||
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 YtLongPage() {
|
||
const confirm = useConfirm();
|
||
const toast = useToast();
|
||
const leftPanelRef = useRef<HTMLElement>(null);
|
||
const rightPanelRef = useRef<HTMLElement>(null);
|
||
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 [upcoming, setUpcoming] = useState<Job[]>([]);
|
||
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 [jobDate, setJobDate] = useState("");
|
||
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/yt-long/jobs?${jobQs}`);
|
||
setJobs((prev) => [...prev, ...(jb.jobs || [])]);
|
||
setJobTotal(jb.total || 0);
|
||
} else {
|
||
const upQs = new URLSearchParams({ limit: "40", offset: "0", status: "scheduled" });
|
||
if (!jobDate) upQs.set("push_date", todayBeijingISO());
|
||
else upQs.set("push_date", jobDate);
|
||
const [st, ch, jb, up] = await Promise.all([
|
||
fetchJson("/api/yt-long/status"),
|
||
fetchJson("/api/yt-long/channels"),
|
||
fetchJson(`/api/yt-long/jobs?${jobQs}`),
|
||
fetchJson(`/api/yt-long/jobs?${upQs}`),
|
||
]);
|
||
setStatus(st);
|
||
setChannels(ch.channels || []);
|
||
setJobs(jb.jobs || []);
|
||
setJobTotal(jb.total || 0);
|
||
setUpcoming(up.jobs || []);
|
||
}
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
toast.push((e as Error).message, "err");
|
||
} finally {
|
||
setLoading(false);
|
||
setLoadingMoreJobs(false);
|
||
}
|
||
},
|
||
[jobChannel, jobDate, jobFilter, toast],
|
||
);
|
||
|
||
useEffect(() => {
|
||
void load();
|
||
const t = setInterval(() => void load(), 10000);
|
||
return () => clearInterval(t);
|
||
}, [load]);
|
||
|
||
useEffect(() => {
|
||
const left = leftPanelRef.current;
|
||
const right = rightPanelRef.current;
|
||
if (!left || !right) return;
|
||
const sync = () => {
|
||
right.style.maxHeight = `${left.getBoundingClientRect().height}px`;
|
||
};
|
||
sync();
|
||
const ro = new ResizeObserver(sync);
|
||
ro.observe(left);
|
||
return () => ro.disconnect();
|
||
});
|
||
|
||
const boardChannels: BoardChannel[] = status?.board?.channels || [];
|
||
const beijingWindow = status?.board?.push_date || status?.stats?.push_date || "";
|
||
const quotaGap = Number(status?.quota_gap ?? status?.board?.total_gap ?? 0);
|
||
const stats = status?.stats?.by_status || {};
|
||
const settings = status?.settings || {};
|
||
|
||
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 channelProfileMap = useMemo(() => {
|
||
const m: Record<string, string> = {};
|
||
for (const c of channels) m[c.id] = normalizeOauthProfileId(c.oauth_profile_id);
|
||
for (const c of boardChannels) {
|
||
const pid = normalizeOauthProfileId(c.oauth_profile_id);
|
||
if (pid) m[c.id] = pid;
|
||
}
|
||
return m;
|
||
}, [channels, boardChannels]);
|
||
|
||
const displayChannels = useMemo(() => {
|
||
const base: BoardChannel[] = boardChannels.length
|
||
? boardChannels
|
||
: channels.map((c) => ({
|
||
id: c.id,
|
||
name: c.name,
|
||
youtube_channel_id: c.youtube_channel_id,
|
||
oauth_profile_id: c.oauth_profile_id,
|
||
enabled: c.enabled,
|
||
daily_max: Number(c.daily_max ?? 3),
|
||
authorized: Boolean(c.authorized || c.has_refresh_token),
|
||
used: 0,
|
||
scheduled: 0,
|
||
uploaded: 0,
|
||
pending: 0,
|
||
failed: 0,
|
||
gap: Number(c.daily_max ?? 3),
|
||
}));
|
||
return base.map((ch) => ({
|
||
...ch,
|
||
oauth_profile_id:
|
||
normalizeOauthProfileId(ch.oauth_profile_id) || channelProfileMap[ch.id] || "",
|
||
}));
|
||
}, [boardChannels, channels, channelProfileMap]);
|
||
|
||
const channelGroups = useMemo(() => {
|
||
const buckets = new Map<string, BoardChannel[]>();
|
||
for (const ch of displayChannels) {
|
||
const key = normalizeOauthProfileId(ch.oauth_profile_id) || "_";
|
||
const arr = buckets.get(key) || [];
|
||
arr.push(ch);
|
||
buckets.set(key, arr);
|
||
}
|
||
for (const [k, arr] of buckets) {
|
||
arr.sort((a, b) => {
|
||
const an = String(a.name || "").localeCompare(String(b.name || ""), "zh");
|
||
if (an !== 0) return an;
|
||
return String(a.id || "").localeCompare(String(b.id || ""));
|
||
});
|
||
buckets.set(k, arr);
|
||
}
|
||
const order = ["A", "B", "C", "D", "_"];
|
||
const keys = [...order.filter((k) => buckets.has(k)), ...[...buckets.keys()].filter((k) => !order.includes(k))];
|
||
return keys.map((k) => ({
|
||
profileId: k === "_" ? "" : k,
|
||
channels: buckets.get(k) || [],
|
||
}));
|
||
}, [displayChannels]);
|
||
|
||
const upcomingGroups = useMemo(() => {
|
||
const buckets = new Map<string, Job[]>();
|
||
for (const u of upcoming) {
|
||
const pk = String(u.channel_pk || "");
|
||
const key = channelProfileMap[pk] || "_";
|
||
const arr = buckets.get(key) || [];
|
||
arr.push(u);
|
||
buckets.set(key, arr);
|
||
}
|
||
const order = ["A", "B", "C", "D", "_"];
|
||
const keys = [...order.filter((k) => buckets.has(k)), ...[...buckets.keys()].filter((k) => !order.includes(k))];
|
||
return keys.map((k) => ({
|
||
profileId: k === "_" ? "" : k,
|
||
items: buckets.get(k) || [],
|
||
}));
|
||
}, [upcoming, channelProfileMap]);
|
||
|
||
const totals = useMemo(() => {
|
||
const uploaded = displayChannels.reduce((s, c) => s + (c.uploaded || 0), 0);
|
||
const scheduled = displayChannels.reduce((s, c) => s + (c.scheduled || 0), 0);
|
||
const used = displayChannels.reduce((s, c) => s + (c.used || 0), 0);
|
||
const cap = displayChannels.reduce((s, c) => s + (c.daily_max || 0), 0);
|
||
const gap = displayChannels
|
||
.filter((c) => c.enabled !== false)
|
||
.reduce((s, c) => s + (c.gap ?? Math.max(0, (c.daily_max || 0) - (c.used || 0))), 0);
|
||
return { uploaded, scheduled, used, cap, gap };
|
||
}, [displayChannels]);
|
||
|
||
async function toggle(enabled: boolean) {
|
||
setBusy(true);
|
||
setMsg("");
|
||
setError("");
|
||
try {
|
||
await fetchJson(enabled ? "/api/yt-long/start" : "/api/yt-long/stop", { method: "POST" });
|
||
setMsg(enabled ? "已启用长视频(Shorts 不受影响)" : "已关闭长视频");
|
||
await load();
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
async function importFromShorts() {
|
||
setBusy(true);
|
||
setMsg("");
|
||
setError("");
|
||
try {
|
||
const r = await fetchJson("/api/yt-long/channels/import-shorts", { method: "POST" });
|
||
setMsg(`已同步 Shorts 频道:新建 ${r.created ?? 0} · 更新 ${r.updated ?? 0}`);
|
||
toast.push("频道已同步", "ok");
|
||
await load();
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
toast.push((e as Error).message, "err");
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
async function saveChannel() {
|
||
setBusy(true);
|
||
setMsg("");
|
||
setError("");
|
||
try {
|
||
const body: Record<string, unknown> = {
|
||
name: form.name.trim() || "未命名长视频频道",
|
||
enabled: form.enabled,
|
||
daily_max: Math.max(0, Number(form.daily_max) || 0),
|
||
title_template: form.title_template,
|
||
description_template: form.description_template,
|
||
privacy: form.privacy,
|
||
note: form.note,
|
||
oauth_profile_id: String(form.oauth_profile_id || "A").toUpperCase(),
|
||
};
|
||
if (editId) {
|
||
await fetchJson(`/api/yt-long/channels/${editId}`, { method: "PATCH", body: JSON.stringify(body) });
|
||
setMsg("频道已更新");
|
||
} else {
|
||
await fetchJson("/api/yt-long/channels", { method: "POST", body: JSON.stringify(body) });
|
||
setMsg("频道已创建");
|
||
}
|
||
setForm({ ...emptyForm });
|
||
setEditId("");
|
||
setShowForm(false);
|
||
await load();
|
||
} 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_max: Number(ch.daily_max ?? 3),
|
||
title_template: ch.title_template || "{anchor} street food live",
|
||
description_template: ch.description_template || "",
|
||
privacy: ch.privacy || "public",
|
||
note: ch.note || "",
|
||
oauth_profile_id: (ch.oauth_profile_id || "A").toUpperCase(),
|
||
});
|
||
}
|
||
|
||
async function removeChannel(id: string) {
|
||
const ok = await confirm({
|
||
title: "删除长视频频道",
|
||
message: "确定删除该频道?将同时删除该频道下全部长视频任务(不影响 Shorts)。",
|
||
confirmLabel: "删除",
|
||
destructive: true,
|
||
});
|
||
if (!ok) return;
|
||
const r = await fetchJson(`/api/yt-long/channels/${id}`, { method: "DELETE" });
|
||
toast.push(`频道已删除(任务 ${r.jobs ?? 0})`, "ok");
|
||
await load();
|
||
}
|
||
|
||
async function retryJob(id: string) {
|
||
await fetchJson(`/api/yt-long/jobs/${id}/retry`, { method: "POST" });
|
||
toast.push("已重新入队", "ok");
|
||
await load();
|
||
}
|
||
|
||
return (
|
||
<Shell>
|
||
<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">Long Video Pipeline</p>
|
||
<h2 className="font-display text-3xl font-semibold tracking-tight">📺 长视频分发台</h2>
|
||
<p className="mt-1 max-w-2xl text-sm text-slate-400">
|
||
在播放样 → 9:16 成片(3–8 分钟)→ 上传为普通 YouTube 视频(watch?v=)。成片可 cascade 切 TikTok / Shorts / X。频道 OAuth 可从 Shorts 同步,业务数据隔离。
|
||
</p>
|
||
</div>
|
||
<div className="flex flex-wrap gap-2">
|
||
<Link href="/shorts" className="rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm hover:bg-white/10">
|
||
Shorts
|
||
</Link>
|
||
<Link href="/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={() => 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 grid gap-3 sm:grid-cols-2 lg:grid-cols-5">
|
||
{[
|
||
["北京日配额", `${status?.quota_used ?? 0} / ${status?.daily_max ?? "—"}`, beijingWindow ? `${beijingWindow}(北京时间 · 全局)` : "全局日上限"],
|
||
["当日缺口", String(quotaGap), `频道合计缺口 ${totals.gap}`],
|
||
["已上传", String(totals.uploaded), `排队 ${totals.scheduled}`],
|
||
[
|
||
"抖音在播源",
|
||
String(status?.douyin_live_count ?? "—"),
|
||
settings.yt_long_live_sample === false ? "在播放样已关" : "取样来自监控在播直播间",
|
||
],
|
||
[
|
||
"成片规格",
|
||
`${Math.round(Number(settings.yt_long_min_sec || 180) / 60)}–${Math.round(Number(settings.yt_long_max_sec || 480) / 60)}分`,
|
||
"9:16 竖屏长视频 · 非 Shorts",
|
||
],
|
||
].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>
|
||
|
||
<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}
|
||
</span>
|
||
<button
|
||
type="button"
|
||
disabled={busy || !status?.enabled}
|
||
onClick={async () => {
|
||
setBusy(true);
|
||
setMsg("");
|
||
setError("");
|
||
try {
|
||
const r = await fetchJson("/api/yt-long/fill-quota", { method: "POST" });
|
||
setMsg(`配额补量入队 ${r.enqueued ?? 0}(剩余缺口 ${r.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);
|
||
setMsg("");
|
||
setError("");
|
||
try {
|
||
const r = await fetchJson("/api/yt-long/sample-now", { method: "POST" });
|
||
const n = Number(r.enqueued ?? 0);
|
||
const live = Number(r.live_count ?? 0);
|
||
if (n > 0) setMsg(`正在从 ${live} 路在播中录制 ${n} 条长视频`);
|
||
else setError(String(r.reason_zh || r.reason || "没有可取样的在播直播间"));
|
||
await load();
|
||
} catch (e) {
|
||
setError((e as Error).message);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}}
|
||
className="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}
|
||
onClick={() => void importFromShorts()}
|
||
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"
|
||
>
|
||
<Download className="h-3.5 w-3.5" />
|
||
同步 Shorts 频道
|
||
</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}
|
||
|
||
{loading && !status ? <PageLoader label="加载长视频分发台…" /> : null}
|
||
|
||
{!loading || status ? (
|
||
<>
|
||
<div className="mb-6 grid gap-4 xl:grid-cols-[1.4fr_1fr] xl:items-start">
|
||
<section ref={leftPanelRef} 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">
|
||
按 Client A/B/C/D 分组 · 北京日 {beijingWindow || "—"} · 独立 yt_long_channels
|
||
</span>
|
||
</div>
|
||
<div className="space-y-4">
|
||
{channelGroups.length === 0 ? (
|
||
<div className="rounded-xl border border-dashed border-white/10 px-3 py-8 text-center text-sm text-slate-500">
|
||
暂无频道。点「同步 Shorts 频道」或「新频道」。
|
||
</div>
|
||
) : (
|
||
channelGroups.map((group) => (
|
||
<div key={group.profileId || "none"} className="space-y-2">
|
||
<div className="flex flex-wrap items-center gap-2 border-b border-white/5 pb-2">
|
||
<OauthClientBadge profileId={group.profileId} size="lg" />
|
||
<span className="text-xs text-slate-400">
|
||
{group.profileId ? `谷歌账号 ${group.profileId}` : "未绑定配置档"}
|
||
</span>
|
||
<span className="rounded-full bg-white/5 px-2 py-0.5 text-[11px] text-slate-300">
|
||
{group.channels.length} 个频道
|
||
</span>
|
||
</div>
|
||
<div className="grid gap-3 sm:grid-cols-2">
|
||
{group.channels.map((ch) => {
|
||
const pct = Math.min(100, Math.round((ch.used / 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);
|
||
return (
|
||
<div
|
||
key={ch.id}
|
||
className={`rounded-xl border bg-gradient-to-br from-white/[0.04] to-transparent p-3 ${
|
||
gap <= 0 && ch.enabled !== false
|
||
? "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">
|
||
<OauthClientBadge profileId={ch.oauth_profile_id} size="sm" />
|
||
<div className="truncate font-medium text-slate-100">{ch.name || "未命名"}</div>
|
||
</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={ch.authorized ? "text-teal-300" : "text-amber-300"}>
|
||
{ch.authorized ? "已授权" : "未授权"}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
<div className="relative mt-3 h-1.5 overflow-hidden rounded-full bg-white/10">
|
||
<div
|
||
className={`h-full rounded-full ${pct >= 100 ? "bg-teal-400" : "bg-amber-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 || 0}
|
||
</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">
|
||
{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>
|
||
</div>
|
||
))
|
||
)}
|
||
</div>
|
||
</section>
|
||
|
||
<section
|
||
ref={rightPanelRef}
|
||
className="flex flex-col overflow-hidden rounded-2xl border border-white/10 bg-black/25 p-4"
|
||
>
|
||
<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">
|
||
北京日 {beijingWindow || "—"} · 按 Client A/B/C/D 分组 · 成片后自动排程上传
|
||
</p>
|
||
<div className="min-h-0 flex-1 space-y-3 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>
|
||
) : (
|
||
upcomingGroups.map((group) => (
|
||
<div key={group.profileId || "none"} className="space-y-2">
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<OauthClientBadge profileId={group.profileId} size="md" />
|
||
<span className="text-[11px] text-slate-400">{group.items.length} 条待传</span>
|
||
</div>
|
||
{group.items.map((u) => (
|
||
<div
|
||
key={u.id}
|
||
className="rounded-xl border border-white/10 bg-gradient-to-br from-white/[0.04] to-transparent px-3 py-2.5"
|
||
>
|
||
<div className="flex items-start justify-between gap-2">
|
||
<div className="min-w-0">
|
||
<div className="truncate text-sm text-slate-100">{u.anchor_name || "—"}</div>
|
||
<div className="mt-0.5 text-[11px] text-slate-500">
|
||
{channelName[String(u.channel_pk || "")] || "—"}
|
||
{u.upload_at ? ` · ${fmtTs(u.upload_at)}` : ""}
|
||
</div>
|
||
</div>
|
||
<span className={`shrink-0 rounded-md px-2 py-0.5 text-[11px] ${STATUS_STYLE[u.status] || ""}`}>
|
||
{u.status}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
))}
|
||
</div>
|
||
))
|
||
)}
|
||
</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"
|
||
min={0}
|
||
max={50}
|
||
value={Number(form.daily_max ?? 3)}
|
||
onChange={(e) =>
|
||
setForm((p) => ({ ...p, daily_max: Math.max(0, Number(e.target.value) || 0) }))
|
||
}
|
||
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 sm:col-span-2">
|
||
标题模板(可选 +1;默认从 100 条英文 SEO 池随机)
|
||
<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"
|
||
placeholder="留空则只用 SEO 标题池"
|
||
/>
|
||
</label>
|
||
<label className="block text-xs text-slate-400 sm:col-span-2">
|
||
描述模板(可选 +1;默认从 100 条英文 SEO 简介池随机)
|
||
<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"
|
||
placeholder="留空则只用 SEO 简介池"
|
||
/>
|
||
</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"
|
||
placeholder="可选备注"
|
||
/>
|
||
</label>
|
||
<label className="block text-xs text-slate-400">
|
||
OAuth 配置档
|
||
<select
|
||
value={form.oauth_profile_id || "A"}
|
||
onChange={(e) => setForm((p) => ({ ...p, oauth_profile_id: e.target.value.toUpperCase() }))}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-slate-900 px-3 py-2 text-sm"
|
||
>
|
||
{["A", "B", "C", "D"].map((p) => (
|
||
<option key={p} value={p}>
|
||
{p} · 谷歌账号 {p}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</label>
|
||
</div>
|
||
<div className="mt-3 flex flex-wrap gap-2">
|
||
<button
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => void saveChannel()}
|
||
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" /> : null}
|
||
保存
|
||
</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}>
|
||
{normalizeOauthProfileId(c.oauth_profile_id)
|
||
? `[Client ${normalizeOauthProfileId(c.oauth_profile_id)}] ${c.name}`
|
||
: 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>
|
||
<div className="table-scroll">
|
||
{loading && jobs.length === 0 ? (
|
||
<TableSkeleton rows={6} cols={5} />
|
||
) : (
|
||
<table className="w-full min-w-[720px] text-left text-xs sm:text-sm md:min-w-[920px]">
|
||
<thead className="text-xs text-slate-500">
|
||
<tr>
|
||
<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">
|
||
<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">
|
||
<div>{j.anchor_name || "—"}</div>
|
||
<div className="font-mono text-[10px] text-slate-500">{j.room_id || ""}</div>
|
||
</td>
|
||
<td className="py-2.5 pr-2 text-xs text-slate-400">
|
||
<div className="flex flex-wrap items-center gap-1.5">
|
||
<OauthClientBadge
|
||
profileId={channelProfileMap[String(j.channel_pk || "")]}
|
||
size="sm"
|
||
/>
|
||
<span>{channelName[String(j.channel_pk || "")] || "—"}</span>
|
||
</div>
|
||
</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 || j.clip_path ? (
|
||
<span className="text-slate-400">成片待传</span>
|
||
) : (
|
||
"—"
|
||
)}
|
||
</td>
|
||
<td className="py-2.5">
|
||
{["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>
|
||
) : (
|
||
<span className="text-[10px] text-slate-600">—</span>
|
||
)}
|
||
</td>
|
||
</tr>
|
||
))}
|
||
{jobs.length === 0 ? (
|
||
<tr>
|
||
<td colSpan={7} 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}
|
||
</Shell>
|
||
);
|
||
}
|