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

0% Statements 0/26
0% Branches 0/32
0% Functions 0/9
0% Lines 0/24

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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import { memo, useCallback, useState } from 'react';
import { Image, ScrollView, Alert } from 'react-native';
import { Card, Text, XStack, YStack } from 'tamagui';
import { Trash2 } from '@tamagui/lucide-icons';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
import { Colors } from '@/src/constants/theme';
import { useThemeAwareColorScheme } from '@/src/hooks/useThemeAwareColorScheme';
import type { Pet } from '@/src/schemas/pet.schema';
import { Button } from '@/src/design-system/components';
import { PetHealthRecords } from './PetHealthRecords';
import { PetWeightChart } from './PetWeightChart';
import { PetWeightRecords } from './PetWeightRecords';
 
/**
 * 宠物信息面板组件的 Props 接口
 */
interface PetInfoPanelProps {
  /** 宠物数据 */
  pet: Pet;
  /** 删除宠物回调 */
  onDelete?: (petId: number) => Promise<void>;
}
 
/**
 * Tab 配置常量
 * 使用 as const 确保类型安全
 */
const TABS = [
  { key: 'info', label: '基本信息', icon: 'info.circle.fill' },
  { key: 'health', label: '健康档案', icon: 'heart.fill' },
  { key: 'activity', label: '活动记录', icon: 'chart.bar.fill' },
] as const;
 
/** Tab 键类型 */
type TabKey = (typeof TABS)[number]['key'];
 
/**
 * 信息行组件 - 用于显示键值对信息
 */
interface InfoRowProps {
  label: string;
  value: string;
  colors: (typeof Colors)[keyof typeof Colors];
}
 
const InfoRow = memo(function InfoRow({ label, value, colors }: InfoRowProps) {
  return (
    <XStack justifyContent="space-between" paddingVertical="$2.5" paddingHorizontal="$1">
      <Text fontSize={14} color={colors.icon as any}>
        {label}
      </Text>
      <Text fontSize={14} fontWeight="600" color={colors.text as any}>
        {value}
      </Text>
    </XStack>
  );
});
 
/**
 * 宠物信息面板组件
 *
 * 功能:
 * - 展示宠物的头像和基本信息
 * - 提供多个 Tab 切换查看不同类型的信息
 * - 支持主题切换
 *
 * @component
 * @example
 * ```tsx
 * <PetInfoPanel pet={selectedPet} />
 * ```
 */
