mirror of
https://gh-proxy.org/https://github.com/tiajinsha/JKVideo
synced 2026-07-07 23:18:38 +08:00
为尊重知识产权及相关法律法规,本仓库即日起停止后续维护与更新,不再接受新的 Issue 和 Pull Request。 现有代码仅作学习参考保留,请勿将本项目用于任何商业或违法用途。 感谢所有支持过本项目的朋友。
32 lines
947 B
TypeScript
32 lines
947 B
TypeScript
import { useState, useCallback, useEffect, useRef } from 'react';
|
|
import { getVideoRelated } from '../services/bilibili';
|
|
import type { VideoItem } from '../services/types';
|
|
|
|
export function useRelatedVideos(bvid: string) {
|
|
const [videos, setVideos] = useState<VideoItem[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const loadingRef = useRef(false);
|
|
|
|
// 切到不同 bvid 时立刻清空,避免新页面短暂显示上一支视频的推荐流
|
|
useEffect(() => {
|
|
setVideos([]);
|
|
}, [bvid]);
|
|
|
|
const load = useCallback(async () => {
|
|
if (loadingRef.current) return;
|
|
loadingRef.current = true;
|
|
setLoading(true);
|
|
try {
|
|
const data = await getVideoRelated(bvid);
|
|
setVideos(data);
|
|
} catch (e) {
|
|
console.warn('useRelatedVideos: failed', e);
|
|
} finally {
|
|
loadingRef.current = false;
|
|
setLoading(false);
|
|
}
|
|
}, [bvid]);
|
|
|
|
return { videos, loading, load, hasMore: false };
|
|
}
|