import React, { useState } from 'react'; import { TouchableOpacity, View, Text, Image } from 'react-native'; import { Ionicons } from '@expo/vector-icons'; import { MealWithItems } from '../../types/Database'; import { AnalysisStatusIndicator } from './AnalysisStatusIndicator'; interface MealCardProps { meal: MealWithItems; onPress: () => void; } export const MealCard: React.FC = ({ meal, onPress }) => { const [imageError, setImageError] = useState(false); const generateMealTitle = (meal: MealWithItems): string => { if (meal.food_items && meal.food_items.length > 0) { const foodNames = meal.food_items.map((item) => item.name); if (foodNames.length === 1) { return foodNames[0]; } else if (foodNames.length === 2) { return `${foodNames[0]} & ${foodNames[1]}`; } else if (foodNames.length > 2) { return `${foodNames[0]} & ${foodNames.length - 1} more`; } } // Fallback to meal type if no food items const mealTypeLabels = { breakfast: 'Breakfast', lunch: 'Lunch', dinner: 'Dinner', snack: 'Snack', }; return mealTypeLabels[meal.meal_type || 'snack'] || 'Meal'; }; const getMealTypeLabel = (mealType?: string): string => { const labels = { breakfast: 'Breakfast', lunch: 'Lunch', dinner: 'Dinner', snack: 'Snack', }; return labels[mealType as keyof typeof labels] || 'Meal'; }; const formatTime = (timestamp: string) => { const date = new Date(timestamp); const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffHours = Math.floor(diffMs / (1000 * 60 * 60)); const diffDays = Math.floor(diffHours / 24); if (diffDays > 0) { return `${diffDays}d ago`; } else if (diffHours > 0) { return `${diffHours}h ago`; } else { return 'Now'; } }; const getMealTypeIcon = (mealType?: string) => { switch (mealType) { case 'breakfast': return '🥐'; case 'lunch': return '🥗'; case 'dinner': return '🍽️'; case 'snack': return '🍎'; default: return '🍽️'; } }; const getHealthScoreColor = (score?: number) => { if (!score) return 'text-gray-400'; if (score >= 80) return 'text-green-400'; if (score >= 60) return 'text-yellow-400'; return 'text-red-400'; }; return ( {/* Background Image */} {meal.photo_path && !imageError ? ( { console.error('MealCard image loading error:', error); console.log('MealCard photo_path:', meal.photo_path); setImageError(true); }} onLoad={() => { console.log('MealCard image loaded successfully:', meal.photo_path); setImageError(false); }} /> ) : ( {getMealTypeIcon(meal.meal_type)} )} {/* Blurry Stats Overlay */} {/* Left side - Meal info */} {generateMealTitle(meal)} {getMealTypeLabel(meal.meal_type)} {formatTime(meal.timestamp)} {/* Location if available */} {meal.location && ( {meal.location} )} {/* Right side - Stats */} {meal.analysis_status === 'completed' && ( {/* Calories */} {meal.total_calories && ( cal {Math.round(meal.total_calories)} )} {/* Health Score */} {meal.health_score && ( health {Math.round(meal.health_score)} )} {/* Rating */} {meal.user_rating && ( rating {meal.user_rating}/5 )} )} {/* Analysis Status for non-completed */} {meal.analysis_status !== 'completed' && ( )} {/* User Notes */} {meal.user_notes && ( “{meal.user_notes}” )} ); };