This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

Refactor checkVerificationsValidity to improve batch processing and state updates

+69 -66
+69 -66
src/components/Verifier/Verifier.js
··· 191 191 } 192 192 }, [session]); 193 193 194 + // Define checkVerificationsValidity *before* fetchVerifications because fetchVerifications depends on it 195 + const checkVerificationsValidity = useCallback(async (verificationsList) => { 196 + if (!verificationsList || verificationsList.length === 0) { 197 + console.log("checkVerificationsValidity called with empty or null list."); 198 + return; // Exit early if list is empty 199 + } 200 + 201 + setIsCheckingValidity(true); 202 + // Create a mutable copy to update status 203 + const updatedVerifications = verificationsList.map(v => ({ ...v })); 204 + try { 205 + const batchSize = 5; 206 + for (let i = 0; i < updatedVerifications.length; i += batchSize) { 207 + const batch = updatedVerifications.slice(i, i + batchSize); 208 + await Promise.all(batch.map(async (verification, index) => { 209 + const batchIndex = i + index; 210 + try { 211 + // *** Get the specific PDS for the verified user *** 212 + const targetDid = verification.subject; 213 + const pdsEndpoint = await getPdsEndpoint(targetDid); 214 + 215 + if (!pdsEndpoint) { 216 + throw new Error(`Could not find PDS for ${verification.handle || targetDid}`); 217 + } 218 + 219 + // *** Use direct fetch to get the profile from the correct PDS *** 220 + const profileUrl = `${pdsEndpoint}/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(targetDid)}`; 221 + const profileResponse = await fetch(profileUrl); 222 + 223 + if (!profileResponse.ok) { 224 + // If profile fetch fails (e.g., 404), mark validity check failed 225 + throw new Error(`Failed to fetch profile from ${pdsEndpoint}: ${profileResponse.status}`); 226 + } 227 + const profileData = await profileResponse.json(); 228 + 229 + // Check if handle and displayName still match 230 + const currentHandle = profileData.handle; 231 + const currentDisplayName = profileData.displayName || profileData.handle; 232 + 233 + updatedVerifications[batchIndex].validityChecked = true; 234 + updatedVerifications[batchIndex].isValid = 235 + currentHandle === verification.handle && 236 + currentDisplayName === verification.displayName; 237 + 238 + if (!updatedVerifications[batchIndex].isValid) { 239 + updatedVerifications[batchIndex].currentHandle = currentHandle; 240 + updatedVerifications[batchIndex].currentDisplayName = currentDisplayName; 241 + } 242 + } catch (err) { 243 + console.error(`Failed to check validity for ${verification.handle || verification.subject}:`, err); 244 + updatedVerifications[batchIndex].validityChecked = true; 245 + updatedVerifications[batchIndex].isValid = false; 246 + updatedVerifications[batchIndex].validityError = true; 247 + } 248 + })); 249 + // Update state after each batch completes to reflect progress 250 + // Use functional update to ensure we're working with the latest state 251 + setVerifications(prev => 252 + prev.map(v => updatedVerifications.find(uv => uv.uri === v.uri) || v) 253 + ); 254 + } 255 + console.log('Verified all records validity (batch processed):', updatedVerifications); 256 + } catch (error) { 257 + console.error('Error during batch processing for validity check:', error); 258 + } finally { 259 + setIsCheckingValidity(false); 260 + } 261 + }, []); // Empty dependency array is likely correct as setters are stable & getPdsEndpoint is global 262 + 194 263 const fetchVerifications = useCallback(async (cursor) => { 195 264 if (!agent || !session) return; 196 265 ··· 267 336 // Note: Removing 'verifications' from dependency array to prevent potential infinite loop 268 337 // The logic relies on setVerifications using the functional update form or constructing the new list manually. 269 338 }, [agent, session, checkVerificationsValidity]); 270 - 271 - const checkVerificationsValidity = useCallback(async (verificationsList) => { 272 - if (!verificationsList || verificationsList.length === 0) { 273 - console.log("checkVerificationsValidity called with empty or null list."); 274 - return; // Exit early if list is empty 275 - } 276 - // if (!agent || verificationsList.length === 0) return; 277 - // Removed agent check as it's not directly used here anymore 278 - 279 - setIsCheckingValidity(true); 280 - const updatedVerifications = [...verificationsList]; 281 - try { 282 - const batchSize = 5; 283 - for (let i = 0; i < updatedVerifications.length; i += batchSize) { 284 - const batch = updatedVerifications.slice(i, i + batchSize); 285 - await Promise.all(batch.map(async (verification, index) => { 286 - const batchIndex = i + index; 287 - try { 288 - // *** Get the specific PDS for the verified user *** 289 - const targetDid = verification.subject; 290 - const pdsEndpoint = await getPdsEndpoint(targetDid); 291 - 292 - if (!pdsEndpoint) { 293 - throw new Error(`Could not find PDS for ${verification.handle || targetDid}`); 294 - } 295 - 296 - // *** Use direct fetch to get the profile from the correct PDS *** 297 - const profileUrl = `${pdsEndpoint}/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(targetDid)}`; 298 - const profileResponse = await fetch(profileUrl); 299 - 300 - if (!profileResponse.ok) { 301 - // If profile fetch fails (e.g., 404), mark validity check failed 302 - throw new Error(`Failed to fetch profile from ${pdsEndpoint}: ${profileResponse.status}`); 303 - } 304 - const profileData = await profileResponse.json(); 305 - 306 - // Check if handle and displayName still match 307 - const currentHandle = profileData.handle; 308 - const currentDisplayName = profileData.displayName || profileData.handle; 309 - 310 - updatedVerifications[batchIndex].validityChecked = true; 311 - updatedVerifications[batchIndex].isValid = 312 - currentHandle === verification.handle && 313 - currentDisplayName === verification.displayName; 314 - 315 - if (!updatedVerifications[batchIndex].isValid) { 316 - updatedVerifications[batchIndex].currentHandle = currentHandle; 317 - updatedVerifications[batchIndex].currentDisplayName = currentDisplayName; 318 - } 319 - } catch (err) { 320 - console.error(`Failed to check validity for ${verification.handle || verification.subject}:`, err); 321 - updatedVerifications[batchIndex].validityChecked = true; 322 - updatedVerifications[batchIndex].isValid = false; 323 - updatedVerifications[batchIndex].validityError = true; 324 - } 325 - })); 326 - // Update state after each batch completes 327 - setVerifications([...updatedVerifications]); 328 - } 329 - console.log('Verified all records validity:', updatedVerifications); 330 - } catch (error) { 331 - console.error('Error during batch processing for validity check:', error); 332 - } finally { 333 - setIsCheckingValidity(false); 334 - } 335 - }, []); // Removed agent dependency as it's no longer used directly here 336 339 337 340 const checkNetworkVerifications = useCallback(async () => { 338 341 if (!agent || !session || !userInfo) {