All files / app/(tabs)/collect/hooks useReportCollectData.ts

80.43% Statements 37/46
50% Branches 1/2
80% Functions 8/10
79.54% Lines 35/44

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                    8x 8x 8x 8x 8x 8x     8x 3x 3x 3x 3x 2x   1x 1x   3x         8x 3x       8x                       8x 1x                   1x 1x   1x 1x                       8x 2x   2x 2x     2x 1x   1x 1x 1x         8x         8x                          
import { useCallback, useEffect, useState } from 'react';
 
import type { AIReportData, FavoriteReport } from '@/src/services/api';
import { aiReportService } from '@/src/services/api';
import { showAlert, toast } from '@/src/components/dialogs';
 
/**
 * 报告收藏数据管理 Hook
 */
export function useReportCollectData() {
  const [refreshing, setRefreshing] = useState(false);
  const [selectedReport, setSelectedReport] = useState<AIReportData | null>(null);
  const [isReportModalVisible, setIsReportModalVisible] = useState(false);
  const [favoriteReports, setFavoriteReports] = useState<FavoriteReport[]>([]);
  const [isLoadingReports, setIsLoadingReports] = useState(true);
  const [reportError, setReportError] = useState<string | null>(null);
 
  // 获取报告收藏列表
  const fetchFavoriteReports = useCallback(async () => {
    try {
      setIsLoadingReports(true);
      setReportError(null);
      const reports = await aiReportService.getFavoriteReports();
      setFavoriteReports(Array.isArray(reports) ? reports : []);
    } catch (err) {
      console.error('获取报告收藏列表失败:', err);
      setReportError('获取报告收藏列表失败');
    } finally {
      setIsLoadingReports(false);
    }
  }, []);
 
  // 初始加载数据
  useEffect(() => {
    fetchFavoriteReports();
  }, [fetchFavoriteReports]);
 
  // 下拉刷新
  const handleRefresh = useCallback(async () => {
    setRefreshing(true);
    try {
      await fetchFavoriteReports();
    } catch {
      toast.error('刷新失败', '请检查网络连接后重试');
    } finally {
      setRefreshing(false);
    }
  }, [fetchFavoriteReports]);
 
  // 删除报告收藏
  const handleDelete = useCallback((favoriteId: number) => {
    showAlert({
      title: '确认取消收藏',
      message: '您确定要取消收藏此报告吗?',
      type: 'warning',
      buttons: [
        { text: '取消', style: 'cancel' },
        {
          text: '确定',
          style: 'destructive',
          onPress: async () => {
            try {
              await aiReportService.deleteFavoriteReport(favoriteId);
              // 乐观更新:立即从列表中移除
              setFavoriteReports((prev) => prev.filter((fav) => fav.id !== favoriteId));
              toast.success('已取消收藏');
            } catch (err) {
              toast.error('取消收藏失败', '请重试');
              console.error('删除报告收藏失败:', err);
            }
          },
        },
      ],
    });
  }, []);
 
  // 点击报告,获取完整报告数据并打开报告详情模态框
  const handlePress = useCallback(async (catfoodId: number) => {
    try {
      // 显示加载状态
      setIsReportModalVisible(true);
      setSelectedReport(null);
 
      // 获取完整的报告数据
      const fullReport = await aiReportService.getReport(catfoodId);
      setSelectedReport(fullReport);
    } catch (err) {
      console.error('获取报告详情失败:', err);
      toast.error('获取报告详情失败', '请重试');
      setIsReportModalVisible(false);
    }
  }, []);
 
  // 关闭报告模态框
  const closeReportModal = useCallback(() => {
    setIsReportModalVisible(false);
    setSelectedReport(null);
  }, []);
 
  return {
    favoriteReports,
    isLoadingReports,
    reportError,
    refreshing,
    handleRefresh,
    handleDelete,
    handlePress,
    selectedReport,
    isReportModalVisible,
    closeReportModal,
  };
}