Changeset View
Changeset View
Standalone View
Standalone View
native/components/user-avatar.react.js
// @flow | // @flow | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import { useENSAvatar } from 'lib/hooks/ens-cache.js'; | |||||
import { userIdentifiedByETHAddress } from 'lib/shared/account-utils.js'; | |||||
import { | import { | ||||
defaultAnonymousUserEmojiAvatar, | |||||
getAvatarForUser, | getAvatarForUser, | ||||
useENSResolvedAvatar, | |||||
} from 'lib/shared/avatar-utils.js'; | } from 'lib/shared/avatar-utils.js'; | ||||
import Avatar from './avatar.react.js'; | import Avatar from './avatar.react.js'; | ||||
import { useSelector } from '../redux/redux-utils.js'; | import { useSelector } from '../redux/redux-utils.js'; | ||||
type Props = { | type Props = { | ||||
+userID: ?string, | +userID: ?string, | ||||
+size: 'micro' | 'small' | 'large' | 'profile', | +size: 'micro' | 'small' | 'large' | 'profile', | ||||
}; | }; | ||||
function UserAvatar(props: Props): React.Node { | function UserAvatar(props: Props): React.Node { | ||||
const { userID, size } = props; | const { userID, size } = props; | ||||
const userInfo = useSelector(state => | const userInfo = useSelector(state => | ||||
userID ? state.userStore.userInfos[userID] : null, | userID ? state.userStore.userInfos[userID] : null, | ||||
); | ); | ||||
const avatarInfo = getAvatarForUser(userInfo); | const avatarInfo = getAvatarForUser(userInfo); | ||||
const ethAddress = React.useMemo(() => { | const resolvedUserAvatar = useENSResolvedAvatar(avatarInfo, userInfo); | ||||
let address = null; | |||||
if (userInfo && avatarInfo.type === 'ens') { | |||||
const { username } = userInfo; | |||||
address = | |||||
username && userIdentifiedByETHAddress(userInfo) ? username : null; | |||||
} | |||||
return address; | |||||
}, [avatarInfo.type, userInfo]); | |||||
const ensAvatarURI = useENSAvatar(ethAddress); | |||||
const userAvatarInfo = React.useMemo(() => { | |||||
if (avatarInfo.type !== 'ens') { | |||||
return avatarInfo; | |||||
} | |||||
if (ensAvatarURI) { | |||||
return { | |||||
type: 'image', | |||||
uri: ensAvatarURI, | |||||
}; | |||||
} | |||||
return defaultAnonymousUserEmojiAvatar; | |||||
}, [ensAvatarURI, avatarInfo]); | |||||
return <Avatar size={size} avatarInfo={userAvatarInfo} />; | return <Avatar size={size} avatarInfo={resolvedUserAvatar} />; | ||||
} | } | ||||
export default UserAvatar; | export default UserAvatar; |