2026-03-25 12:03:28 +08:00
|
|
|
import { useState, useCallback, useRef } from 'react';
|
|
|
|
|
import { getVideoRelated } from '../services/bilibili';
|
2026-03-19 16:34:56 +08:00
|
|
|
import type { VideoItem } from '../services/types';
|
|
|
|
|
|
2026-03-25 12:03:28 +08:00
|
|
|
export function useRelatedVideos(bvid: string) {
|
|
|
|
|
const [videos, setVideos] = useState<VideoItem[]>([]);
|
2026-03-19 16:34:56 +08:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const loadingRef = useRef(false);
|
|
|
|
|
|
|
|
|
|
const load = useCallback(async () => {
|
|
|
|
|
if (loadingRef.current) return;
|
|
|
|
|
loadingRef.current = true;
|
|
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
2026-03-25 12:03:28 +08:00
|
|
|
const data = await getVideoRelated(bvid);
|
|
|
|
|
setVideos(data);
|
2026-03-19 16:34:56 +08:00
|
|
|
} catch (e) {
|
|
|
|
|
console.warn('useRelatedVideos: failed', e);
|
|
|
|
|
} finally {
|
|
|
|
|
loadingRef.current = false;
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
2026-03-25 12:03:28 +08:00
|
|
|
}, [bvid]);
|
2026-03-19 16:34:56 +08:00
|
|
|
|
2026-03-25 12:03:28 +08:00
|
|
|
return { videos, loading, load, hasMore: false };
|
2026-03-19 16:34:56 +08:00
|
|
|
}
|