All files / app/(tabs)/profile/hooks useReputation.ts

62.71% Statements 37/59
50% Branches 10/20
100% Functions 8/8
61.81% Lines 34/55

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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                          18x 18x 18x 18x     18x 8x         8x 8x 8x   8x   7x 1x     6x   2x 2x   8x         18x 8x   8x 8x                     8x       8x 8x             18x   1x                                                               18x   1x                                               18x 1x     18x 10x 7x 7x       18x                    
/**
 * 信誉分和勋章管理 Hook
 *
 * 处理信誉分查询、勋章管理等功能
 */
import { useState, useEffect, useCallback } from 'react';
import { Alert } from 'react-native';
import { getUserReputation, type ReputationSummary } from '@/src/lib/supabase/services/reputation';
import { supabase } from '@/src/lib/supabase/client';
import { convertKeysToCamel } from '@/src/lib/supabase/helpers';
import type { DbUserBadge } from '@/src/lib/supabase/types/database';
 
export function useReputation(userId?: string) {
  const [reputation, setReputation] = useState<ReputationSummary | null>(null);
  const [badges, setBadges] = useState<DbUserBadge[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
 
  // 加载信誉分数据
  const loadReputation = useCallback(async () => {
    Iif (!userId) {
      setLoading(false);
      return;
    }
 
    try {
      setLoading(true);
      setError(null);
 
      const { data, error: reputationError } = await getUserReputation(userId);
 
      if (reputationError) {
        throw new Error(reputationError.message);
      }
 
      setReputation(data);
    } catch (err) {
      console.error('加载信誉分失败:', err);
      setError(err instanceof Error ? err.message : '加载失败');
    } finally {
      setLoading(false);
    }
  }, [userId]);
 
  // 加载勋章数据
  const loadBadges = useCallback(async () => {
    Iif (!userId) return;
 
    try {
      const { data, error: badgeError } = await supabase
        .from('user_badges')
        .select(
          `
          *,
          badge:badges(*)
        `
        )
        .eq('user_id', userId)
        .order('acquired_at', { ascending: false });
 
      Iif (badgeError) {
        throw new Error(badgeError.message);
      }
 
      const convertedBadges = data.map((item) => convertKeysToCamel(item)) as DbUserBadge[];
      setBadges(convertedBadges);
    } catch (err) {
      console.error('加载勋章失败:', err);
    }
  }, [userId]);
 
  // 装备勋章
  const equipBadge = useCallback(
    async (badgeId: number) => {
      Eif (!userId) return;
 
      try {
        // 先取消所有已装备的勋章
        await supabase
          .from('user_badges')
          .update({ is_equipped: false })
          .eq('user_id', userId)
          .eq('is_equipped', true);
 
        // 装备新勋章
        const { error } = await supabase
          .from('user_badges')
          .update({ is_equipped: true })
          .eq('user_id', userId)
          .eq('badge_id', badgeId);
 
        if (error) {
          throw new Error(error.message);
        }
 
        Alert.alert('成功', '勋章已装备');
        await loadBadges();
      } catch (err) {
        console.error('装备勋章失败:', err);
        Alert.alert('失败', '装备勋章失败,请稍后再试');
      }
    },
    [userId, loadBadges]
  );
 
  // 取消装备勋章
  const unequipBadge = useCallback(
    async (badgeId: number) => {
      Eif (!userId) return;
 
      try {
        const { error } = await supabase
          .from('user_badges')
          .update({ is_equipped: false })
          .eq('user_id', userId)
          .eq('badge_id', badgeId);
 
        if (error) {
          throw new Error(error.message);
        }
 
        Alert.alert('成功', '已取消装备');
        await loadBadges();
      } catch (err) {
        console.error('取消装备失败:', err);
        Alert.alert('失败', '取消装备失败,请稍后再试');
      }
    },
    [userId, loadBadges]
  );
 
  // 刷新数据
  const refresh = useCallback(async () => {
    await Promise.all([loadReputation(), loadBadges()]);
  }, [loadReputation, loadBadges]);
 
  useEffect(() => {
    if (userId) {
      loadReputation();
      loadBadges();
    }
  }, [userId, loadReputation, loadBadges]);
 
  return {
    reputation,
    badges,
    loading,
    error,
    refresh,
    equipBadge,
    unequipBadge,
  };
}