All files / app/(tabs)/collect/components PostCollectItem.tsx

80% Statements 20/25
57.69% Branches 15/26
75% Functions 3/4
100% Lines 19/19

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                                      3x 3x 3x 3x   3x 3x 2x 2x 2x 2x       2x             2x               3x 3x 3x     3x   3x                                                                                                                                                                                                                                                         1x 1x                    
/**
 * 帖子收藏列表项 - 展示收藏的帖子信息
 */
import { Image } from 'react-native';
import { Card, Text, XStack, YStack } from 'tamagui';
import { Button } from '@/src/design-system/components';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
import type { Post } from '@/src/lib/supabase';
import { useThemeColors, useIsDarkMode } from '@/src/hooks/useThemeColors';
import { useResponsive } from '@/src/hooks/useResponsive';
 
interface PostCollectItemProps {
  post: Post;
  onDelete?: () => void;
  onPress?: () => void;
}
 
// 格式化日期为相对时间
function formatDate(dateString: string) {
  const date = new Date(dateString);
  const now = new Date();
  const diffTime = Math.abs(now.getTime() - date.getTime());
  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
 
  Iif (diffDays === 0) return '今天';
  if (diffDays === 1) return '昨天';
  Iif (diffDays < 7) return `${diffDays} 天前`;
  Iif (diffDays < 30) return `${Math.floor(diffDays / 7)} 周前`;
  Iif (diffDays < 365) return `${Math.floor(diffDays / 30)} 月前`;
  return date.toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' });
}
 
// 分类标签颜色
const CATEGORY_COLORS: Record<string, string> = {
  help: '#FF6B6B',
  share: '#4ECDC4',
  science: '#45B7D1',
  warning: '#FFA726',
};
 
const CATEGORY_LABELS: Record<string, string> = {
  help: '求助',
  share: '分享',
  science: '科普',
  warning: '避雷',
};
 
export default function PostCollectItem({ post, onDelete, onPress }: PostCollectItemProps) {
  const colors = useThemeColors();
  const isDark = useIsDarkMode();
  const { sw, sf, spacing, fontSize: fs, iconSize } = useResponsive();
 
  // 获取第一张图片作为封面
  const coverImage = post.media?.find((m) => m.mediaType === 'image')?.fileUrl;
 
  return (
    <Card
      size="$4"
      bordered
      borderColor={colors.borderMuted as any}
      backgroundColor={colors.cardBackground as any}
      pressStyle={{ scale: 0.98, opacity: 0.95 }}
      animation="quick"
      onPress={onPress}
    >
      <Card.Header padding={spacing.md as any}>
        <XStack gap={spacing.md as any} alignItems="flex-start">
          {/* 帖子封面图 */}
          <YStack
            borderRadius="$3"
            overflow="hidden"
            borderWidth={1}
            borderColor={colors.borderMuted as any}
          >
            {coverImage ? (
              <Image
                source={{ uri: coverImage }}
                style={{ width: sw(80), height: sw(80), borderRadius: 8 }}
                resizeMode="cover"
              />
            ) : (
              <YStack
                width={sw(80)}
                height={sw(80)}
                backgroundColor={colors.backgroundMuted as any}
                alignItems="center"
                justifyContent="center"
              >
                <IconSymbol name="doc.text" size={iconSize.xl} color={colors.textTertiary} />
              </YStack>
            )}
          </YStack>
 
          {/* 帖子信息 */}
          <YStack flex={1} gap="$2">
            {/* 分类标签 */}
            {post.category && (
              <XStack>
                <YStack
                  backgroundColor={(CATEGORY_COLORS[post.category] + '20') as any}
                  paddingHorizontal="$2"
                  paddingVertical="$1"
                  borderRadius="$2"
                >
                  <Text
                    fontSize={fs.xs}
                    fontWeight="600"
                    color={CATEGORY_COLORS[post.category] as any}
                  >
                    {CATEGORY_LABELS[post.category] || post.category}
                  </Text>
                </YStack>
              </XStack>
            )}
 
            {/* 帖子内容预览 */}
            <Text
              fontSize={fs.md}
              fontWeight="500"
              color={colors.text as any}
              numberOfLines={2}
              lineHeight={22}
            >
              {post.content || '无内容'}
            </Text>
 
            {/* 作者和时间 */}
            <XStack alignItems="center" gap="$2">
              {post.author?.avatar ? (
                <Image
                  source={{ uri: post.author.avatar }}
                  style={{ width: 18, height: 18, borderRadius: 9 }}
                />
              ) : (
                <YStack
                  width={18}
                  height={18}
                  borderRadius={9}
                  backgroundColor={colors.borderMuted as any}
                  alignItems="center"
                  justifyContent="center"
                >
                  <IconSymbol name="person.fill" size={10} color={colors.textTertiary} />
                </YStack>
              )}
              <Text fontSize={fs.xs} color={colors.textSecondary as any}>
                {post.author?.username || '匿名用户'}
              </Text>
              <Text fontSize={fs.xs} color={colors.textTertiary as any}>
                ·
              </Text>
              <Text fontSize={fs.xs} color={colors.textTertiary as any}>
                {formatDate(post.createdAt)}
              </Text>
            </XStack>
 
            {/* 互动数据 */}
            <XStack alignItems="center" gap="$4">
              <XStack alignItems="center" gap="$1">
                <IconSymbol name="heart.fill" size={14} color={colors.primary} />
                <Text fontSize={fs.xs} color={colors.textTertiary as any}>
                  {post.favoritesCount || 0}
                </Text>
              </XStack>
              <XStack alignItems="center" gap="$1">
                <IconSymbol name="bubble.left" size={14} color={colors.textTertiary} />
                <Text fontSize={fs.xs} color={colors.textTertiary as any}>
                  {post.commentsCount || 0}
                </Text>
              </XStack>
            </XStack>
          </YStack>
 
          {/* 删除按钮 */}
          <Button
            size="$2"
            circular
            backgroundColor="transparent"
            pressStyle={{ backgroundColor: colors.errorMuted as any }}
            onPress={(e) => {
              e.stopPropagation();
              onDelete?.();
            }}
          >
            <IconSymbol name="trash" size={18} color={colors.error} />
          </Button>
        </XStack>
      </Card.Header>
    </Card>
  );
}