Expose /api/douyin/live-map from monitor DB with SSE-triggered client refresh, and show live/offline badges on assign, rooms, anchors, slots, and overview cards. Co-authored-by: Cursor <cursoragent@cursor.com>
103 lines
2.8 KiB
TypeScript
103 lines
2.8 KiB
TypeScript
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
|
|
|
import { fetchJson } from "@/lib/api";
|
|
import { useEventStream } from "@/lib/realtime";
|
|
|
|
export type DouyinLiveEntry = {
|
|
is_live?: boolean;
|
|
last_status?: string;
|
|
anchor?: string;
|
|
url?: string;
|
|
last_checked_at?: number;
|
|
active_session_id?: string;
|
|
};
|
|
|
|
export type DouyinLiveMap = {
|
|
ok?: boolean;
|
|
checked_at?: number;
|
|
live_count?: number;
|
|
source?: string;
|
|
entries?: Record<string, DouyinLiveEntry>;
|
|
};
|
|
|
|
const DouyinLiveContext = createContext<{
|
|
map: DouyinLiveMap | null;
|
|
loading: boolean;
|
|
refresh: () => Promise<void>;
|
|
}>({
|
|
map: null,
|
|
loading: false,
|
|
refresh: async () => {},
|
|
});
|
|
|
|
export function useDouyinLiveProviderValue(intervalMs = 10000) {
|
|
const [map, setMap] = useState<DouyinLiveMap | null>(null);
|
|
const [loading, setLoading] = useState(false);
|
|
const { tick, last } = useEventStream(true);
|
|
|
|
const refresh = useCallback(async (silent = false) => {
|
|
if (!silent) setLoading(true);
|
|
try {
|
|
const data = (await fetchJson("/api/douyin/live-map")) as DouyinLiveMap;
|
|
setMap(data);
|
|
} catch {
|
|
/* keep last map */
|
|
} finally {
|
|
if (!silent) setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
void refresh(true);
|
|
const id = window.setInterval(() => 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]);
|
|
|
|
useEffect(() => {
|
|
if (!last?.kind) return;
|
|
if (!["live", "offline", "risk"].includes(String(last.kind))) return;
|
|
void refresh(true);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [tick]);
|
|
|
|
return useMemo(
|
|
() => ({
|
|
map,
|
|
loading,
|
|
refresh: () => refresh(false),
|
|
}),
|
|
[map, loading, refresh],
|
|
);
|
|
}
|
|
|
|
export const DouyinLiveContextProvider = DouyinLiveContext.Provider;
|
|
|
|
export function useDouyinLiveMap() {
|
|
return useContext(DouyinLiveContext);
|
|
}
|
|
|
|
export function useDouyinLiveEntry(roomId?: string, fallback?: DouyinLiveEntry | null): DouyinLiveEntry | null {
|
|
const { map } = useDouyinLiveMap();
|
|
const rid = String(roomId || "").trim();
|
|
if (!rid) return fallback ?? null;
|
|
return map?.entries?.[rid] ?? fallback ?? null;
|
|
}
|
|
|
|
export function useDouyinLiveCount() {
|
|
const { map } = useDouyinLiveMap();
|
|
return Number(map?.live_count || 0);
|
|
}
|
|
|
|
export function resolveDouyinLive(entry: DouyinLiveEntry | null | undefined, lastStatus?: string) {
|
|
const status = String(entry?.last_status || lastStatus || "unknown").trim() || "unknown";
|
|
const isLive = entry?.is_live ?? status === "live";
|
|
return { status, isLive };
|
|
}
|