diff --git a/dev-dist/sw.js b/dev-dist/sw.js index da47ea6..619a24c 100644 --- a/dev-dist/sw.js +++ b/dev-dist/sw.js @@ -82,7 +82,7 @@ define(['./workbox-ca84f546'], (function (workbox) { 'use strict'; "revision": "3ca0b8505b4bec776b69afdba2768812" }, { "url": "index.html", - "revision": "0.tucc18p1f38" + "revision": "0.7vc86ovebpk" }], {}); workbox.cleanupOutdatedCaches(); workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), { diff --git a/src/components/Community.tsx b/src/components/Community.tsx index 4a954a0..d8c38ea 100644 --- a/src/components/Community.tsx +++ b/src/components/Community.tsx @@ -102,6 +102,126 @@ export default function Community() { } }, [user]); + // ============ REALTIME SUBSCRIPTIONS ============ + // Subscribe to trade changes + useEffect(() => { + if (!user) return; + + const tradesChannel = supabase + .channel('trades-changes') + .on( + 'postgres_changes', + { + event: '*', + schema: 'public', + table: 'trades', + }, + (payload: any) => { + // Filter for trades involving this user + const newData = payload.new || payload.old; + if (newData && (newData.user1_id === user.id || newData.user2_id === user.id)) { + console.log('Trade change:', payload); + loadTradesData(); + } + } + ) + .subscribe(); + + return () => { + supabase.removeChannel(tradesChannel); + }; + }, [user]); + + // Subscribe to friendship changes + useEffect(() => { + if (!user) return; + + const friendshipsChannel = supabase + .channel('friendships-changes') + .on( + 'postgres_changes', + { + event: '*', + schema: 'public', + table: 'friendships', + }, + (payload: any) => { + // Filter for friendships involving this user + const newData = payload.new || payload.old; + if (newData && (newData.requester_id === user.id || newData.addressee_id === user.id)) { + console.log('Friendship change:', payload); + loadFriendsData(); + } + } + ) + .subscribe(); + + return () => { + supabase.removeChannel(friendshipsChannel); + }; + }, [user]); + + // Subscribe to profile changes (for visibility updates) + useEffect(() => { + if (!user) return; + + const profilesChannel = supabase + .channel('profiles-changes') + .on( + 'postgres_changes', + { + event: 'UPDATE', + schema: 'public', + table: 'profiles', + }, + (payload: any) => { + console.log('Profile change:', payload); + // Reload public users if a profile's visibility changed + if (payload.new && payload.old && payload.new.collection_visibility !== payload.old.collection_visibility) { + loadPublicUsers(); + } + // Reload own profile if it's the current user + if (payload.new && payload.new.id === user.id) { + loadProfile(); + } + } + ) + .subscribe(); + + return () => { + supabase.removeChannel(profilesChannel); + }; + }, [user]); + + // Subscribe to collection changes when viewing someone's collection + useEffect(() => { + if (!user || !selectedUser) return; + + const collectionsChannel = supabase + .channel(`collections-${selectedUser.id}`) + .on( + 'postgres_changes', + { + event: '*', + schema: 'public', + table: 'collections', + }, + (payload: any) => { + // Filter for the selected user's collections + const data = payload.new || payload.old; + if (data && data.user_id === selectedUser.id) { + console.log('Collection change for viewed user:', payload); + loadUserCollection(selectedUser.id); + } + } + ) + .subscribe(); + + return () => { + supabase.removeChannel(collectionsChannel); + }; + }, [user, selectedUser]); + const loadAllData = async () => { if (!user) return; setLoading(true); @@ -502,13 +622,13 @@ export default function Community() { // ============ MAIN VIEW ============ return ( -