All files / app/detail/components ActionBar.tsx

61.29% Statements 19/31
52.38% Branches 22/42
83.33% Functions 5/6
62.96% Lines 17/27

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                                    9x 9x       9x 9x   9x 9x     9x 2x             9x                             9x 2x 2x 2x 2x 2x         2x       9x                                                                                                                                                                                                                    
/**
 * 底部操作栏 - 包含收藏和点赞功能
 */
import { useCallback, useMemo, useState } from 'react';
import { Animated } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { Separator, Text, XStack, YStack } from 'tamagui';
import { Button } from '@/src/design-system/components';
import { useActionStatus } from '@/src/app/(tabs)/collect/hooks/useActionStatus';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
import { warningScale, errorScale, neutralScale } from '@/src/design-system/tokens';
import { toast } from '@/src/components/dialogs';
 
interface ActionBarProps {
  catfoodId: number;
}
 
export function ActionBar({ catfoodId }: ActionBarProps) {
  const insets = useSafeAreaInsets();
  const { liked, likeCount, favorited, toggleLike, toggleFavorite } = useActionStatus(
    catfoodId.toString()
  );
 
  const [favoriteLoading, setFavoriteLoading] = useState(false);
  const [likeLoading, setLikeLoading] = useState(false);
 
  const favoriteScale = useMemo(() => new Animated.Value(1), []);
  const likeScale = useMemo(() => new Animated.Value(1), []);
 
  // 按钮动画
  const animateButton = useCallback((scale: Animated.Value) => {
    Animated.sequence([
      Animated.timing(scale, { toValue: 1.2, duration: 150, useNativeDriver: true }),
      Animated.timing(scale, { toValue: 1, duration: 150, useNativeDriver: true }),
    ]).start();
  }, []);
 
  // 处理收藏
  const handleFavorite = useCallback(async () => {
    if (favoriteLoading) return;
    try {
      setFavoriteLoading(true);
      animateButton(favoriteScale);
      await toggleFavorite();
    } catch (error) {
      console.error('收藏操作失败:', error);
      toast.error('收藏操作失败,请稍后重试');
    } finally {
      setFavoriteLoading(false);
    }
  }, [favoriteLoading, toggleFavorite, animateButton, favoriteScale]);
 
  // 处理点赞
  const handleLike = useCallback(async () => {
    Iif (likeLoading) return;
    try {
      setLikeLoading(true);
      animateButton(likeScale);
      await toggleLike();
    } catch (error) {
      console.error('点赞操作失败:', error);
      toast.error('点赞操作失败,请稍后重试');
    } finally {
      setLikeLoading(false);
    }
  }, [likeLoading, toggleLike, animateButton, likeScale]);
 
  return (
    <YStack
      position="absolute"
      bottom={0}
      left={0}
      right={0}
      backgroundColor="$background"
      borderTopWidth={1}
      borderColor="$borderColor"
      paddingBottom={Math.max(insets.bottom, 16)}
    >
      <Separator />
 
      <XStack
        paddingHorizontal="$5"
        paddingTop="$4"
        gap="$4"
        alignItems="center"
        justifyContent="space-evenly"
      >
        {/* 收藏按钮 */}
        <YStack flex={1}>
          <Button
            testID="favorite-button"
            height={52}
            backgroundColor={favorited ? warningScale.warning8 : 'transparent'}
            borderWidth={2}
            borderColor={favorited ? warningScale.warning8 : neutralScale.neutral6}
            borderRadius="$4"
            onPress={handleFavorite}
            disabled={favoriteLoading}
            pressStyle={{
              scale: 0.97,
              backgroundColor: favorited ? warningScale.warning9 : neutralScale.neutral2,
            }}
            icon={
              <Animated.View style={{ transform: [{ scale: favoriteScale }] }}>
                <IconSymbol
                  name={favorited ? 'star.fill' : 'star'}
                  size={24}
                  color={favorited ? 'white' : neutralScale.neutral10}
                />
              </Animated.View>
            }
          >
            <Text
              color={favorited ? 'white' : neutralScale.neutral10}
              fontSize="$5"
              fontWeight="700"
            >
              {favoriteLoading ? '处理中...' : favorited ? '已收藏' : '收藏'}
            </Text>
          </Button>
        </YStack>
 
        {/* 点赞按钮 */}
        <YStack flex={1}>
          <Button
            height={52}
            backgroundColor={liked ? errorScale.error8 : 'transparent'}
            borderWidth={2}
            borderColor={liked ? errorScale.error8 : neutralScale.neutral6}
            borderRadius="$4"
            onPress={handleLike}
            disabled={likeLoading}
            pressStyle={{
              scale: 0.97,
              backgroundColor: liked ? errorScale.error9 : neutralScale.neutral2,
            }}
            icon={
              <Animated.View style={{ transform: [{ scale: likeScale }] }}>
                <IconSymbol
                  name={liked ? 'heart.fill' : 'heart'}
                  size={24}
                  color={liked ? 'white' : neutralScale.neutral10}
                />
              </Animated.View>
            }
          >
            <XStack alignItems="center" gap="$2">
              <Text color={liked ? 'white' : neutralScale.neutral10} fontSize="$5" fontWeight="700">
                {likeLoading ? '处理中...' : liked ? '已点赞' : '点赞'}
              </Text>
              {likeCount > 0 && (
                <YStack
                  paddingHorizontal="$2"
                  paddingVertical="$1"
                  backgroundColor={liked ? 'rgba(255,255,255,0.2)' : neutralScale.neutral3}
                  borderRadius="$10"
                >
                  <Text
                    color={liked ? 'white' : neutralScale.neutral10}
                    fontSize="$3"
                    fontWeight="bold"
                  >
                    {likeCount}
                  </Text>
                </YStack>
              )}
            </XStack>
          </Button>
        </YStack>
      </XStack>
    </YStack>
  );
}