"use client" import { supabase } from "@/services/supabase" import { Feather } from "@expo/vector-icons" import { useQuery } from "@tanstack/react-query" import { router } from "expo-router" import { useState, useEffect } from "react" import { ActivityIndicator, ScrollView, Text, View, TextInput, TouchableOpacity, Image, Dimensions } from "react-native" import { SafeAreaView } from "react-native-safe-area-context" interface Recipe { id: number name: string description: string image_url: string created_by: string is_shared: boolean time_to_cook_minutes?: number } const { width } = Dimensions.get("window") const cardWidth = (width - 32) / 2 // 2 cards per row with 16px padding on each side export default function RecipesScreen() { const [searchQuery, setSearchQuery] = useState("") const [filteredRecipes, setFilteredRecipes] = useState([]) const { data: allRecipes, isLoading: isAllLoading, error: allError, } = useQuery({ queryKey: ["all-recipes"], queryFn: async () => { const { data, error } = await supabase .from("foods") .select("*") .eq("is_shared", true) .order("created_at", { ascending: false }) if (error) throw error return data ?? [] }, staleTime: 1000 * 60, }) // Filter recipes based on search query useEffect(() => { if (!allRecipes) return if (!searchQuery.trim()) { setFilteredRecipes(allRecipes) return } const filtered = allRecipes.filter((recipe) => recipe.name.toLowerCase().includes(searchQuery.toLowerCase())) setFilteredRecipes(filtered) }, [searchQuery, allRecipes]) const recipes: Recipe[] = filteredRecipes || [] const loading = isAllLoading const error = allError if (loading) { return ( Loading recipes... ) } if (error) { return ( Failed to load recipes router.push("/home")}> Go back to home ) } return ( All Recipes {/* Search Bar */} {searchQuery.length > 0 && ( setSearchQuery("")}> )} {recipes.length === 0 ? ( {searchQuery.trim() ? `No recipes found for "${searchQuery}"` : "No recipes available."} ) : ( recipes.map((item) => ( router.push(`/food/${item.id}`)} activeOpacity={0.7} > {item.image_url ? ( ) : ( )} {item.name} {item.description || "No description available"} {item.time_to_cook_minutes !== undefined && ( {item.time_to_cook_minutes} min )} )) )} ) }