All files / components CatFoodCard.tsx

50% Statements 9/18
37.09% Branches 23/62
50% Functions 3/6
52.94% Lines 9/17

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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313                                                                2x 6x 6x                                                                                             6x 6x           6x 6x   6x     6x                                                                                                                                                                                                                                                                                                                                                                                                                                                    
/**
 * CatFoodCard - 猫粮卡片组件
 *
 * 采用现代购物App风格设计:
 * - 清晰的视觉层次
 * - 精致的微交互
 * - 优雅的排名展示
 */
 
import { Image, Pressable, StyleSheet } from 'react-native';
import { LinearGradient } from 'expo-linear-gradient';
import { Text, XStack, YStack } from 'tamagui';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
import type { CatFood } from '@/src/types/catFood';
import {
  primaryScale,
  neutralScale,
  warningScale,
  errorScale,
  successScale,
} from '@/src/design-system/tokens';
 
interface CatFoodCardProps {
  catfood: CatFood;
  index?: number;
  onPress?: (catfood: CatFood) => void;
  onImagePress?: (imageUrl: string) => void;
  showRank?: boolean;
  showNutritionInfo?: boolean;
}
 
// 排名徽章配置
const getRankConfig = (rank: number) => {
  Eif (rank === 0) {
    return {
      gradient: ['#FFD700', '#FFA500'] as const,
      textColor: '#FFFFFF',
      icon: 'crown.fill' as const,
      bgColor: '#FFF9E6',
      borderColor: '#FFD700',
      label: '冠军',
    };
  }
  if (rank === 1) {
    return {
      gradient: ['#E8E8E8', '#B0B0B0'] as const,
      textColor: '#FFFFFF',
      icon: 'medal.fill' as const,
      bgColor: '#F8F8F8',
      borderColor: '#C0C0C0',
      label: '亚军',
    };
  }
  if (rank === 2) {
    return {
      gradient: ['#DEB887', '#CD853F'] as const,
      textColor: '#FFFFFF',
      icon: 'medal.fill' as const,
      bgColor: '#FDF5EE',
      borderColor: '#CD853F',
      label: '季军',
    };
  }
  return {
    gradient: [neutralScale.neutral4, neutralScale.neutral5] as const,
    textColor: neutralScale.neutral11,
    icon: 'number' as const,
    bgColor: '#FFFFFF',
    borderColor: neutralScale.neutral4,
    label: '',
  };
};
 
