This commit is contained in:
Developer
2026-03-14 18:25:13 +08:00
parent fb819798b1
commit ff659dcef7
13 changed files with 516 additions and 314 deletions

View File

@@ -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 };
}

View File

@@ -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]);