fix(youtube): show channel avatar and YouTube nickname on cards for OAuth matching

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
eric 2026-08-21 22:39:47 -05:00
parent b08b66724d
commit 2ee2775bfb
4 changed files with 124 additions and 56 deletions

View File

@ -106,16 +106,21 @@ def list_managed_channels() -> list[dict[str, Any]]:
rows = db.list_shorts_channels() rows = db.list_shorts_channels()
out = [] out = []
for ch in rows: for ch in rows:
sn = _snippet_from_channel_row(ch) or {}
out.append( out.append(
{ {
"id": ch.get("id"), "id": ch.get("id"),
"name": ch.get("name"), "name": ch.get("name"),
"youtube_channel_id": ch.get("youtube_channel_id"), "youtube_channel_id": ch.get("youtube_channel_id") or sn.get("youtube_channel_id") or "",
"enabled": bool(ch.get("enabled")), "enabled": bool(ch.get("enabled")),
"authorized": bool(str(ch.get("refresh_token") or "").strip()), "authorized": bool(str(ch.get("refresh_token") or "").strip()),
"daily_min": ch.get("daily_min"), "daily_min": ch.get("daily_min"),
"daily_max": ch.get("daily_max"), "daily_max": ch.get("daily_max"),
"privacy": ch.get("privacy"), "privacy": ch.get("privacy"),
"title": sn.get("title") or ch.get("name"),
"custom_url": sn.get("custom_url") or "",
"thumb_url": sn.get("thumb_url") or "",
"yt_profile": sn or None,
} }
) )
return out return out

View File

