mirror of
https://gh-proxy.org/https://github.com/tiajinsha/JKVideo
synced 2026-07-08 07:28:37 +08:00
优化
This commit is contained in:
@@ -1,27 +1,30 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
import { useState, useCallback, useRef } from 'react';
|
||||
import { getComments } from '../services/bilibili';
|
||||
import type { Comment } from '../services/types';
|
||||
|
||||
export function useComments(aid: number) {
|
||||
const [comments, setComments] = useState<Comment[]>([]);
|
||||
const [page, setPage] = useState(1);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [hasMore, setHasMore] = useState(true);
|
||||
const pageRef = useRef(1);
|
||||
const loadingRef = useRef(false);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
if (loading || !hasMore || !aid) return;
|
||||
if (loadingRef.current || !hasMore || !aid) return;
|
||||
loadingRef.current = true;
|
||||
setLoading(true);
|
||||
try {
|
||||
const data = await getComments(aid, page);
|
||||
const data = await getComments(aid, pageRef.current);
|
||||
if (data.length === 0) { setHasMore(false); return; }
|
||||
setComments(prev => [...prev, ...data]);
|
||||
setPage(p => p + 1);
|
||||
pageRef.current += 1;
|
||||
} catch (e) {
|
||||
console.error('Failed to load comments', e);
|
||||
} finally {
|
||||
loadingRef.current = false;
|
||||
setLoading(false);
|
||||
}
|
||||
}, [aid, page, loading, hasMore]);
|
||||
}, [aid, hasMore]);
|
||||
|
||||
return { comments, loading, hasMore, load };
|
||||
}
|
||||
|
||||
@@ -52,7 +52,9 @@ export function useVideoDetail(bvid: string) {
|
||||
// 登录状态变化时重新拉取清晰度列表(登录后可能获得更高画质)
|
||||
useEffect(() => {
|
||||
if (cidRef.current) {
|
||||
fetchPlayData(cidRef.current, 120, true).catch(() => {});
|
||||
fetchPlayData(cidRef.current, 120, true).catch((e) => {
|
||||
console.warn('Failed to refresh quality list after login change:', e);
|
||||
});
|
||||
}
|
||||
}, [isLoggedIn]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user