"use client"; import { useEffect, useState } from "react"; import { ActivityIndicator, Image, ScrollView, Text, TouchableOpacity, View, } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; import { useAuth } from "@/context/auth-context"; import { getFoods } from "@/services/data/foods"; import { getProfile } from "@/services/data/profile"; import { supabase } from "@/services/supabase"; import { useIsFocused } from "@react-navigation/native"; import { useQuery } from "@tanstack/react-query"; export default function ProfileScreen() { const [activeTab, setActiveTab] = useState("My Recipes"); const { isAuthenticated } = useAuth(); const isFocused = useIsFocused(); const [userId, setUserId] = useState(null); useEffect(() => { let ignore = false; async function getUser() { const { data } = await supabase.auth.getUser(); if (!ignore) { setUserId(data?.user?.id ?? null); } } if (isAuthenticated) getUser(); else setUserId(null); return () => { ignore = true; }; }, [isAuthenticated]); const { data: profileData, error, isLoading, } = useQuery({ queryKey: ["profile", userId], queryFn: async () => { if (!userId) throw new Error("No user id"); return getProfile(userId); }, enabled: !!userId, subscribed: isFocused, }); // Fetch user's foods for 'My Recipes' const { data: foodsData, isLoading: isFoodsLoading, error: foodsError, } = useQuery({ queryKey: ["my-recipes", userId], queryFn: async () => { if (!userId) throw new Error("No user id"); return getFoods(userId); }, enabled: !!userId && activeTab === "My Recipes", subscribed: isFocused && activeTab === "My Recipes", }); // Remove static foodItems, use foodsData for My Recipes return ( {/* Profile Header */} 👨‍🍳 {isLoading ? ( ) : error ? ( {error.message || error.toString()} ) : ( {profileData?.data?.username ?? "-"} )} Edit {/* Tab Navigation */} {["My Recipes", "Likes", "Saved"].map((tab) => ( setActiveTab(tab)} > {tab} ))} {/* Food Grid / Tab Content */} {activeTab === "My Recipes" && ( {isFoodsLoading ? ( ) : foodsError ? ( {foodsError.message || foodsError.toString()} ) : foodsData?.data?.length ? ( foodsData.data.map((item, index) => ( {item.name} )) ) : ( No recipes found. )} )} {activeTab === "Likes" && ( Liked recipes will appear here. )} {activeTab === "Saved" && ( Saved recipes will appear here. )} ); }