fix: preserve profile labels from pending updates in useEffect

- Fix merge logic in useEffect that syncs profileLabels state
- Previously was overwriting newly resolved labels when initialLabels changed
- Now preserves existing labels and only adds missing ones from initialLabels
- This fixes the issue where profileLabels was being reset to 0 after applyPendingUpdates
- Add debug logs to track when useEffect sync runs
This commit is contained in:
Gigi
2025-11-02 23:05:16 +01:00
parent 4a432bac8d
commit 945b9502bc

View File

@@ -142,12 +142,15 @@ export function useProfileLabels(
const currentPubkeys = new Set(Array.from(prevLabels.keys())) const currentPubkeys = new Set(Array.from(prevLabels.keys()))
const newPubkeys = new Set(profileData.map(p => p.pubkey)) const newPubkeys = new Set(profileData.map(p => p.pubkey))
console.log(`[shimmer-debug][profile-labels] useEffect sync: prevLabels.size=${prevLabels.size}, initialLabels.size=${initialLabels.size}, profileData.length=${profileData.length}`)
// If the content changed significantly (different set of profiles), reset state // If the content changed significantly (different set of profiles), reset state
const hasDifferentProfiles = const hasDifferentProfiles =
currentPubkeys.size !== newPubkeys.size || currentPubkeys.size !== newPubkeys.size ||
!Array.from(newPubkeys).every(pk => currentPubkeys.has(pk)) !Array.from(newPubkeys).every(pk => currentPubkeys.has(pk))
if (hasDifferentProfiles) { if (hasDifferentProfiles) {
console.log(`[shimmer-debug][profile-labels] useEffect: Different profiles detected, resetting state`)
// Clear pending updates and cancel RAF for old profiles // Clear pending updates and cancel RAF for old profiles
pendingUpdatesRef.current.clear() pendingUpdatesRef.current.clear()
if (rafScheduledRef.current !== null) { if (rafScheduledRef.current !== null) {
@@ -157,14 +160,17 @@ export function useProfileLabels(
// Reset to initial labels // Reset to initial labels
return new Map(initialLabels) return new Map(initialLabels)
} else { } else {
// Same profiles, merge initial labels with existing state (initial labels take precedence for missing ones) // Same profiles, merge initial labels with existing state
// IMPORTANT: Preserve existing labels (from pending updates) and only add initial labels if missing
const merged = new Map(prevLabels) const merged = new Map(prevLabels)
for (const [pubkey, label] of initialLabels.entries()) { for (const [pubkey, label] of initialLabels.entries()) {
// Only update if missing or if initial label has a better value (not a fallback) // Only add initial label if we don't already have a label for this pubkey
if (!merged.has(pubkey) || (!prevLabels.get(pubkey)?.startsWith('@') && label.startsWith('@'))) { // This preserves labels that were added via applyPendingUpdates
if (!merged.has(pubkey)) {
merged.set(pubkey, label) merged.set(pubkey, label)
} }
} }
console.log(`[shimmer-debug][profile-labels] useEffect: Merged labels, before=${prevLabels.size}, after=${merged.size}`)
return merged return merged
} }
}) })