All files / app/detail/components AIReportModal.tsx

83.33% Statements 20/24
71.42% Branches 15/21
100% Functions 11/11
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 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                                              3x 3x   3x   2x                                                                                                                                                                                     2x                                                                                                   2x   2x             4x                                                               4x   4x                                         2x 4x     2x   2x           4x                                                   4x   4x                 4x                                         2x                          
/**
 * AI 报告详情模态框
 */
 
import { Modal, ScrollView } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { Feather } from '@expo/vector-icons';
import { H3, H5, Separator, Spinner, Text, XStack, YStack } from 'tamagui';
import { Button } from '@/src/design-system/components';
 
import { getNutritionColor, getNutritionLabel } from '@/src/constants/nutrition';
import type { AIReportData } from '@/src/services/api';
import { useFavorite } from '@/src/hooks';
 
import { NutrientBar } from './NutrientBar';
 
interface AIReportModalProps {
  visible: boolean;
  report: AIReportData | null;
  onClose: () => void;
}
 
export function AIReportModal({ visible, report, onClose }: AIReportModalProps) {
  const insets = useSafeAreaInsets();
  const { isFavorited, isToggling, toggle } = useFavorite({ catfoodId: report?.catfood_id });
 
  if (!report) return null;
 
  return (
    <Modal
      visible={visible}
      animationType="slide"
      presentationStyle="pageSheet"
      onRequestClose={onClose}
    >
      <YStack flex={1} backgroundColor="$background">
        {/* 头部 */}
        <ModalHeader
          catfoodId={report.catfood_id}
          isFavorited={isFavorited}
          isToggling={isToggling}
          onToggleFavorite={toggle}
          onClose={onClose}
          topInset={insets.top}
        />
 
        {/* 内容 */}
        <ScrollView
          showsVerticalScrollIndicator={false}
          contentContainerStyle={{ padding: 16, paddingBottom: Math.max(insets.bottom + 16, 32) }}
        >
          <TagsSection tags={report.tags} />
          <Separator marginVertical="$3" />
 
          <AnalysisSection
            icon="🛡️"
            title="安全性分析"
            content={report.safety}
            bgColor="$green2"
            borderColor="$green6"
          />
          <Separator marginVertical="$3" />
 
          <AnalysisSection
            icon="🍖"
            title="营养分析"
            content={report.nutrient}
            bgColor="$orange2"
            borderColor="$orange6"
          />
 
          {report.percentage && report.percent_data && (
            <>
              <Separator marginVertical="$3" />
              <NutritionSection percentData={report.percent_data} />
            </>
          )}
 
          <ItemsSection
            icon="⚗️"
            title="识别到的添加剂"
            items={report.additives}
            bgColor="$purple2"
            borderColor="$purple6"
            textColor="$purple11"
          />
          <ItemsSection
            icon="🧪"
            title="识别到的营养成分"
            items={report.ingredients}
            bgColor="$green2"
            borderColor="$green6"
            textColor="$green11"
          />
 
          <TimestampSection createdAt={report.created_at} updatedAt={report.updated_at} />
        </ScrollView>
      </YStack>
    </Modal>
  );
}
 
// ==================== 子组件 ====================
 
function ModalHeader({
  catfoodId,
  isFavorited,
  isToggling,
  onToggleFavorite,
  onClose,
  topInset,
}: {
  catfoodId: number;
  isFavorited: boolean;
  isToggling: boolean;
  onToggleFavorite: () => void;
  onClose: () => void;
  topInset: number;
}) {
  return (
    <XStack
      paddingHorizontal="$4"
      paddingTop={Math.max(topInset, 16)}
      paddingBottom="$3"
      backgroundColor="$blue5"
      borderBottomWidth={1}
      borderBottomColor="$borderColor"
      alignItems="center"
      justifyContent="space-between"
    >
      <YStack flex={1}>
        <H3 color="$blue11" fontWeight="700">
          AI 分析报告
        </H3>
        <Text fontSize="$2" color="$gray11" marginTop="$1">
          猫粮 ID: {catfoodId}
        </Text>
      </YStack>
 
      <XStack gap="$2" alignItems="center">
        <Button
          size="$3"
          circular
          chromeless
          disabled={isToggling}
          pressStyle={{ opacity: 0.7 }}
          onPress={onToggleFavorite}
          icon={
            isToggling ? (
              <Spinner size="small" />
            ) : (
              <Feather name="heart" size={20} color={isFavorited ? '#ef4444' : undefined} />
            )
          }
        />
        <Button
          size="$3"
          circular
          chromeless
          pressStyle={{ opacity: 0.7 }}
          onPress={onClose}
          icon={<Feather name="x" size={20} />}
        />
      </XStack>
    </XStack>
  );
}
 
