mirror of
https://github.com/tiajinsha/JKVideo.git
synced 2026-04-04 22:49:02 +08:00
27 lines
781 B
TypeScript
27 lines
781 B
TypeScript
import { useState, useCallback, useRef } from 'react';
|
|
import { getVideoRelated } from '../services/api';
|
|
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);
|
|
|
|
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 };
|
|
}
|