Files
JKVideo/hooks/useRelatedVideos.ts

32 lines
947 B
TypeScript
Raw Normal View History

import { useState, useCallback, useEffect, useRef } from 'react';
import { getVideoRelated } from '../services/bilibili';
2026-03-19 16:34:56 +08:00
import type { VideoItem } from '../services/types';
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);
// 切到不同 bvid 时立刻清空,避免新页面短暂显示上一支视频的推荐流
useEffect(() => {
setVideos([]);
}, [bvid]);
2026-03-19 16:34:56 +08:00
const load = useCallback(async () => {
if (loadingRef.current) return;
loadingRef.current = true;
setLoading(true);
try {
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);
}
}, [bvid]);
2026-03-19 16:34:56 +08:00
return { videos, loading, load, hasMore: false };
2026-03-19 16:34:56 +08:00
}