feat: add all source files - services, store, hooks, components, screens

This commit is contained in:
Developer
2026-03-05 18:02:54 +08:00
parent bbe0df6ed2
commit a0e53bd073
15 changed files with 755 additions and 0 deletions

27
hooks/useComments.ts Normal file
View 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 };
}

30
hooks/useVideoDetail.ts Normal file
View File

@@ -0,0 +1,30 @@
import { useState, useEffect } from 'react';
import { getVideoDetail, getPlayUrl } from '../services/bilibili';
import type { VideoItem } from '../services/types';
export function useVideoDetail(bvid: string) {
const [video, setVideo] = useState<VideoItem | null>(null);
const [streamUrl, setStreamUrl] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
async function fetchData() {
try {
setLoading(true);
const detail = await getVideoDetail(bvid);
setVideo(detail);
const cid = detail.pages?.[0]?.cid ?? detail.cid;
const playData = await getPlayUrl(bvid, cid);
setStreamUrl(playData.durl[0]?.url ?? null);
} catch (e: any) {
setError(e.message ?? 'Load failed');
} finally {
setLoading(false);
}
}
if (bvid) fetchData();
}, [bvid]);
return { video, streamUrl, loading, error };
}

34
hooks/useVideoList.ts Normal file
View File

@@ -0,0 +1,34 @@
import { useState, useCallback } from 'react';
import { getPopularVideos } from '../services/bilibili';
import type { VideoItem } from '../services/types';
export function useVideoList() {
const [videos, setVideos] = useState<VideoItem[]>([]);
const [page, setPage] = useState(1);
const [loading, setLoading] = useState(false);
const [refreshing, setRefreshing] = useState(false);
const load = useCallback(async (reset = false) => {
if (loading) return;
const nextPage = reset ? 1 : page;
setLoading(true);
try {
const data = await getPopularVideos(nextPage);
setVideos(prev => reset ? data : [...prev, ...data]);
setPage(nextPage + 1);
} catch (e) {
console.error('Failed to load videos', e);
} finally {
setLoading(false);
setRefreshing(false);
}
}, [loading, page]);
const refresh = useCallback(() => {
setRefreshing(true);
setPage(1);
load(true);
}, [load]);
return { videos, loading, refreshing, load, refresh };
}