mirror of
https://gh-proxy.org/https://github.com/tiajinsha/JKVideo
synced 2026-07-08 07:28:37 +08:00
1
This commit is contained in:
54
hooks/useLiveDetail.ts
Normal file
54
hooks/useLiveDetail.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { getLiveRoomDetail, getLiveAnchorInfo, getLiveStreamUrl } from '../services/bilibili';
|
||||
import type { LiveRoomDetail, LiveAnchorInfo, LiveStreamInfo } from '../services/types';
|
||||
|
||||
interface LiveDetailState {
|
||||
room: LiveRoomDetail | null;
|
||||
anchor: LiveAnchorInfo | null;
|
||||
stream: LiveStreamInfo | null;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
export function useLiveDetail(roomId: number) {
|
||||
const [state, setState] = useState<LiveDetailState>({
|
||||
room: null,
|
||||
anchor: null,
|
||||
stream: null,
|
||||
loading: true,
|
||||
error: null,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!roomId) return;
|
||||
let cancelled = false;
|
||||
|
||||
setState({ room: null, anchor: null, stream: null, loading: true, error: null });
|
||||
|
||||
async function fetch() {
|
||||
try {
|
||||
const [room, anchor] = await Promise.all([
|
||||
getLiveRoomDetail(roomId),
|
||||
getLiveAnchorInfo(roomId),
|
||||
]);
|
||||
if (cancelled) return;
|
||||
|
||||
let stream: LiveStreamInfo = { hlsUrl: '', flvUrl: '', qn: 0 };
|
||||
if (room.live_status === 1) {
|
||||
stream = await getLiveStreamUrl(roomId);
|
||||
}
|
||||
if (cancelled) return;
|
||||
|
||||
setState({ room, anchor, stream, loading: false, error: null });
|
||||
} catch (e: any) {
|
||||
if (cancelled) return;
|
||||
setState(prev => ({ ...prev, loading: false, error: e?.message ?? '加载失败' }));
|
||||
}
|
||||
}
|
||||
|
||||
fetch();
|
||||
return () => { cancelled = true; };
|
||||
}, [roomId]);
|
||||
|
||||
return state;
|
||||
}
|
||||
Reference in New Issue
Block a user