mirror of
https://gh-proxy.org/https://github.com/tiajinsha/JKVideo
synced 2026-07-08 15:33:51 +08:00
1
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user