deduplicate cards in collection updates for Collection and Community views + async stuff

This commit is contained in:
Matthieu
2025-11-27 15:07:43 +01:00
parent 239a2c9591
commit 1d77b6fb8e
2 changed files with 18 additions and 4 deletions

View File

@@ -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);

View File

@@ -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);