add realtime updates for collection total value in Collection and Community views

This commit is contained in:
Matthieu
2025-11-27 14:20:37 +01:00
parent 64c48da05a
commit 239a2c9591
2 changed files with 56 additions and 0 deletions

View File

@@ -95,6 +95,34 @@ export default function Collection() {
calculateTotalValue(); calculateTotalValue();
}, [user]); }, [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 // Load user's collection from Supabase on mount
useEffect(() => { useEffect(() => {
const loadCollection = async () => { const loadCollection = async () => {

View File

@@ -405,6 +405,34 @@ export default function Community() {
}; };
}, [selectedUser, hasMoreUserCards, isLoadingMoreUserCards, loadMoreUserCards]); }, [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 ============ // ============ FRIENDS FUNCTIONS ============
const loadFriendsData = async () => { const loadFriendsData = async () => {
if (!user) return; if (!user) return;