ATlast — you'll never need to find your favorites on another platform again. Find your favs in the ATmosphere.
atproto

fix excess search result re-renders

byarielm.fyi cf5ff83a da36ed7c

verified
Changed files
+17 -22
src
hooks
+17 -22
src/hooks/useSearch.ts
··· 47 47 const batch = resultsToSearch.slice(i, i + BATCH_SIZE); 48 48 const usernames = batch.map((r) => r.sourceUser.username); 49 49 50 - setSearchResults((prev) => 51 - prev.map((result, index) => 52 - i <= index && index < i + BATCH_SIZE 53 - ? { ...result, isSearching: true } 54 - : result, 55 - ), 56 - ); 57 - 58 50 try { 59 51 const data = await apiClient.batchSearchActors( 60 52 usernames, ··· 79 71 `Searched ${totalSearched} of ${resultsToSearch.length} users. Found ${totalFound} matches.`, 80 72 ); 81 73 74 + // Single state update per batch - updates results with API data 82 75 setSearchResults((prev) => 83 76 prev.map((result, index) => { 84 77 const batchResultIndex = index - i; ··· 112 105 console.error("Batch search error:", error); 113 106 consecutiveErrors++; 114 107 108 + // Single state update on error - marks batch as failed 115 109 setSearchResults((prev) => 116 110 prev.map((result, index) => 117 111 i <= index && index < i + BATCH_SIZE ··· 142 136 } 143 137 144 138 function toggleMatchSelection(resultIndex: number, did: string) { 145 - setSearchResults((prev) => 146 - prev.map((result, index) => { 147 - if (index === resultIndex) { 148 - const newSelectedMatches = new Set(result.selectedMatches); 149 - if (newSelectedMatches.has(did)) { 150 - newSelectedMatches.delete(did); 151 - } else { 152 - newSelectedMatches.add(did); 153 - } 154 - return { ...result, selectedMatches: newSelectedMatches }; 155 - } 156 - return result; 157 - }), 158 - ); 139 + setSearchResults((prev) => { 140 + // Only update the specific item instead of mapping entire array 141 + const newResults = [...prev]; 142 + const result = newResults[resultIndex]; 143 + 144 + const newSelectedMatches = new Set(result.selectedMatches); 145 + if (newSelectedMatches.has(did)) { 146 + newSelectedMatches.delete(did); 147 + } else { 148 + newSelectedMatches.add(did); 149 + } 150 + 151 + newResults[resultIndex] = { ...result, selectedMatches: newSelectedMatches }; 152 + return newResults; 153 + }); 159 154 } 160 155 161 156 function toggleExpandResult(index: number) {