douyinWEB/frontend/src/lib/youtubeLiveNav.ts
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

122 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { YouTubeLiveMap } from "@/lib/youtubeLive";
export type YouTubeLiveNavKind = "assign" | "group" | "shorts";
export type YouTubeLiveNavTarget = {
href: string;
refKey: string;
kind: YouTubeLiveNavKind;
id: string;
title?: string;
};
export function ytLiveAssignDomId(groupId: string) {
return `yt-live-assign-${groupId}`;
}
export function ytLiveGroupDomId(groupId: string) {
return `yt-live-group-${groupId}`;
}
export function ytLiveShortsDomId(channelPk: string) {
return `yt-live-shorts-${channelPk}`;
}
function assignNavTarget(groupId: string, refKey: string, title?: string): YouTubeLiveNavTarget {
return {
href: `/youtube?live=1&focus=${encodeURIComponent(groupId)}`,
refKey,
kind: "assign",
id: groupId,
title,
};
}
export function liveRefToNavTarget(
refKey: string,
entry?: { title?: string; assign_group_id?: string | null },
): YouTubeLiveNavTarget | null {
const title = entry?.title;
const assignId = String(entry?.assign_group_id || "").trim();
if (refKey.startsWith("assign:")) {
const id = refKey.slice("assign:".length);
if (!id) return null;
return assignNavTarget(id, refKey, title);
}
if (refKey.startsWith("shorts:")) {
const id = refKey.slice("shorts:".length);
if (!id) return null;
// Prefer Live 分配卡片;无绑定分组时仍进 /youtube,不进 Shorts 运维页。
if (assignId) return assignNavTarget(assignId, refKey, title);
return {
href: "/youtube?live=1",
refKey,
kind: "assign",
id: "",
title,
};
}
if (refKey.startsWith("group:")) {
const id = refKey.slice("group:".length);
if (!id) return null;
return { href: `/groups?focus=${encodeURIComponent(id)}`, refKey, kind: "group", id, title };
}
return null;
}
export function listLiveYouTubeNavTargets(map: YouTubeLiveMap | null): YouTubeLiveNavTarget[] {
const entries = map?.entries || {};
const out: YouTubeLiveNavTarget[] = [];
const seenAssign = new Set<string>();
const pushAssign = (groupId: string, refKey: string, title?: string) => {
if (!groupId || seenAssign.has(groupId)) return;
seenAssign.add(groupId);
out.push(assignNavTarget(groupId, refKey, title));
};
for (const [k, v] of Object.entries(entries)) {
if (!v?.is_live) continue;
if (k.startsWith("assign:")) {
pushAssign(k.slice("assign:".length), k, v.title);
}
}
for (const [k, v] of Object.entries(entries)) {
if (!v?.is_live) continue;
if (k.startsWith("shorts:") && v.assign_group_id) {
pushAssign(String(v.assign_group_id), k, v.title);
continue;
}
// Skip unbound shorts in the primary list — they would otherwise dilute /youtube focus.
if (k.startsWith("shorts:")) continue;
const t = liveRefToNavTarget(k, v);
if (t && t.kind !== "assign") out.push(t);
}
return out;
}
/** Primary link for header / ticker badge — always Live 分配 (/youtube), never Shorts. */
export function primaryYouTubeLiveNav(map: YouTubeLiveMap | null): YouTubeLiveNavTarget | null {
const lives = listLiveYouTubeNavTargets(map);
const assigns = lives.filter((l) => l.kind === "assign");
if (assigns.length === 1) return assigns[0];
if (assigns.length > 1) {
const first = assigns[0];
return { ...first, href: `/youtube?live=1&focus=${encodeURIComponent(first.id)}` };
}
const liveCount = Number(map?.live_count || 0);
const hasLiveEntries = Object.values(map?.entries || {}).some((v) => v?.is_live);
if (liveCount > 0 || hasLiveEntries || lives.length > 0) {
return {
href: "/youtube?live=1",
refKey: "",
kind: "assign",
id: "",
title: "查看 Live 分配在播频道",
};
}
return null;
}