douyinWEB/frontend/src/lib/youtubeLive.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

108 lines
2.6 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.

"use client";
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
import { fetchJson } from "@/lib/api";
export type YouTubeLiveEntry = {
is_live?: boolean;
live_url?: string;
title?: string;
channel_id?: string;
source?: string;
error?: string;
/** yt_assign_groups.id when this Shorts channel has a Live 分配卡片 */
assign_group_id?: string;
};
export type YouTubeLiveMap = {
ok?: boolean;
checked_at?: number;
live_count?: number;
source?: string;
entries?: Record<string, YouTubeLiveEntry>;
svc_error?: string;
};
const YouTubeLiveContext = createContext<{
map: YouTubeLiveMap | null;
loading: boolean;
refresh: () => Promise<void>;
}>({
map: null,
loading: false,
refresh: async () => {},
});
export function groupLiveRef(groupId: string) {
return `group:${groupId}`;
}
export function shortsLiveRef(channelPk: string) {
return `shorts:${channelPk}`;
}
export function channelLiveRef(channelId: string) {
return `channel:${channelId}`;
}
export function useYouTubeLiveProviderValue(intervalMs = 35000) {
const [map, setMap] = useState<YouTubeLiveMap | null>(null);
const [loading, setLoading] = useState(false);
const refresh = useCallback(async (silent = false) => {
if (!silent) setLoading(true);
try {
// 不 force:读服务端后台轮询写入的缓存(约 15–20s 更新)
const data = (await fetchJson("/api/youtube/live-map")) as YouTubeLiveMap;
setMap(data);
} catch {
/* keep last map */
} finally {
if (!silent) setLoading(false);
}
}, []);
useEffect(() => {
void refresh(true);
const id = window.setInterval(() => {
if (document.hidden) return;
void refresh(true);
}, intervalMs);
const onVis = () => {
if (document.visibilityState === "visible") void refresh(true);
};
document.addEventListener("visibilitychange", onVis);
return () => {
window.clearInterval(id);
document.removeEventListener("visibilitychange", onVis);
};
}, [intervalMs, refresh]);
return useMemo(
() => ({
map,
loading,
refresh: () => refresh(false),
}),
[map, loading, refresh],
);
}
export const YouTubeLiveContextProvider = YouTubeLiveContext.Provider;
export function useYouTubeLiveMap() {
return useContext(YouTubeLiveContext);
}
export function useYouTubeLiveEntry(refKey?: string): YouTubeLiveEntry | null {
const { map } = useYouTubeLiveMap();
if (!refKey) return null;
return map?.entries?.[refKey] ?? null;
}
export function useYouTubeLiveCount() {
const { map } = useYouTubeLiveMap();
return Number(map?.live_count || 0);
}