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

0% Statements 0/12
0% Branches 0/16
0% Functions 0/4
0% Lines 0/12

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                                                                                                                                                                                                                                                                               
import { memo, useCallback, useState } from 'react';
import { Dimensions } from 'react-native';
import { Text, XStack, YStack } from 'tamagui';
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 { CommentsTab } from './tabs/CommentsTab';
import { LikesTab } from './tabs/LikesTab';
import { PetsTab } from './tabs/PetsTab';
 
const { width: _SCREEN_WIDTH } = Dimensions.get('window');
 
/**
 * Tab 配置
 */
const TABS = [
  { key: 'pets', label: '宠物', icon: 'pawprint.fill' },
  { key: 'comments', label: '评论', icon: 'bubble.left.and.bubble.right.fill' },
  { key: 'likes', label: '点赞', icon: 'heart.fill' },
] as const;
 
type TabKey = (typeof TABS)[number]['key'];
 
/**
 * Profile Tabs 组件的 Props
 */
interface ProfileTabsProps {
  /** 宠物列表 */
  pets?: Pet[];
  /** 是否正在加载 */
  isLoading?: boolean;
  /** 添加宠物回调 */
  onAddPet: () => void;
  /** 删除宠物回调 */
  onDeletePet?: (petId: number) => Promise<void>;
}
 
/**
 * Profile Tabs 组件
 *
 * 功能:
 * - 三个主要 Tab:宠物、评论、点赞
 * - 每个 Tab 有独立的内容区域
 * - 支持切换动画和状态管理
 *
 * @component
 * @example
 * ```tsx
 * <ProfileTabs
 *   pets={user?.pets}
 *   isLoading={isLoading}
 *   onAddPet={handleAddPet}
 * />
 * ```
 */
export const ProfileTabs = memo(function ProfileTabs({
  pets = [],
  isLoading = false,
  onAddPet,
  onDeletePet,
}: ProfileTabsProps) {
  const colorScheme = useThemeAwareColorScheme();
  const colors = Colors[colorScheme];
  const [activeTab, setActiveTab] = useState<TabKey>('pets');
 
  /**
   * 切换 Tab
   */
  const handleTabChange = useCallback((tab: TabKey) => {
    setActiveTab(tab);
  }, []);
 
  return (
    <YStack width="100%" flex={1}>
      {/* Tab 导航栏 */}
      <XStack
        width="100%"
        paddingHorizontal="$4"
        paddingTop="$3"
        paddingBottom="$2"
        backgroundColor={colors.background}
        borderBottomWidth={1}
        borderBottomColor={colors.iconAlpha10 as any}
      >
        {TABS.map((tab) => {
          const isActive = activeTab === tab.key;
          return (
            <YStack
              key={tab.key}
              flex={1}
              alignItems="center"
              paddingVertical="$3"
              borderBottomWidth={3}
              borderBottomColor={isActive ? '#FEBE98' : 'transparent'}
              pressStyle={{ opacity: 0.7 }}
              onPress={() => handleTabChange(tab.key)}
              cursor="pointer"
            >
              <XStack gap="$2" alignItems="center">
                <IconSymbol
                  name={tab.icon as any}
                  size={20}
                  color={isActive ? '#FEBE98' : colors.icon}
                />
                <Text
                  fontSize={15}
                  fontWeight={isActive ? '700' : '500'}
                  color={isActive ? '#FEBE98' : colors.text}
                >
                  {tab.label}
                </Text>
              </XStack>
            </YStack>
          );
        })}
      </XStack>
 
      {/* Tab 内容区域 */}
      <YStack flex={1} width="100%">
        {activeTab === 'pets' && (
          <PetsTab
            pets={pets}
            isLoading={isLoading}
            onAddPet={onAddPet}
            onDeletePet={onDeletePet}
          />
        )}
        {activeTab === 'comments' && <CommentsTab />}
        {activeTab === 'likes' && <LikesTab />}
      </YStack>
    </YStack>
  );
});