@ -24,6 +24,7 @@ import { useConfirm } from "@/components/ConfirmDialog";
import { useToast } from "@/components/Toast"; import { useToast } from "@/components/Toast";
import { YouTubeLiveBadge } from "@/components/YouTubeLiveBadge"; import { YouTubeLiveBadge } from "@/components/YouTubeLiveBadge";
import { YtAssignPanel } from "@/components/YtAssignPanel"; import { YtAssignPanel } from "@/components/YtAssignPanel";
import { YtChannelProfileSnippet } from "@/components/YtChannelProfileSnippet";
import { fetchJson, fmtTs } from "@/lib/api"; import { fetchJson, fmtTs } from "@/lib/api";
import { shortsLiveRef } from "@/lib/youtubeLive"; import { shortsLiveRef } from "@/lib/youtubeLive";
@ -38,6 +39,10 @@ type ManagedChannel = {
authorized?: boolean; authorized?: boolean;
daily_min?: number; daily_min?: number;
daily_max?: number; daily_max?: number;
title?: string;
thumb_url?: string;
custom_url?: string;
yt_profile?: any;
}; };
type YtVideo = { type YtVideo = {
@ -315,18 +320,31 @@ export default function YouTubePage() {
</div> </div>
) : ( ) : (
<> <>
<div className="mb-4 flex flex-wrap items-center gap-2"> <div className="mb-4 grid gap-2 sm:grid-cols-2">
<select {channels.map((c) => {
value={channelPk} const active = c.id === channelPk;
onChange={(e) => setChannelPk(e.target.value)} return (
className="w-full min-w-0 rounded-xl border border-white/10 bg-slate-900 px-3 py-2 text-sm sm:min-w-[220px]" <button
key={c.id}
type="button"
onClick={() => setChannelPk(c.id)}
className={`rounded-xl border p-3 text-left transition ${
active
? "border-rose-400/40 bg-rose-500/10"
: "border-white/10 bg-black/30 hover:bg-white/5"
}`}
> >
{channels.map((c) => ( <YtChannelProfileSnippet
<option key={c.id} value={c.id}> profile={c.yt_profile}
{c.name} {c.youtube_channel_id ? `(${c.youtube_channel_id.slice(0, 8)}…)` : ""} fallbackName={c.title || c.name}
</option> fallbackChannelId={c.youtube_channel_id || c.custom_url}
))} liveChannelPk={c.id}
</select> />
</button>
);
})}
</div>
<div className="mb-4 flex flex-wrap items-center gap-2">
{channelPk ? <YouTubeLiveBadge refKey={shortsLiveRef(channelPk)} /> : null} {channelPk ? <YouTubeLiveBadge refKey={shortsLiveRef(channelPk)} /> : null}
<button <button
type="button" type="button"

View File

@ -19,15 +19,17 @@ import { useConfirm } from "@/components/ConfirmDialog";
import { ImageLightbox } from "@/components/ImageLightbox"; import { ImageLightbox } from "@/components/ImageLightbox";
import { useToast } from "@/components/Toast"; import { useToast } from "@/components/Toast";
import { YtChannelProfileSnippet, type YtProfileSnippet } from "@/components/YtChannelProfileSnippet"; import { YtChannelProfileSnippet, type YtProfileSnippet } from "@/components/YtChannelProfileSnippet";
import { YouTubeLiveBadge } from "@/components/YouTubeLiveBadge";
import { fetchJson } from "@/lib/api"; import { fetchJson } from "@/lib/api";
import { shortsLiveRef } from "@/lib/youtubeLive";
type ManagedChannel = { type ManagedChannel = {
id: string; id: string;
name?: string; name?: string;
youtube_channel_id?: string; youtube_channel_id?: string;
authorized?: boolean; authorized?: boolean;
title?: string;
custom_url?: string;
thumb_url?: string;
yt_profile?: YtProfileSnippet | null;
}; };
type PoolUrl = { type PoolUrl = {
@ -120,36 +122,14 @@ function ManageStyleProfileCard({
fallbackName?: string; fallbackName?: string;
fallbackChannelId?: string; fallbackChannelId?: string;
}) { }) {
const title = String(profile?.title || fallbackName || "未命名").trim() || "未命名";
const handle =
String(profile?.custom_url || "").trim() ||
String(fallbackChannelId || profile?.youtube_channel_id || "").trim();
const thumb =
String(profile?.thumb_url || "").trim() ||
String(profile?.thumbnails?.medium?.url || profile?.thumbnails?.default?.url || "").trim();
const stats = profile?.statistics;
return ( return (
<div className="rounded-2xl border border-white/10 bg-black/30 p-4"> <div className="rounded-2xl border border-white/10 bg-black/30 p-4">
<div className="flex gap-3"> <YtChannelProfileSnippet
{thumb ? ( profile={profile}
// eslint-disable-next-line @next/next/no-img-element fallbackName={fallbackName}
<img src={thumb} alt="" className="h-14 w-14 rounded-full object-cover" /> fallbackChannelId={fallbackChannelId}
) : ( liveChannelPk={channelPk}
<div className="h-14 w-14 rounded-full bg-white/10" /> />
)}
<div className="min-w-0">
<div className="flex flex-wrap items-center gap-2">
<div className="font-display text-xl text-white">{title}</div>
{channelPk ? <YouTubeLiveBadge refKey={shortsLiveRef(channelPk)} compact /> : null}
</div>
<div className="text-xs text-slate-500">{handle || "—"}</div>
<div className="mt-1 text-xs text-slate-400">
订阅 {stats?.subscriberCount ?? "—"} · 视频 {stats?.videoCount ?? "—"} · 播放{" "}
{stats?.viewCount ?? "—"}
</div>
</div>
</div>
</div> </div>
); );
} }
@ -202,6 +182,26 @@ export function YtAssignPanel({ channels }: { channels: ManagedChannel[] }) {
() => new Set((selected?.featured || []).map((f) => String(f.pool_url_id))), () => new Set((selected?.featured || []).map((f) => String(f.pool_url_id))),
[selected] [selected]
); );
const channelById = useMemo(() => {
const m: Record<string, ManagedChannel> = {};
for (const c of channels) m[c.id] = c;
return m;
}, [channels]);
function profileFor(pk: string): YtProfileSnippet | undefined {
if (profiles[pk]) return profiles[pk];
const ch = channelById[pk];
if (ch?.yt_profile) return ch.yt_profile;
if (ch?.thumb_url || ch?.title) {
return {
title: ch.title,
thumb_url: ch.thumb_url,
custom_url: ch.custom_url,
youtube_channel_id: ch.youtube_channel_id,
};
}
return undefined;
}
const loadGroups = useCallback(async () => { const loadGroups = useCallback(async () => {
setBusy(true); setBusy(true);
@ -566,7 +566,8 @@ export function YtAssignPanel({ channels }: { channels: ManagedChannel[] }) {
<option value="">选择已授权频道…</option> <option value="">选择已授权频道…</option>
{availableChannels.map((c) => ( {availableChannels.map((c) => (
<option key={c.id} value={c.id}> <option key={c.id} value={c.id}>
{c.name} {c.youtube_channel_id ? `(${c.youtube_channel_id.slice(0, 8)}…)` : ""} {c.title || c.name}
{c.custom_url ? ` ${c.custom_url}` : ""}
</option> </option>
))} ))}
</select> </select>
@ -619,7 +620,7 @@ export function YtAssignPanel({ channels }: { channels: ManagedChannel[] }) {
}`} }`}
> >
<YtChannelProfileSnippet <YtChannelProfileSnippet
profile={profiles[pk]} profile={profileFor(pk)}
fallbackName={g.name} fallbackName={g.name}
fallbackChannelId={g.youtube_channel_id} fallbackChannelId={g.youtube_channel_id}
liveChannelPk={pk || undefined} liveChannelPk={pk || undefined}
@ -637,7 +638,7 @@ export function YtAssignPanel({ channels }: { channels: ManagedChannel[] }) {
{selected ? ( {selected ? (
<div className="space-y-4"> <div className="space-y-4">
<ManageStyleProfileCard <ManageStyleProfileCard
profile={profiles[String(selected.channel_pk || "")]} profile={profileFor(String(selected.channel_pk || ""))}
channelPk={selected.channel_pk} channelPk={selected.channel_pk}
fallbackName={selected.name} fallbackName={selected.name}
fallbackChannelId={selected.youtube_channel_id} fallbackChannelId={selected.youtube_channel_id}

View File

@ -1,6 +1,6 @@
"use client"; "use client";
import type { ReactNode } from "react"; import { useState, type ReactNode } from "react";
import { YouTubeLiveBadge } from "@/components/YouTubeLiveBadge"; import { YouTubeLiveBadge } from "@/components/YouTubeLiveBadge";
import { shortsLiveRef } from "@/lib/youtubeLive"; import { shortsLiveRef } from "@/lib/youtubeLive";
@ -22,7 +22,18 @@ export type YtProfileSnippet = {
}; };
}; };
function thumbOf(p?: YtProfileSnippet | null): string { 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 ""; if (!p) return "";
return ( return (
String(p.thumb_url || "").trim() || String(p.thumb_url || "").trim() ||
@ -37,7 +48,27 @@ function handleOf(p?: YtProfileSnippet | null, fallbackId?: string): string {
return raw.startsWith("@") ? raw : raw.startsWith("UC") ? raw : `@${raw.replace(/^@/, "")}`; return raw.startsWith("@") ? raw : raw.startsWith("UC") ? raw : `@${raw.replace(/^@/, "")}`;
} }
/** Compact card header matching /youtube 频道管理 profile strip. */ 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({ export function YtChannelProfileSnippet({
profile, profile,
fallbackName, fallbackName,
@ -53,27 +84,40 @@ export function YtChannelProfileSnippet({
trailing?: ReactNode; trailing?: ReactNode;
className?: string; className?: string;
}) { }) {
const title = String(profile?.title || fallbackName || "未命名").trim() || "未命名"; const ytName = String(profile?.title || "").trim();
const localName = String(fallbackName || "").trim();
const title = ytName || localName || "未命名";
const handle = handleOf(profile, fallbackChannelId); const handle = handleOf(profile, fallbackChannelId);
const thumb = thumbOf(profile); const thumb = thumbOf(profile);
const stats = profile?.statistics; const stats = profile?.statistics;
const [imgBroken, setImgBroken] = useState(false);
const showPhoto = Boolean(thumb) && !imgBroken;
const alias = localName && ytName && localName.toLowerCase() !== ytName.toLowerCase() ? localName : "";
return ( return (
<div className={`flex min-w-0 gap-2.5 ${className}`}> <div className={`flex min-w-0 gap-3 ${className}`}>
{thumb ? ( {showPhoto ? (
// eslint-disable-next-line @next/next/no-img-element // eslint-disable-next-line @next/next/no-img-element
<img src={thumb} alt="" className="h-11 w-11 shrink-0 rounded-full object-cover" /> <img
src={thumb}
alt={title}
className="h-12 w-12 shrink-0 rounded-full object-cover ring-1 ring-white/10"
onError={() => setImgBroken(true)}
/>
) : ( ) : (
<div className="h-11 w-11 shrink-0 rounded-full bg-white/10" /> <LetterAvatar name={title} />
)} )}
<div className="min-w-0 flex-1"> <div className="min-w-0 flex-1">
<div className="flex flex-wrap items-center gap-1.5"> <div className="flex flex-wrap items-center gap-1.5">
<div className="truncate font-medium text-slate-100">{title}</div> <div className="truncate font-medium text-slate-100" title="YouTube 频道昵称(授权页同名)">
{title}
</div>
{liveChannelPk ? <YouTubeLiveBadge refKey={shortsLiveRef(liveChannelPk)} compact showOffline /> : null} {liveChannelPk ? <YouTubeLiveBadge refKey={shortsLiveRef(liveChannelPk)} compact showOffline /> : null}
{trailing} {trailing}
</div> </div>
{handle ? <div className="truncate text-xs text-slate-500">{handle}</div> : null} {handle ? <div className="truncate text-xs text-slate-400">{handle}</div> : null}
<div className="mt-0.5 text-[11px] text-slate-400"> {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 ?? "—"} 订阅 {stats?.subscriberCount ?? "—"} · 视频 {stats?.videoCount ?? "—"} · 播放 {stats?.viewCount ?? "—"}
</div> </div>
</div> </div>