diff --git a/src/components/Collection.tsx b/src/components/Collection.tsx index 1dfcac9..4838024 100644 --- a/src/components/Collection.tsx +++ b/src/components/Collection.tsx @@ -95,6 +95,34 @@ export default function Collection() { calculateTotalValue(); }, [user]); + // Subscribe to realtime updates for collection total value + useEffect(() => { + if (!user) return; + + const profileChannel = supabase + .channel('profile-total-value-changes') + .on( + 'postgres_changes', + { + event: 'UPDATE', + schema: 'public', + table: 'profiles', + filter: `id=eq.${user.id}`, + }, + (payload: any) => { + if (payload.new?.collection_total_value !== undefined) { + console.log('Collection total value updated:', payload.new.collection_total_value); + setTotalCollectionValue(payload.new.collection_total_value); + } + } + ) + .subscribe(); + + return () => { + supabase.removeChannel(profileChannel); + }; + }, [user]); + // Load user's collection from Supabase on mount useEffect(() => { const loadCollection = async () => { diff --git a/src/components/Community.tsx b/src/components/Community.tsx index 07aa58c..441d32a 100644 --- a/src/components/Community.tsx +++ b/src/components/Community.tsx @@ -405,6 +405,34 @@ export default function Community() { }; }, [selectedUser, hasMoreUserCards, isLoadingMoreUserCards, loadMoreUserCards]); + // Subscribe to realtime updates for selected user's collection total value + useEffect(() => { + if (!selectedUser) return; + + const userProfileChannel = supabase + .channel(`user-profile-value-${selectedUser.id}`) + .on( + 'postgres_changes', + { + event: 'UPDATE', + schema: 'public', + table: 'profiles', + filter: `id=eq.${selectedUser.id}`, + }, + (payload: any) => { + if (payload.new?.collection_total_value !== undefined) { + console.log(`User ${selectedUser.username}'s collection total value updated:`, payload.new.collection_total_value); + setUserCollectionTotalValue(payload.new.collection_total_value); + } + } + ) + .subscribe(); + + return () => { + supabase.removeChannel(userProfileChannel); + }; + }, [selectedUser]); + // ============ FRIENDS FUNCTIONS ============ const loadFriendsData = async () => { if (!user) return;