mirror of
https://gh-proxy.org/https://github.com/tiajinsha/JKVideo
synced 2026-07-08 07:28:37 +08:00
feat: add all source files - services, store, hooks, components, screens
This commit is contained in:
30
hooks/useVideoDetail.ts
Normal file
30
hooks/useVideoDetail.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { getVideoDetail, getPlayUrl } from '../services/bilibili';
|
||||
import type { VideoItem } from '../services/types';
|
||||
|
||||
export function useVideoDetail(bvid: string) {
|
||||
const [video, setVideo] = useState<VideoItem | null>(null);
|
||||
const [streamUrl, setStreamUrl] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
async function fetchData() {
|
||||
try {
|
||||
setLoading(true);
|
||||
const detail = await getVideoDetail(bvid);
|
||||
setVideo(detail);
|
||||
const cid = detail.pages?.[0]?.cid ?? detail.cid;
|
||||
const playData = await getPlayUrl(bvid, cid);
|
||||
setStreamUrl(playData.durl[0]?.url ?? null);
|
||||
} catch (e: any) {
|
||||
setError(e.message ?? 'Load failed');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
if (bvid) fetchData();
|
||||
}, [bvid]);
|
||||
|
||||
return { video, streamUrl, loading, error };
|
||||
}
|
||||
Reference in New Issue
Block a user