"use client"; import { useState, type ReactNode } from "react"; import { YouTubeLiveBadge } from "@/components/YouTubeLiveBadge"; import { YtChannelMetaBadges, type YtChannelMeta } from "@/components/YtChannelMetaBadges"; import { shortsLiveRef } from "@/lib/youtubeLive"; export type YtProfileSnippet = { title?: string; custom_url?: string; youtube_channel_id?: string; thumbnails?: { default?: { url?: string }; medium?: { url?: string }; high?: { url?: string }; }; thumb_url?: string; statistics?: { subscriberCount?: string | number; videoCount?: string | number; viewCount?: string | number; }; }; const AVATAR_COLORS = [ "bg-purple-600", "bg-fuchsia-600", "bg-orange-500", "bg-violet-600", "bg-stone-600", "bg-amber-600", "bg-rose-700", "bg-sky-700", ]; export function thumbOf(p?: YtProfileSnippet | null): string { if (!p) return ""; return ( String(p.thumb_url || "").trim() || String(p.thumbnails?.medium?.url || p.thumbnails?.default?.url || p.thumbnails?.high?.url || "").trim() ); } function handleOf(p?: YtProfileSnippet | null, fallbackId?: string): string { const raw = String(p?.custom_url || "").trim() || String(fallbackId || p?.youtube_channel_id || "").trim(); if (!raw) return ""; if (raw.startsWith("http")) return raw.replace(/^https?:\/\/(www\.)?youtube\.com\//i, ""); return raw.startsWith("@") ? raw : raw.startsWith("UC") ? raw : `@${raw.replace(/^@/, "")}`; } function letterStyle(name: string): { letter: string; color: string } { const t = name.trim() || "?"; const letter = t[0].toUpperCase(); let h = 0; for (let i = 0; i < t.length; i += 1) h = (h * 31 + t.charCodeAt(i)) >>> 0; return { letter, color: AVATAR_COLORS[h % AVATAR_COLORS.length] }; } function LetterAvatar({ name }: { name: string }) { const { letter, color } = letterStyle(name); return (
{letter}
); } /** Compact card header matching Google 授权页:头像 + YouTube 昵称。 */ export function YtChannelProfileSnippet({ profile, fallbackName, fallbackChannelId, liveChannelPk, trailing, meta, metaLayout = "column", compact = false, className = "", }: { profile?: YtProfileSnippet | null; fallbackName?: string; fallbackChannelId?: string; liveChannelPk?: string; trailing?: ReactNode; meta?: YtChannelMeta | null; metaLayout?: "column" | "row"; compact?: boolean; className?: string; }) { const ytName = String(profile?.title || "").trim(); const localName = String(fallbackName || "").trim(); const title = ytName || localName || "未命名"; const handle = handleOf(profile, fallbackChannelId); const thumb = thumbOf(profile); const stats = profile?.statistics; const [imgBroken, setImgBroken] = useState(false); const showPhoto = Boolean(thumb) && !imgBroken; const alias = localName && ytName && localName.toLowerCase() !== ytName.toLowerCase() ? localName : ""; const avatarCls = compact ? "h-10 w-10" : "h-12 w-12"; const titleCls = compact ? "text-sm" : ""; return (
{showPhoto ? ( // eslint-disable-next-line @next/next/no-img-element {title} setImgBroken(true)} /> ) : (
)}
{title}
{liveChannelPk ? : null} {trailing}
{handle ?
{handle}
: null} {alias && !compact ?
内部备注 {alias}
: null} {!compact ? (
订阅 {stats?.subscriberCount ?? "—"} · 视频 {stats?.videoCount ?? "—"} · 播放 {stats?.viewCount ?? "—"}
) : null} {meta && (compact || metaLayout === "row") ? (
) : null}
{meta && !compact && metaLayout === "column" ? : null}
); }