mirror of
https://gh-proxy.org/https://github.com/tiajinsha/JKVideo
synced 2026-07-09 15:56:06 +08:00
feat: add all source files - services, store, hooks, components, screens
This commit is contained in:
27
hooks/useComments.ts
Normal file
27
hooks/useComments.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { useState, useCallback } 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 load = useCallback(async () => {
|
||||
if (loading || !hasMore || !aid) return;
|
||||
setLoading(true);
|
||||
try {
|
||||
const data = await getComments(aid, page);
|
||||
if (data.length === 0) { setHasMore(false); return; }
|
||||
setComments(prev => [...prev, ...data]);
|
||||
setPage(p => p + 1);
|
||||
} catch (e) {
|
||||
console.error('Failed to load comments', e);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [aid, page, loading, hasMore]);
|
||||
|
||||
return { comments, loading, hasMore, load };
|
||||
}
|
||||
Reference in New Issue
Block a user