"use client"; import { IconSymbol } from "@/components/ui/IconSymbol"; import { getFoodById, getIngredients, getNutrients, } from "@/services/data/foods"; import { supabase } from "@/services/supabase"; import { Foods } from "@/types"; import { Ingredient, Nutrient } from "@/types/index"; import { Feather } from "@expo/vector-icons"; import { useQuery } from "@tanstack/react-query"; import { Image } from "expo-image"; import { router, useLocalSearchParams } from "expo-router"; import { useState } from "react"; import { KeyboardAvoidingView, Platform, ScrollView, Text, TouchableOpacity, View, } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; interface Step { id: string; food_id: string; title: string; step_order: number; description: string; created_at: string; } export default function FoodDetailScreen() { const { id } = useLocalSearchParams(); const [activeTab, setActiveTab] = useState("Ingredients"); const foodId = typeof id === "string" ? id : ""; const { data: foodData, isLoading, error, } = useQuery({ queryKey: ["food-detail", foodId], queryFn: async () => { const { data, error } = await getFoodById(foodId); if (error) throw error; if (!data) throw new Error("Food not found"); return data; }, enabled: !!foodId, }); const { data: nutrients, isLoading: nutrientsLoading, error: nutrientsError, } = useQuery({ queryKey: ["food-nutrients", foodId], queryFn: async () => { const { data, error } = await getNutrients(foodId); if (error) throw error; return data; }, enabled: !!foodId && !!foodData, }); const { data: ingredients, error: ingredientsError, isLoading: ingredientsLoading, } = useQuery({ queryKey: ["food-ingredients", foodId], queryFn: async () => { const { data, error } = await getIngredients(foodId); if (error) throw error; return data ?? []; }, enabled: !!foodId && !!foodData, }); const { data: steps, isLoading: stepsLoading, error: stepsError, } = useQuery({ queryKey: ["food-steps", foodId], queryFn: async () => { const { data, error } = await supabase .from("cooking_steps") .select( ` id, food_id, title, step_order, description, created_at ` ) .eq("food_id", foodId) .order("step_order", { ascending: true }); if (error) throw error; return data ?? []; }, enabled: !!foodId && !!foodData, }); if (isLoading || stepsLoading || nutrientsLoading || ingredientsLoading) { return ( Loading... ); } if (error || !foodData || ingredientsError || stepsError || nutrientsError) { return ( Error loading food details router.push("/home")} > Go back to home page ); } const startCookingSession = () => { // Corrected router push to use the actual foodId router.push(`/cooking/${foodId}`); }; return ( {/* Header with back and share buttons */} router.back()} > {/* Food Image */} {foodData.image_url ? ( ) : ( Image not available )} {/* Food Title and Description */} {foodData.name} {foodData.description} {/* Info Tabs */} setActiveTab("Skills")} > Skills {foodData.skill_level} setActiveTab("Time")} > Time {foodData.time_to_cook_minutes} setActiveTab("Ingredients")} > Ingredients {/* Use ingredient_count from foodData or length of the fetched ingredients array */} {foodData.ingredient_count ?? ingredients?.length ?? 0} setActiveTab("Calories")} > Calories {foodData.calories} {/* Ingredients Section */} Ingredients {(ingredients ?? []).map( // Use the 'ingredients' state variable ( ingredient: Ingredient, index: number // Added type for ingredient ) => ( {ingredient.emoji} {ingredient.name} ) )} {/* You might want to show a loading/empty state for ingredients here too */} {/*!ingredientsLoading && ingredients?.length === 0 && ( No ingredients listed. )*/} {/* Nutrition Section - Improved UI */} Nutrition Facts {/* Conditionally render nutrients or show placeholder/loading */} {nutrients ? ( {nutrients.fat_g ?? 0} g Fat {nutrients.fiber_g ?? 0} g Fiber {nutrients.protein_g ?? 0} g Protein {nutrients.carbs_g ?? 0} g Carbs ) : ( Nutrition facts not available. )} {/* Steps Preview */} Cooking Steps {steps && steps.length > 0 ? ( steps.slice(0, 2).map( ( step: Step, index: number // Added type for step ) => ( {step.step_order ?? index + 1}{" "} {/* Use step_order or fallback to index */} {step.description || step.title}{" "} {/* Display description or title */} ) ) ) : ( No cooking steps listed )} {steps && steps.length > 2 && ( ...and {steps.length - 2} more steps )} {/* Cook Button */} Let's Cook! ); }