douyinWEB/frontend/src/components/SessionDetailModal.tsx
eric 47d17aee3c Document YouTube groups and Shorts side-path; sync dual-platform pipeline.
Add isolated Shorts OAuth/clip/upload worker (default off), youtube-svc + group poller, Beijing time UX, and SHORTS.md.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-10 00:04:06 -05:00

257 lines
10 KiB
TypeScript

"use client";
import { useState } from "react";
import Link from "next/link";
import { ExternalLink, Loader2, RefreshCw, Square } from "lucide-react";
import { ImageLightbox } from "@/components/ImageLightbox";
import { Modal } from "@/components/Modal";
import { RoomQuickActions } from "@/components/RoomQuickActions";
import { fetchJson, fmtDuration, fmtTs, sessionCoverSrc } from "@/lib/api";
export type SessionDetail = {
id: string;
anchor_name?: string;
source_url?: string;
room_id?: string;
room_pk?: 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 [zoom, setZoom] = useState(false);
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}
panelClassName="max-h-[94vh] w-full max-w-5xl overflow-y-auto rounded-2xl border border-white/10 bg-[#0b1626] p-4 shadow-2xl sm:p-5"
>
{current ? (
<>
<div className="flex items-start justify-between gap-3">
<div className="min-w-0">
<h2 className="truncate text-lg font-semibold">{current.anchor_name || "直播间详情"}</h2>
<p className="mt-1 line-clamp-2 text-sm text-slate-400">{current.title || "—"}</p>
</div>
<div className="flex shrink-0 flex-wrap items-center justify-end gap-2">
<RoomQuickActions
room={{
room_pk: current.room_pk,
room_id: current.room_id,
source_url: current.source_url,
anchor_name: current.anchor_name,
}}
onDone={() => onUpdated?.(current)}
onError={(m) => setErr(m)}
/>
{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 lg:grid-cols-[minmax(240px,42%)_1fr]">
{cover ? (
<button
type="button"
onClick={() => setZoom(true)}
className="group relative flex max-h-[72vh] min-h-[280px] items-center justify-center overflow-hidden rounded-xl bg-black/50 ring-1 ring-white/10"
title="点击放大封面"
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={cover}
alt=""
className="max-h-[72vh] w-full object-contain transition duration-300 group-hover:scale-[1.02]"
/>
<span className="pointer-events-none absolute bottom-2 right-2 rounded-md bg-black/60 px-2 py-1 text-[10px] text-slate-200 opacity-0 ring-1 ring-white/10 transition group-hover:opacity-100">
点击放大
</span>
</button>
) : (
<div className="flex min-h-[220px] items-center justify-center rounded-xl bg-slate-900 text-sm text-slate-500 ring-1 ring-white/10">
暂无截图
</div>
)}
<dl className="grid content-start gap-2.5 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 shrink-0" />
</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>
<div className="flex flex-wrap gap-2 pt-1">
{current.anchor_name ? (
<Link
href={`/sessions?q=${encodeURIComponent(current.anchor_name)}`}
className="rounded-lg border border-white/10 bg-white/5 px-2 py-1 text-xs text-teal-200 hover:bg-white/10"
onClick={onClose}
>
本主播场次
</Link>
) : null}
{current.room_id ? (
<Link
href={`/events?q=${encodeURIComponent(current.room_id)}`}
className="rounded-lg border border-white/10 bg-white/5 px-2 py-1 text-xs text-sky-200 hover:bg-white/10"
onClick={onClose}
>
本房事件
</Link>
) : null}
{current.anchor_name || current.room_id ? (
<Link
href={`/covers?q=${encodeURIComponent(current.anchor_name || current.room_id || "")}`}
className="rounded-lg border border-white/10 bg-white/5 px-2 py-1 text-xs text-fuchsia-200 hover:bg-white/10"
onClick={onClose}
>
相关截图
</Link>
) : null}
{current.anchor_name ? (
<Link
href={`/anchors?q=${encodeURIComponent(current.anchor_name)}`}
className="rounded-lg border border-white/10 bg-white/5 px-2 py-1 text-xs text-amber-100 hover:bg-white/10"
onClick={onClose}
>
主播画像
</Link>
) : null}
</div>
</dl>
</div>
</>
) : null}
</Modal>
{zoom && cover ? (
<ImageLightbox
src={cover}
title={current?.anchor_name || current?.room_id || "封面"}
subtitle={current?.source_url}
onClose={() => setZoom(false)}
/>
) : null}
</>
);
}