All files / app/(tabs)/profile/components BadgeGrid.tsx

0% Statements 0/15
0% Branches 0/20
0% Functions 0/4
0% Lines 0/14

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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207                                                                                                                                                                                                                                                                                                                                                                                                                             
/**
 * 勋章网格展示组件
 *
 * 显示用户已获得的勋章
 */
import { memo } from 'react';
import { TouchableOpacity, StyleSheet } from 'react-native';
import { LinearGradient } from 'expo-linear-gradient';
import { Text, XStack, YStack } from 'tamagui';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
import { BADGE_CONFIGS, RARITY_CONFIGS } from '@/src/constants/badges';
import { useThemeColors } from '@/src/hooks/useThemeColors';
import type { DbUserBadge } from '@/src/lib/supabase/types/database';
 
interface BadgeGridProps {
  badges: DbUserBadge[];
  onBadgePress?: (badge: DbUserBadge) => void;
  maxDisplay?: number;
}
 
export const BadgeGrid = memo(function BadgeGrid({
  badges,
  onBadgePress,
  maxDisplay,
}: BadgeGridProps) {
  const colors = useThemeColors();
  const displayBadges = maxDisplay ? badges.slice(0, maxDisplay) : badges;
  const remainingCount = maxDisplay && badges.length > maxDisplay ? badges.length - maxDisplay : 0;
 
  if (badges.length === 0) {
    return (
      <YStack
        padding="$6"
        alignItems="center"
        justifyContent="center"
        backgroundColor={colors.backgroundMuted as any}
        borderRadius={16}
        borderWidth={2}
        borderStyle="dashed"
        borderColor={colors.border as any}
      >
        <IconSymbol name="trophy" size={48} color={colors.textTertiary} />
        <Text fontSize={15} color={colors.textSecondary as any} marginTop="$3" textAlign="center">
          还没有获得任何勋章
        </Text>
        <Text fontSize={13} color={colors.textTertiary as any} marginTop="$1" textAlign="center">
          完善资料、发表评论即可获得
        </Text>
      </YStack>
    );
  }
 
  return (
    <YStack gap="$3">
      <XStack flexWrap="wrap" gap="$3">
        {displayBadges.map((userBadge) => {
          const badgeConfig = userBadge.badge?.code ? BADGE_CONFIGS[userBadge.badge.code] : null;
 
          if (!badgeConfig) return null;
 
          const rarityConfig = RARITY_CONFIGS[badgeConfig.rarity];
 
          return (
            <TouchableOpacity
              key={userBadge.id}
              activeOpacity={0.7}
              onPress={() => onBadgePress?.(userBadge)}
              style={styles.badgeContainer}
            >
              <YStack alignItems="center" gap="$2">
                {/* 勋章图标 */}
                <YStack
                  position="relative"
                  width={72}
                  height={72}
                  alignItems="center"
                  justifyContent="center"
                >
                  {/* 光晕效果 */}
                  <YStack
                    position="absolute"
                    width={72}
                    height={72}
                    borderRadius={36}
                    style={{
                      backgroundColor: rarityConfig.glow,
                    }}
                  />
 
                  {/* 勋章主体 */}
                  <YStack
                    width={64}
                    height={64}
                    borderRadius={32}
                    alignItems="center"
                    justifyContent="center"
                    borderWidth={3}
                    borderColor={colors.cardBackground as any}
                    style={{
                      backgroundColor: badgeConfig.gradient
                        ? 'transparent'
                        : badgeConfig.color + '20',
                    }}
                  >
                    {badgeConfig.gradient ? (
                      <LinearGradient
                        colors={badgeConfig.gradient}
                        style={{
                          width: 58,
                          height: 58,
                          borderRadius: 29,
                          alignItems: 'center',
                          justifyContent: 'center',
                        }}
                      >
                        <IconSymbol name={badgeConfig.icon as any} size={32} color="white" />
                      </LinearGradient>
                    ) : (
                      <IconSymbol
                        name={badgeConfig.icon as any}
                        size={32}
                        color={badgeConfig.color}
                      />
                    )}
                  </YStack>
 
                  {/* 已装备标记 */}
                  {userBadge.is_equipped && (
                    <YStack
                      position="absolute"
                      top={-2}
                      right={-2}
                      width={20}
                      height={20}
                      borderRadius={10}
                      backgroundColor={badgeConfig.color as any}
                      alignItems="center"
                      justifyContent="center"
                      borderWidth={2}
                      borderColor={colors.cardBackground as any}
                    >
                      <IconSymbol name="checkmark" size={10} color="white" />
                    </YStack>
                  )}
                </YStack>
 
                {/* 勋章名称 */}
                <Text
                  fontSize={12}
                  fontWeight="600"
                  color={colors.text as any}
                  textAlign="center"
                  numberOfLines={1}
                  style={{ maxWidth: 72 }}
                >
                  {badgeConfig.name}
                </Text>
              </YStack>
            </TouchableOpacity>
          );
        })}
 
        {/* 更多徽章提示 */}
        {remainingCount > 0 && (
          <TouchableOpacity
            activeOpacity={0.7}
            onPress={() => onBadgePress?.(badges[0])}
            style={styles.badgeContainer}
          >
            <YStack alignItems="center" gap="$2">
              <YStack
                width={64}
                height={64}
                borderRadius={32}
                alignItems="center"
                justifyContent="center"
                backgroundColor={colors.backgroundMuted as any}
                borderWidth={2}
                borderStyle="dashed"
                borderColor={colors.border as any}
              >
                <Text fontSize={20} fontWeight="700" color={colors.textSecondary as any}>
                  +{remainingCount}
                </Text>
              </YStack>
              <Text
                fontSize={12}
                fontWeight="600"
                color={colors.textSecondary as any}
                textAlign="center"
              >
                更多
              </Text>
            </YStack>
          </TouchableOpacity>
        )}
      </XStack>
    </YStack>
  );
});
 
const styles = StyleSheet.create({
  badgeContainer: {
    width: 72,
  },
});