This commit is contained in:
Developer
2026-03-10 19:22:38 +08:00
parent cf20b016ff
commit 4dbb3cb3d6
5 changed files with 120 additions and 12 deletions

View File

@@ -1,32 +1,31 @@
import { useState, useCallback } from 'react';
import { getPopularVideos } from '../services/bilibili';
import { getRecommendFeed } from '../services/bilibili';
import type { VideoItem } from '../services/types';
export function useVideoList() {
const [videos, setVideos] = useState<VideoItem[]>([]);
const [page, setPage] = useState(1);
const [freshIdx, setFreshIdx] = useState(0);
const [loading, setLoading] = useState(false);
const [refreshing, setRefreshing] = useState(false);
const load = useCallback(async (reset = false) => {
if (loading) return;
const nextPage = reset ? 1 : page;
const idx = reset ? 0 : freshIdx;
setLoading(true);
try {
const data = await getPopularVideos(nextPage);
const data = await getRecommendFeed(idx);
setVideos(prev => reset ? data : [...prev, ...data]);
setPage(nextPage + 1);
setFreshIdx(idx + 1);
} catch (e) {
console.error('Failed to load videos', e);
} finally {
setLoading(false);
setRefreshing(false);
}
}, [loading, page]);
}, [loading, freshIdx]);
const refresh = useCallback(() => {
setRefreshing(true);
setPage(1);
load(true);
}, [load]);