diff --git a/lib/hooks/ens-cache.js b/lib/hooks/ens-cache.js index 1340502ad..324d8198e 100644 --- a/lib/hooks/ens-cache.js +++ b/lib/hooks/ens-cache.js @@ -1,67 +1,65 @@ // @flow import * as React from 'react'; import { ENSCacheContext } from '../components/ens-cache-provider.react'; import { userIdentifiedByETHAddress } from '../shared/account-utils'; import { stringForUser } from '../shared/user-utils'; -import type { RelativeMemberInfo } from '../types/thread-types'; -import type { RelativeUserInfo } from '../types/user-types'; function useStringForUser( - user: ?(RelativeUserInfo | RelativeMemberInfo), + user: ?{ +username?: ?string, +isViewer?: ?boolean, ... }, ): ?string { const ethAddress = React.useMemo(() => { if ( !user || user.isViewer || !user.username || !userIdentifiedByETHAddress(user) ) { return null; } return user.username; }, [user]); const cacheContext = React.useContext(ENSCacheContext); const { ensCache } = cacheContext; const cachedResult = ethAddress && ensCache ? ensCache.getCachedNameForAddress(ethAddress) : null; const [ensName, setENSName] = React.useState(null); React.useEffect(() => { // Whenever the ETH address changes, clear out ENS name before requery below setENSName(null); }, [ethAddress]); React.useEffect(() => { if (cachedResult || !ethAddress || !ensCache) { return; } let cancelled = false; (async () => { const result = await ensCache.getNameForAddress(ethAddress); if (result && !cancelled) { setENSName(result); } })(); return () => { cancelled = true; }; }, [cachedResult, ethAddress, ensCache]); if (ensName) { return ensName; } else if (cachedResult) { return cachedResult; } else if (user) { return stringForUser(user); } else { return null; } } export { useStringForUser }; diff --git a/lib/shared/user-utils.js b/lib/shared/user-utils.js index 3239f37da..f92a99fcc 100644 --- a/lib/shared/user-utils.js +++ b/lib/shared/user-utils.js @@ -1,60 +1,57 @@ // @flow import bots from '../facts/bots'; import staff from '../facts/staff'; import type { ServerThreadInfo, RawThreadInfo, - RelativeMemberInfo, ThreadInfo, } from '../types/thread-types'; -import type { - RelativeUserInfo, - UserInfo, - UserInfos, -} from '../types/user-types'; +import type { UserInfo, UserInfos } from '../types/user-types'; import { memberHasAdminPowers } from './thread-utils'; -function stringForUser(user: RelativeUserInfo | RelativeMemberInfo): string { +function stringForUser(user: { + +username?: ?string, + +isViewer?: ?boolean, + ... +}): string { if (user.isViewer) { return 'you'; } return stringForUserExplicit(user); } -function stringForUserExplicit( - user: RelativeUserInfo | RelativeMemberInfo, -): string { +function stringForUserExplicit(user: { +username: ?string, ... }): string { if (user.username) { return user.username; } else { return 'anonymous'; } } function isStaff(userID: string): boolean { if (staff.includes(userID)) { return true; } for (const key in bots) { const bot = bots[key]; if (userID === bot.userID) { return true; } } return false; } function getKeyserverAdmin( community: ThreadInfo | RawThreadInfo | ServerThreadInfo, userInfos: UserInfos, ): ?UserInfo { // This hack only works as long as there is only one admin // Linear task to revert this: // https://linear.app/comm/issue/ENG-1707/revert-fix-getting-the-keyserver-admin-info const admin = community.members.find(memberHasAdminPowers); return admin ? userInfos[admin.id] : undefined; } export { stringForUser, stringForUserExplicit, isStaff, getKeyserverAdmin };