diff --git a/lib/selectors/nav-selectors.js b/lib/selectors/nav-selectors.js --- a/lib/selectors/nav-selectors.js +++ b/lib/selectors/nav-selectors.js @@ -3,7 +3,7 @@ import * as React from 'react'; import { createSelector } from 'reselect'; -import { useENSNames } from '../hooks/ens-cache.js'; +import { ENSCacheContext } from '../components/ens-cache-provider.react.js'; import SearchIndex from '../shared/search-index.js'; import { memberHasAdminPowers } from '../shared/thread-utils.js'; import type { Platform } from '../types/device-types.js'; @@ -16,6 +16,7 @@ import type { BaseAppState } from '../types/redux-types.js'; import type { RawThreadInfo, ThreadInfo } from '../types/thread-types.js'; import { getConfig } from '../utils/config.js'; +import { getENSNames } from '../utils/ens-helpers.js'; import { values } from '../utils/objects.js'; import { useSelector } from '../utils/redux-utils.js'; @@ -100,14 +101,34 @@ return [...allMembersOfAllThreads.values()]; }, [threadInfos, userInfos, viewerID]); - const nonViewerMembersWithENSNames = useENSNames(nonViewerMembers); + const cacheContext = React.useContext(ENSCacheContext); + const { ensCache } = cacheContext; + + // We avoid using useENSNames here because the SearchIndex memo below is very + // expensive. useENSNames would trigger its recalculation for each ENS name as + // it streams in, but we would prefer to trigger its recaculation just once + // for every update of the underlying Redux data. + const [nonViewerMembersWithENSNames, setNonViewerMembersWithENSNames] = + React.useState(); + React.useEffect(() => { + if (!ensCache) { + return; + } + (async () => { + const withENSNames = await getENSNames(ensCache, nonViewerMembers); + setNonViewerMembersWithENSNames(withENSNames); + })(); + }, [ensCache, nonViewerMembers]); + + const resolvedNonViewerMembers = + nonViewerMembersWithENSNames ?? nonViewerMembers; const memberMap = React.useMemo(() => { const result = new Map(); - for (const userInfo of nonViewerMembersWithENSNames) { + for (const userInfo of resolvedNonViewerMembers) { result.set(userInfo.id, userInfo); } return result; - }, [nonViewerMembersWithENSNames]); + }, [resolvedNonViewerMembers]); return React.useMemo(() => { const searchIndex = new SearchIndex();