function TagsSection({ tags }: { tags?: string[] }) {
  Iif (!tags?.length) return null;
 
  return (
    <YStack gap="$2" marginBottom="$4">
      <H5 color="$gray12" fontWeight="600">
        🏷️ 产品特征
      </H5>
      <XStack gap="$2" flexWrap="wrap">
        {tags.map((tag, i) => (
          <YStack
            key={i}
            paddingHorizontal="$3"
            paddingVertical="$2"
            backgroundColor="$blue3"
            borderRadius="$3"
            borderWidth={1}
            borderColor="$blue6"
          >
            <Text fontSize="$3" color="$blue11" fontWeight="500">
              {tag}
            </Text>
          </YStack>
        ))}
      </XStack>
    </YStack>
  );
}
 
function AnalysisSection({
  icon,
  title,
  content,
  bgColor,
  borderColor,
}: {
  icon: string;
  title: string;
  content?: string;
  bgColor: string;
  borderColor: string;
}) {
  Iif (!content) return null;
 
  return (
    <YStack gap="$2" marginBottom="$4">
      <H5 color="$gray12" fontWeight="600">
        {icon} {title}
      </H5>
      <YStack
        backgroundColor={bgColor as any}
        padding="$3"
        borderRadius="$3"
        borderWidth={1}
        borderColor={borderColor as any}
      >
        <Text fontSize="$3" color="$gray12" lineHeight={22}>
          {content}
        </Text>
      </YStack>
    </YStack>
  );
}
 
function NutritionSection({ percentData }: { percentData: Record<string, number | null> }) {
  const validEntries = Object.entries(percentData).filter(
    ([_, v]) => v !== null && v !== undefined
  );
 
  Iif (validEntries.length === 0) return null;
 
  return (
    <YStack gap="$3" marginBottom="$4">
      <H5 color="$gray12" fontWeight="600">
        📊 营养成分占比
      </H5>
      {validEntries.map(([key, value]) => (
        <NutrientBar
          key={key}
          label={getNutritionLabel(key)}
          value={value as number}
          color={getNutritionColor(key)}
        />
      ))}
    </YStack>
  );
}
 
function ItemsSection({
  icon,
  title,
  items,
  bgColor,
  borderColor,
  textColor,
}: {
  icon: string;
  title: string;
  items?: string[];
  bgColor: string;
  borderColor: string;
  textColor: string;
}) {
  Iif (!items?.length) return null;
 
  return (
    <>
      <Separator marginVertical="$3" />
      <YStack gap="$2" marginBottom="$4">
        <H5 color="$gray12" fontWeight="600">
          {icon} {title}
        </H5>
        <XStack gap="$2" flexWrap="wrap">
          {items.map((item, i) => (
            <YStack
              key={i}
              paddingHorizontal="$2.5"
              paddingVertical="$1.5"
              backgroundColor={bgColor as any}
              borderRadius="$2"
              borderWidth={1}
              borderColor={borderColor as any}
            >
              <Text fontSize="$2" color={textColor as any}>
                {item}
              </Text>
            </YStack>
          ))}
        </XStack>
      </YStack>
    </>
  );
}
 
function TimestampSection({ createdAt, updatedAt }: { createdAt: string; updatedAt: string }) {
  return (
    <YStack marginTop="$3" alignItems="center">
      <Text fontSize="$2" color="$gray10">
        报告生成时间: {new Date(createdAt).toLocaleString('zh-CN')}
      </Text>
      {updatedAt !== createdAt && (
        <Text fontSize="$2" color="$gray10" marginTop="$1">
          最后更新: {new Date(updatedAt).toLocaleString('zh-CN')}
        </Text>
      )}
    </YStack>
  );
}