douyinWEB/frontend/src/components/SessionDetailModal.tsx
eric a360eec2ba Document slots/covers/PB ops UX and sync feature pass to main.
Ship zombie reconcile, S3 covers, session end/drill-down, rooms ops, and refresh README for production.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-08 20:07:15 -05:00

173 lines
6.4 KiB
TypeScript

"use client";
import { useState } from "react";
import { ExternalLink, Loader2, RefreshCw, Square } from "lucide-react";
import { Modal } from "@/components/Modal";
import { fetchJson, fmtDuration, fmtTs, sessionCoverSrc } from "@/lib/api";
export type SessionDetail = {
id: string;
anchor_name?: string;
source_url?: string;
room_id?: string;
title?: string;
started_at?: number;
ended_at?: number | null;
duration_seconds?: number;
cover_url?: string;
cover_proxy_url?: string;
status?: string;
end_reason?: string;
};
type Props = {
session: SessionDetail | null;
onClose: () => void;
onUpdated?: (session: SessionDetail) => void;
extraRows?: Array<{ label: string; value: React.ReactNode }>;
};
export function SessionDetailModal({ session, onClose, onUpdated, extraRows }: Props) {
const [busy, setBusy] = useState("");
const [err, setErr] = useState("");
const [local, setLocal] = useState<SessionDetail | null>(null);
const current = local && local.id === session?.id ? local : session;
const cover = sessionCoverSrc(current || undefined);
const isActive = Boolean(current && !current.ended_at);
async function recapture() {
if (!current?.id) return;
setBusy("cover");
setErr("");
try {
await fetchJson(`/api/sessions/${current.id}/recapture`, { method: "POST", body: "{}" });
const data = await fetchJson(`/api/sessions/${current.id}`);
const next = (data.session || data) as SessionDetail;
setLocal(next);
onUpdated?.(next);
} catch (e) {
setErr((e as Error).message);
} finally {
setBusy("");
}
}
async function endNow() {
if (!current?.id) return;
if (!confirm(`确认手动结束「${current.anchor_name || current.room_id}」这场?`)) return;
setBusy("end");
setErr("");
try {
const data = await fetchJson(`/api/sessions/${current.id}/end`, { method: "POST" });
const next = (data.session || data) as SessionDetail;
setLocal(next);
onUpdated?.(next);
} catch (e) {
setErr((e as Error).message);
} finally {
setBusy("");
}
}
return (
<Modal open={Boolean(session)} onClose={onClose}>
{current ? (
<>
<div className="flex items-start justify-between gap-3">
<div>
<h2 className="text-lg font-semibold">{current.anchor_name || "直播间详情"}</h2>
<p className="mt-1 text-sm text-slate-400">{current.title || "—"}</p>
</div>
<div className="flex flex-wrap items-center justify-end gap-2">
{isActive ? (
<button
type="button"
disabled={Boolean(busy)}
onClick={() => void endNow()}
className="inline-flex items-center gap-1 rounded-lg border border-amber-500/30 bg-amber-500/10 px-2.5 py-1 text-xs text-amber-100 hover:bg-amber-500/20 disabled:opacity-50"
>
{busy === "end" ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Square className="h-3.5 w-3.5" />}
结束场次
</button>
) : null}
<button
type="button"
disabled={Boolean(busy)}
onClick={() => void recapture()}
className="inline-flex items-center gap-1 rounded-lg border border-teal-500/30 bg-teal-500/10 px-2.5 py-1 text-xs text-teal-100 hover:bg-teal-500/20 disabled:opacity-50"
>
{busy === "cover" ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <RefreshCw className="h-3.5 w-3.5" />}
补抓封面
</button>
<button
type="button"
className="rounded-lg border border-white/10 bg-white/5 px-2.5 py-1 text-xs text-slate-300 hover:bg-white/10"
onClick={onClose}
>
关闭 Esc
</button>
</div>
</div>
{err ? <div className="mt-2 rounded-lg border border-rose-500/30 bg-rose-500/10 px-3 py-2 text-xs text-rose-100">{err}</div> : null}
<div className="mt-4 grid gap-4 md:grid-cols-[8rem_1fr]">
{cover ? (
// eslint-disable-next-line @next/next/no-img-element
<img src={cover} alt="" className="h-36 w-full rounded-lg object-cover ring-1 ring-white/10" />
) : (
<div className="flex h-36 items-center justify-center rounded-lg bg-slate-900 text-xs text-slate-500 ring-1 ring-white/10">
暂无截图
</div>
)}
<dl className="grid gap-2 text-sm">
<div>
<dt className="text-slate-500">直播间 URL</dt>
<dd className="break-all font-mono text-cyan-300">
{current.source_url ? (
<a
href={current.source_url}
target="_blank"
rel="noreferrer"
className="inline-flex items-center gap-1 hover:underline"
>
{current.source_url} <ExternalLink className="h-3.5 w-3.5" />
</a>
) : (
"—"
)}
</dd>
</div>
<div>
<dt className="text-slate-500">房间号</dt>
<dd>{current.room_id || "—"}</dd>
</div>
{extraRows?.map((row) => (
<div key={row.label}>
<dt className="text-slate-500">{row.label}</dt>
<dd>{row.value}</dd>
</div>
))}
<div>
<dt className="text-slate-500">开播</dt>
<dd>{fmtTs(current.started_at)}</dd>
</div>
<div>
<dt className="text-slate-500">停播</dt>
<dd>{current.ended_at ? fmtTs(current.ended_at) : "进行中"}</dd>
</div>
<div>
<dt className="text-slate-500">总时长</dt>
<dd>{fmtDuration(current.duration_seconds)}</dd>
</div>
<div>
<dt className="text-slate-500">结束原因</dt>
<dd>{current.end_reason || "—"}</dd>
</div>
</dl>
</div>
</>
) : null}
</Modal>
);
}