export const PetInfoPanel = memo(function PetInfoPanel({ pet, onDelete }: PetInfoPanelProps) {
  const colorScheme = useThemeAwareColorScheme();
  const colors = Colors[colorScheme];
  const [activeTab, setActiveTab] = useState<TabKey>('info');
  const [deleting, setDeleting] = useState(false);
  const [refreshTrigger, setRefreshTrigger] = useState(0);
 
  /**
   * 处理 Tab 切换
   * 使用 useCallback 避免不必要的重渲染
   */
  const handleTabChange = useCallback((tab: TabKey) => {
    setActiveTab(tab);
  }, []);
 
  /**
   * 刷新体重数据
   */
  const handleRefresh = useCallback(() => {
    setRefreshTrigger((prev) => prev + 1);
  }, []);
 
  /**
   * 处理删除宠物
   */
  const handleDelete = useCallback(() => {
    if (!onDelete) return;
 
    Alert.alert('确认删除', `确定要删除 ${pet.name} 吗?此操作无法撤销。`, [
      {
        text: '取消',
        style: 'cancel',
      },
      {
        text: '删除',
        style: 'destructive',
        onPress: async () => {
          try {
            setDeleting(true);
            await onDelete(pet.id);
          } catch (error) {
            // 错误已在 Hook 中处理
          } finally {
            setDeleting(false);
          }
        },
      },
    ]);
  }, [onDelete, pet.id, pet.name]);
 
  return (
    <YStack width="90%" marginTop="$4" gap="$3">
      {/* Pet Header Card */}
      <Card
        padding="$4"
        backgroundColor={colors.background as any}
        borderWidth={1}
        borderColor={colors.iconAlpha15 as any}
        borderRadius="$4"
      >
        <XStack gap="$4" alignItems="center">
          {/* Pet Photo */}
          {pet.photo_url ? (
            <YStack width={80} height={80} borderRadius="$4" overflow="hidden" borderWidth={0}>
              <Image
                source={{ uri: pet.photo_url }}
                style={{ width: '100%', height: '100%' }}
                resizeMode="cover"
              />
            </YStack>
          ) : (
            <YStack
              width={80}
              height={80}
              borderRadius="$4"
              backgroundColor="$orange3"
              alignItems="center"
              justifyContent="center"
              borderWidth={0}
            >
              <Text fontSize={48}>🐱</Text>
            </YStack>
          )}
 
          {/* Pet Basic Info */}
          <YStack flex={1} gap="$1.5">
            <Text fontSize={20} fontWeight="700" color={colors.text as any}>
              {pet.name}
            </Text>
            <XStack gap="$2" alignItems="center" flexWrap="wrap">
              <XStack
                paddingHorizontal="$2.5"
                paddingVertical="$1"
                backgroundColor="#FEF3E8"
                borderRadius="$2"
              >
                <Text fontSize={13} fontWeight="600" color="#D97706">
                  {pet.species_display ?? pet.species}
                </Text>
              </XStack>
              {pet.breed && (
                <Text fontSize={14} color={colors.icon as any}>
                  {pet.breed}
                </Text>
              )}
              {pet.age != null && (
                <>
                  <Text fontSize={14} color={colors.iconAlpha60 as any}>
                    ·
                  </Text>
                  <Text fontSize={14} color={colors.icon as any}>
                    {pet.age}岁
                  </Text>
                </>
              )}
            </XStack>
          </YStack>
        </XStack>
      </Card>
 
      {/* Divider */}
      <YStack width="100%" alignItems="center" paddingVertical="$2">
        <YStack width="90%" height={1} backgroundColor={colors.iconAlpha15 as any} />
      </YStack>
 
      {/* Tab Navigation */}
      <XStack gap="$2" paddingHorizontal="$1">
        {TABS.map((tab) => {
          const isActive = activeTab === tab.key;
          return (
            <YStack
              key={tab.key}
              flex={1}
              paddingVertical="$3"
              borderRadius="$3"
              backgroundColor={isActive ? '#FEBE98' : '$gray3'}
              alignItems="center"
              justifyContent="center"
              pressStyle={{ scale: 0.97, opacity: 0.8 }}
              onPress={() => handleTabChange(tab.key)}
              cursor="pointer"
              gap="$1"
              animation="quick"
            >
              <IconSymbol
                name={tab.icon as any}
                size={20}
                color={isActive ? 'white' : colors.icon}
              />
              <Text fontSize={13} fontWeight="600" color={isActive ? 'white' : colors.text}>
                {tab.label}
              </Text>
            </YStack>
          );
        })}
      </XStack>
 
      {/* Tab Content */}
      <Card
        padding="$4"
        backgroundColor={colors.background as any}
        borderWidth={1}
        borderColor={colors.iconAlpha20 as any}
        borderRadius="$4"
        minHeight={200}
      >
        {activeTab === 'info' && (
          <YStack gap="$4">
            <XStack alignItems="center" gap="$2">
              <IconSymbol name="info.circle.fill" size={20} color="#FEBE98" />
              <Text fontSize={16} fontWeight="700" color={colors.text as any}>
                基本信息
              </Text>
            </XStack>
 
            {pet.description && (
              <>
                <YStack gap="$2.5">
                  <Text fontSize={14} fontWeight="600" color={colors.icon as any}>
                    📝 描述
                  </Text>
                  <YStack
                    padding="$3"
                    backgroundColor="$gray2"
                    borderRadius="$3"
                    borderLeftWidth={3}
                    borderLeftColor={colors.iconAlpha30 as any}
                  >
                    <Text fontSize={14} color={colors.text as any} lineHeight={22}>
                      {pet.description}
                    </Text>
                  </YStack>
                </YStack>
                <YStack
                  height={1}
                  backgroundColor={colors.iconAlpha20 as any}
                  marginVertical="$2"
                />
              </>
            )}
 
            <YStack gap="$1">
              <InfoRow
                label="宠物类型"
                value={pet.species_display ?? pet.species}
                colors={colors}
              />
              <YStack height={1} backgroundColor={colors.iconAlpha15 as any} />
 
              {pet.breed && (
                <>
                  <InfoRow label="品种" value={pet.breed} colors={colors} />
                  <YStack height={1} backgroundColor={colors.iconAlpha15 as any} />
                </>
              )}
 
              {pet.age != null && <InfoRow label="年龄" value={`${pet.age}岁`} colors={colors} />}
            </YStack>
          </YStack>
        )}
 
        {activeTab === 'health' && (
          <ScrollView
            style={{ maxHeight: 400 }}
            showsVerticalScrollIndicator={false}
            nestedScrollEnabled
          >
            <PetHealthRecords petId={pet.id} petName={pet.name} />
          </ScrollView>
        )}
 
        {activeTab === 'activity' && (
          <ScrollView
            style={{ maxHeight: 400 }}
            showsVerticalScrollIndicator={false}
            nestedScrollEnabled
          >
            <YStack gap="$4">
              <PetWeightChart petId={pet.id} petName={pet.name} refreshTrigger={refreshTrigger} />
              <PetWeightRecords petId={pet.id} petName={pet.name} onRefresh={handleRefresh} />
            </YStack>
          </ScrollView>
        )}
      </Card>
 
      {/* 删除按钮 - 放在最下方 */}
      {onDelete && (
        <>
          <YStack height={1} backgroundColor={colors.iconAlpha15 as any} marginVertical="$3" />
          <Button
            fullWidth
            variant="danger"
            leftIcon={<Trash2 size={18} />}
            onPress={handleDelete}
            loading={deleting}
            disabled={deleting}
          >
            删除宠物
          </Button>
        </>
      )}
    </YStack>
  );
});