This commit is contained in:
Developer
2026-03-14 11:55:45 +08:00
parent a971a65f96
commit e9b00d026f
6 changed files with 102 additions and 108 deletions

View File

@@ -1,9 +1,10 @@
import { useState, useCallback, useRef, useMemo } from 'react';
import { getRecommendFeed } from '../services/bilibili';
import type { VideoItem } from '../services/types';
import { getRecommendFeed, getLiveList } from '../services/bilibili';
import type { VideoItem, LiveRoom } from '../services/types';
export function useVideoList() {
const [pages, setPages] = useState<VideoItem[][]>([]);
const [liveRooms, setLiveRooms] = useState<LiveRoom[]>([]);
const [loading, setLoading] = useState(false);
const [refreshing, setRefreshing] = useState(false);
@@ -17,8 +18,19 @@ export function useVideoList() {
const idx = reset ? 0 : freshIdxRef.current;
setLoading(true);
try {
const data = await getRecommendFeed(idx);
const promises: [Promise<VideoItem[]>, Promise<LiveRoom[]>] = [
getRecommendFeed(idx),
reset || idx === 0
? getLiveList(1, 0).catch(() => [] as LiveRoom[])
: Promise.resolve([] as LiveRoom[]),
];
const [data, live] = await Promise.all(promises);
setPages(prev => reset ? [data] : [...prev, data]);
if (reset || idx === 0) {
// Take top 2 by online count
const sorted = [...live].sort((a, b) => b.online - a.online).slice(0, 2);
setLiveRooms(sorted);
}
freshIdxRef.current = idx + 1;
} catch (e) {
console.error('Failed to load videos', e);
@@ -36,5 +48,5 @@ export function useVideoList() {
const videos = useMemo(() => pages.flat(), [pages]);
return { videos, pages, loading, refreshing, load, refresh };
return { videos, pages, liveRooms, loading, refreshing, load, refresh };
}