From 1d77b6fb8e55fa1572dd9fc02eea82d2a18a7e69 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Thu, 27 Nov 2025 15:07:43 +0100 Subject: [PATCH] deduplicate cards in collection updates for Collection and Community views + async stuff --- src/components/Collection.tsx | 8 +++++++- src/components/Community.tsx | 14 +++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/components/Collection.tsx b/src/components/Collection.tsx index 4838024..118936f 100644 --- a/src/components/Collection.tsx +++ b/src/components/Collection.tsx @@ -196,7 +196,13 @@ export default function Collection() { quantity: result.items.get(card.id) || 0, })); - setCollection(prev => [...prev, ...newCards]); + // Deduplicate: only add cards that aren't already in the collection + setCollection(prev => { + const existingIds = new Set(prev.map(item => item.card.id)); + const uniqueNewCards = newCards.filter(item => !existingIds.has(item.card.id)); + return [...prev, ...uniqueNewCards]; + }); + setOffset(prev => prev + PAGE_SIZE); } catch (error) { console.error('Error loading more cards:', error); diff --git a/src/components/Community.tsx b/src/components/Community.tsx index 441d32a..4f72391 100644 --- a/src/components/Community.tsx +++ b/src/components/Community.tsx @@ -208,6 +208,7 @@ export default function Community() { }, [user]); // Subscribe to collection changes when viewing someone's collection + // Auto-price trigger is disabled, so no more infinite loops! useEffect(() => { if (!user || !selectedUser) return; @@ -221,10 +222,11 @@ export default function Community() { 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); + console.log('Collection change for viewed user:', payload.eventType); + // Reload on any change (INSERT/UPDATE/DELETE) + // No more infinite loops since auto-price trigger is disabled loadUserCollection(selectedUser.id); } } @@ -371,7 +373,13 @@ export default function Community() { quantity: result.items.get(card.id) || 0, })); - setSelectedUserCollection(prev => [...prev, ...newCards]); + // Deduplicate: only add cards that aren't already in the collection + setSelectedUserCollection(prev => { + const existingIds = new Set(prev.map(item => item.card.id)); + const uniqueNewCards = newCards.filter(item => !existingIds.has(item.card.id)); + return [...prev, ...uniqueNewCards]; + }); + setUserCollectionOffset(prev => prev + PAGE_SIZE); } catch (error) { console.error('Error loading more cards:', error);