douyinWEB/frontend/src/components/YouTubeLiveBadge.tsx
2026-08-10 20:11:23 -05:00

78 lines
1.9 KiB
TypeScript

"use client";
import { LivePulse } from "@/components/ui";
import { useYouTubeLiveEntry, type YouTubeLiveEntry } from "@/lib/youtubeLive";
type Props = {
/** e.g. group:uuid, shorts:uuid, channel:UCxxx */
refKey?: string;
/** Direct entry override (probe tab, etc.) */
entry?: YouTubeLiveEntry | null;
className?: string;
/** Show stream title next to badge */
showTitle?: boolean;
compact?: boolean;
/** When not live, still show 离线/检测中 instead of hiding */
showOffline?: boolean;
};
export function YouTubeLiveBadge({
refKey,
entry,
className = "",
showTitle = false,
compact = false,
showOffline = false,
}: Props) {
const fromMap = useYouTubeLiveEntry(refKey);
const live = entry ?? fromMap;
if (!live) {
if (!showOffline) return null;
return (
<span className={`inline-flex items-center rounded px-1.5 py-0.5 text-[10px] text-slate-500 ring-1 ring-white/10 ${className}`}>
检测中
</span>
);
}
if (!live.is_live) {
if (!showOffline) return null;
const tip = live.error ? String(live.error) : "YouTube 未开播";
return (
<span
className={`inline-flex items-center rounded px-1.5 py-0.5 text-[10px] text-slate-500 ring-1 ring-white/10 ${className}`}
title={tip}
>
离线
</span>
);
}
const label = compact ? "LIVE" : "LIVE";
const inner = (
<>
<LivePulse label={label} />
{showTitle && live.title ? (
<span className="max-w-[180px] truncate text-[11px] text-rose-100/90">{live.title}</span>
) : null}
</>
);
if (live.live_url) {
return (
<a
href={live.live_url}
target="_blank"
rel="noreferrer"
className={`inline-flex items-center gap-2 hover:opacity-90 ${className}`}
title={live.title || "打开直播"}
>
{inner}
</a>
);
}
return <span className={`inline-flex items-center gap-2 ${className}`}>{inner}</span>;
}