mirror of
https://gh-proxy.org/https://github.com/tiajinsha/JKVideo
synced 2026-07-07 23:18:38 +08:00
bug修改
This commit is contained in:
@@ -1,14 +1,12 @@
|
||||
import { useState, useCallback, useRef, useMemo } from 'react';
|
||||
import { getRecommendFeed, getLiveList } from '../services/bilibili';
|
||||
import type { VideoItem, LiveRoom } from '../services/types';
|
||||
import { getRecommendFeed } from '../services/bilibili';
|
||||
import type { VideoItem } 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);
|
||||
|
||||
// Use refs to avoid stale closures — load() has stable identity
|
||||
const loadingRef = useRef(false);
|
||||
const freshIdxRef = useRef(0);
|
||||
|
||||
@@ -21,19 +19,8 @@ export function useVideoList() {
|
||||
const idx = freshIdxRef.current;
|
||||
setLoading(true);
|
||||
try {
|
||||
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);
|
||||
const data = await getRecommendFeed(idx);
|
||||
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, 10);
|
||||
setLiveRooms(sorted);
|
||||
}
|
||||
freshIdxRef.current = idx + 1;
|
||||
} catch (e) {
|
||||
console.error('Failed to load videos', e);
|
||||
@@ -42,7 +29,7 @@ export function useVideoList() {
|
||||
setLoading(false);
|
||||
setRefreshing(false);
|
||||
}
|
||||
}, []); // stable — no stale closure risk
|
||||
}, []);
|
||||
|
||||
const refresh = useCallback(() => {
|
||||
console.log('Refreshing video list');
|
||||
@@ -51,5 +38,5 @@ export function useVideoList() {
|
||||
}, [load]);
|
||||
|
||||
const videos = useMemo(() => pages.flat(), [pages]);
|
||||
return { videos, pages, liveRooms, loading, refreshing, load, refresh };
|
||||
return { videos, pages, loading, refreshing, load, refresh };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user