import { useEffect, useState } from 'react'; import { StyleSheet, Text, View } from 'react-native'; import PrimaryButton from '../components/PrimaryButton'; import AppHeader from '../components/layout/AppHeader'; import ScreenContainer from '../components/layout/ScreenContainer'; import AlternativeProductCard from '../components/product/AlternativeProductCard'; import { alternativeSearchIssue, findAlternatives } from '../services/alternativesService'; import { calculateProductRating } from '../services/ratingService'; import { colors } from '../utils/constants'; const DISPLAY_ALTERNATIVE_LIMIT = 8; export default function AlternativesScreen({ product, preferences, productCache, shoppingList, dailyLog, onBack, onChoose, onOpenProduct }) { const [alternatives, setAlternatives] = useState([]); const [isLoading, setIsLoading] = useState(true); const [searchError, setSearchError] = useState(''); const [retryCount, setRetryCount] = useState(0); useEffect(() => { let isMounted = true; async function loadAlternatives() { setIsLoading(true); setSearchError(''); try { const results = await findAlternatives(product, preferences, productCache, { shoppingList, dailyLog }); if (isMounted) setAlternatives(results); } catch (error) { if (isMounted) { setAlternatives([]); setSearchError('Open Food Facts ist gerade nicht erreichbar. Versuche es später erneut oder nutze den Offline-Modus.'); } } finally { if (isMounted) setIsLoading(false); } } loadAlternatives(); return () => { isMounted = false; }; }, [product, preferences, productCache, shoppingList, dailyLog, retryCount]); return ( {isLoading ? {preferences.offlineModeEnabled ? 'Gespeicherte Produkte werden verglichen...' : 'Open Food Facts wird nach besseren Produkten derselben Produktart durchsucht...'} : null} {searchError ? {searchError} : null} {searchError ? setRetryCount((count) => count + 1)} /> : null} {!isLoading && alternatives.length > 0 ? ( {alternatives.length} mögliche Alternative{alternatives.length === 1 ? '' : 'n'} gefunden. ) : null} {!isLoading && !searchError && alternatives.length === 0 ? ( Keine Alternative gefunden {preferences.offlineModeEnabled ? 'Im lokalen Cache ist noch keine bessere Option derselben Produktart gespeichert. Deaktiviere den Offline-Modus für die Online-Suche.' : alternativeSearchIssue(product, calculateProductRating(product, preferences, { shoppingList, dailyLog })) || 'Unter den geprüften Open Food Facts Treffern war keine belegbar bessere Option derselben Produktart mit ausreichenden Angaben.'} ) : null} {alternatives.slice(0, DISPLAY_ALTERNATIVE_LIMIT).map((item) => ( { if (!item.requiresAllergenCheck) { onChoose(item.product); return; } onOpenProduct(item.product); }} onOpen={() => onOpenProduct(item.product)} /> ))} ); } const styles = StyleSheet.create({ note: { color: colors.muted, fontSize: 14 }, empty: { backgroundColor: colors.surface, borderColor: colors.border, borderRadius: 18, borderWidth: 1, padding: 16 }, emptyTitle: { color: colors.text, fontSize: 16, fontWeight: '800' }, emptyCopy: { color: colors.muted, fontSize: 14, lineHeight: 20, marginTop: 4 } });