Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | 12x 12x 12x 12x 12x 12x 5x 5x 5x 5x 5x 1x 4x 1x 1x 5x 12x 4x 12x 1x 1x 1x 1x 12x 1x 1x 1x 1x 1x 1x 12x 1x 1x 1x 12x 12x 1x 1x 1x 12x | import { useCallback, useEffect, useState } from 'react';
import { supabaseForumService, type Post } from '@/src/lib/supabase';
import { showAlert, toast } from '@/src/components/dialogs';
/**
* 帖子收藏数据管理 Hook
*/
export function usePostCollectData() {
const [refreshing, setRefreshing] = useState(false);
const [favoritePosts, setFavoritePosts] = useState<Post[]>([]);
const [isLoadingPosts, setIsLoadingPosts] = useState(true);
const [postError, setPostError] = useState<string | null>(null);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);
// 获取帖子收藏列表
const fetchFavoritePosts = useCallback(async () => {
try {
setIsLoadingPosts(true);
setPostError(null);
const { data, error } = await supabaseForumService.getMyFavorites();
if (error) {
throw error;
}
setFavoritePosts(Array.isArray(data) ? data : []);
} catch (err) {
console.error('获取帖子收藏列表失败:', err);
setPostError('获取帖子收藏列表失败');
} finally {
setIsLoadingPosts(false);
}
}, []);
// 初始加载数据
useEffect(() => {
fetchFavoritePosts();
}, [fetchFavoritePosts]);
// 下拉刷新
const handleRefresh = useCallback(async () => {
setRefreshing(true);
try {
await fetchFavoritePosts();
} catch {
toast.error('刷新失败', '请检查网络连接后重试');
} finally {
setRefreshing(false);
}
}, [fetchFavoritePosts]);
// 取消收藏帖子
const handleDelete = useCallback((postId: number) => {
showAlert({
title: '确认取消收藏',
message: '您确定要取消收藏此帖子吗?',
type: 'warning',
buttons: [
{ text: '取消', style: 'cancel' },
{
text: '确定',
style: 'destructive',
onPress: async () => {
try {
const { error } = await supabaseForumService.toggleFavorite(postId);
Iif (error) throw error;
// 乐观更新:立即从列表中移除
setFavoritePosts((prev) => prev.filter((post) => post.id !== postId));
toast.success('已取消收藏');
} catch (err) {
toast.error('取消收藏失败', '请重试');
console.error('取消帖子收藏失败:', err);
}
},
},
],
});
}, []);
// 点击帖子,打开详情
const handlePress = useCallback(
(postId: number) => {
const post = favoritePosts.find((p) => p.id === postId);
Eif (post) {
setSelectedPost(post);
}
},
[favoritePosts]
);
// 关闭帖子详情
const closePostDetail = useCallback(() => {
setSelectedPost(null);
}, []);
// 帖子删除后的回调
const handlePostDeleted = useCallback(() => {
Eif (selectedPost) {
setFavoritePosts((prev) => prev.filter((post) => post.id !== selectedPost.id));
}
setSelectedPost(null);
}, [selectedPost]);
return {
favoritePosts,
isLoadingPosts,
postError,
refreshing,
handleRefresh,
handleDelete,
handlePress,
selectedPost,
closePostDetail,
handlePostDeleted,
};
}
|