127 lines
4.0 KiB
TypeScript
127 lines
4.0 KiB
TypeScript
"use client";
|
|
|
|
import { useState, type ReactNode } from "react";
|
|
|
|
import { YouTubeLiveBadge } from "@/components/YouTubeLiveBadge";
|
|
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 (
|
|
<div
|
|
className={`${color} flex h-12 w-12 shrink-0 items-center justify-center rounded-full text-lg font-semibold text-white`}
|
|
title={name}
|
|
>
|
|
{letter}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Compact card header matching Google 授权页:头像 + YouTube 昵称。 */
|
|
export function YtChannelProfileSnippet({
|
|
profile,
|
|
fallbackName,
|
|
fallbackChannelId,
|
|
liveChannelPk,
|
|
trailing,
|
|
className = "",
|
|
}: {
|
|
profile?: YtProfileSnippet | null;
|
|
fallbackName?: string;
|
|
fallbackChannelId?: string;
|
|
liveChannelPk?: string;
|
|
trailing?: ReactNode;
|
|
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 : "";
|
|
|
|
return (
|
|
<div className={`flex min-w-0 gap-3 ${className}`}>
|
|
{showPhoto ? (
|
|
// eslint-disable-next-line @next/next/no-img-element
|
|
<img
|
|
src={thumb}
|
|
alt={title}
|
|
className="h-12 w-12 shrink-0 rounded-full object-cover ring-1 ring-white/10"
|
|
onError={() => setImgBroken(true)}
|
|
/>
|
|
) : (
|
|
<LetterAvatar name={title} />
|
|
)}
|
|
<div className="min-w-0 flex-1">
|
|
<div className="flex flex-wrap items-center gap-1.5">
|
|
<div className="truncate font-medium text-slate-100" title="YouTube 频道昵称(授权页同名)">
|
|
{title}
|
|
</div>
|
|
{liveChannelPk ? <YouTubeLiveBadge refKey={shortsLiveRef(liveChannelPk)} compact showOffline /> : null}
|
|
{trailing}
|
|
</div>
|
|
{handle ? <div className="truncate text-xs text-slate-400">{handle}</div> : null}
|
|
{alias ? <div className="truncate text-[11px] text-slate-500">内部备注 {alias}</div> : null}
|
|
<div className="mt-0.5 text-[11px] text-slate-500">
|
|
订阅 {stats?.subscriberCount ?? "—"} · 视频 {stats?.videoCount ?? "—"} · 播放 {stats?.viewCount ?? "—"}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|