export function CatFoodCard({
  catfood,
  index = 0,
  onPress,
  onImagePress,
  showRank = true,
  showNutritionInfo = true,
}: CatFoodCardProps) {
  const handlePress = () => onPress?.(catfood);
  const handleImagePress = () => {
    if (catfood.imageUrl && onImagePress) {
      onImagePress(catfood.imageUrl);
    }
  };
 
  const rankConfig = getRankConfig(index);
  const isTopThree = index < 3;
 
  return (
    <Pressable onPress={handlePress}>
      {({ pressed }) => (
        <YStack
          backgroundColor="$background"
          marginHorizontal="$3"
          marginBottom="$3"
          borderRadius={16}
          borderWidth={isTopThree ? 1.5 : 1}
          borderColor={(isTopThree ? rankConfig.borderColor : neutralScale.neutral3) as any}
          overflow="hidden"
          opacity={pressed ? 0.96 : 1}
          scale={pressed ? 0.985 : 1}
          animation="quick"
        >
          {/* 主内容区 */}
          <XStack padding="$3.5" gap="$3">
            {/* 左侧:排名徽章 */}
            {showRank && (
              <YStack alignItems="center" gap="$1.5" width={50}>
                <YStack
                  width={46}
                  height={46}
                  borderRadius={23}
                  overflow="hidden"
                  alignItems="center"
                  justifyContent="center"
                >
                  <LinearGradient
                    colors={rankConfig.gradient}
                    start={{ x: 0, y: 0 }}
                    end={{ x: 1, y: 1 }}
                    style={StyleSheet.absoluteFill}
                  />
                  {isTopThree ? (
                    <IconSymbol name={rankConfig.icon} size={22} color={rankConfig.textColor} />
                  ) : (
                    <Text fontSize="$5" fontWeight="900" color={rankConfig.textColor as any}>
                      {index + 1}
                    </Text>
                  )}
                </YStack>
                {isTopThree && (
                  <Text fontSize={10} fontWeight="800" color={rankConfig.borderColor as any}>
                    {rankConfig.label}
                  </Text>
                )}
              </YStack>
            )}
 
            {/* 中间:产品图片 */}
            <Pressable onPress={handleImagePress}>
              <YStack
                width={90}
                height={90}
                borderRadius={12}
                overflow="hidden"
                backgroundColor={neutralScale.neutral2}
                borderWidth={1}
                borderColor={neutralScale.neutral3}
              >
                {catfood.imageUrl ? (
                  <Image
                    source={{ uri: catfood.imageUrl }}
                    style={{ width: '100%', height: '100%' }}
                    resizeMode="cover"
                  />
                ) : (
                  <YStack flex={1} alignItems="center" justifyContent="center">
                    <IconSymbol name="photo" size={32} color={neutralScale.neutral5} />
                  </YStack>
                )}
              </YStack>
            </Pressable>
 
            {/* 右侧:产品信息 */}
            <YStack flex={1} gap="$2" justifyContent="space-between">
              {/* 名称 */}
              <Text fontSize="$4" fontWeight="700" color="$color" numberOfLines={2} lineHeight={22}>
                {catfood.name}
              </Text>
 
              {/* 品牌标签 */}
              <XStack alignItems="center">
                <YStack
                  backgroundColor={primaryScale.primary2}
                  paddingHorizontal="$2"
                  paddingVertical={4}
                  borderRadius={6}
                  borderWidth={1}
                  borderColor={primaryScale.primary4}
                >
                  <Text fontSize={11} fontWeight="600" color={primaryScale.primary10}>
                    {catfood.brand || '未知品牌'}
                  </Text>
                </YStack>
              </XStack>
 
              {/* 评分和点赞 */}
              <XStack alignItems="center" gap="$3">
                {/* 评分 */}
                <XStack
                  alignItems="center"
                  gap="$1"
                  backgroundColor={warningScale.warning1}
                  paddingHorizontal="$2"
                  paddingVertical={4}
                  borderRadius={6}
                >
                  <IconSymbol name="star.fill" size={13} color={warningScale.warning6} />
                  <Text fontSize={13} fontWeight="800" color={warningScale.warning8}>
                    {catfood.score?.toFixed(1) || '0.0'}
                  </Text>
                  <Text fontSize={10} color={neutralScale.neutral8}>
                    ({catfood.countNum || 0})
                  </Text>
                </XStack>
 
                {/* 点赞 */}
                <XStack alignItems="center" gap="$1">
                  <IconSymbol name="heart.fill" size={13} color={errorScale.error5} />
                  <Text fontSize={13} fontWeight="700" color={errorScale.error7}>
                    {catfood.like_count || 0}
                  </Text>
                </XStack>
              </XStack>
            </YStack>
          </XStack>
 
          {/* 底部信息区:标签 + 营养状态 */}
          {((catfood.tags && catfood.tags.length > 0) ||
            (showNutritionInfo && (catfood.ingredient?.length > 0 || catfood.percentage))) && (
            <YStack
              paddingHorizontal="$3.5"
              paddingBottom="$3"
              paddingTop="$1"
              gap="$2"
              borderTopWidth={1}
              borderTopColor={neutralScale.neutral2}
            >
              {/* 标签 */}
              {catfood.tags && catfood.tags.length > 0 && (
                <XStack gap="$1.5" flexWrap="wrap">
                  {catfood.tags.slice(0, 4).map((tag, idx) => (
                    <YStack
                      key={idx}
                      paddingHorizontal="$2"
                      paddingVertical={3}
                      backgroundColor={
                        idx === 0
                          ? `${primaryScale.primary3}80`
                          : idx === 1
                            ? `${warningScale.warning2}80`
                            : `${neutralScale.neutral3}80`
                      }
                      borderRadius={10}
                    >
                      <Text
                        fontSize={10}
                        fontWeight="600"
                        color={
                          idx === 0
                            ? primaryScale.primary10
                            : idx === 1
                              ? warningScale.warning10
                              : neutralScale.neutral10
                        }
                      >
                        #{tag}
                      </Text>
                    </YStack>
                  ))}
                </XStack>
              )}
 
              {/* 营养信息标签 */}
              {showNutritionInfo && (catfood.ingredient?.length > 0 || catfood.percentage) && (
                <XStack gap="$2">
                  {catfood.ingredient?.length > 0 && (
                    <XStack
                      alignItems="center"
                      gap="$1"
                      backgroundColor={successScale.success1}
                      paddingHorizontal="$2"
                      paddingVertical={4}
                      borderRadius={6}
                    >
                      <IconSymbol
                        name="checkmark.seal.fill"
                        size={12}
                        color={successScale.success7}
                      />
                      <Text fontSize={10} fontWeight="600" color={successScale.success8}>
                        已录入营养成分
                      </Text>
                    </XStack>
                  )}
                  {catfood.percentage && (
                    <XStack
                      alignItems="center"
                      gap="$1"
                      backgroundColor="#EFF6FF"
                      paddingHorizontal="$2"
                      paddingVertical={4}
                      borderRadius={6}
                    >
                      <IconSymbol name="chart.line.uptrend.xyaxis" size={12} color="#2563EB" />
                      <Text fontSize={10} fontWeight="600" color="#1D4ED8">
                        可查看分析
                      </Text>
                    </XStack>
                  )}
                </XStack>
              )}
            </YStack>
          )}
        </YStack>
      )}
    </Pressable>
  );
}