diff --git a/native/avatars/avatar-hooks.js b/native/avatars/avatar-hooks.js --- a/native/avatars/avatar-hooks.js +++ b/native/avatars/avatar-hooks.js @@ -23,6 +23,7 @@ import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js'; import type { ImageAvatarDBContent, + ENSAvatarDBContent, UpdateUserAvatarRemoveRequest, } from 'lib/types/avatar-types.js'; import type { LoadingStatus } from 'lib/types/loading-types.js'; @@ -37,6 +38,7 @@ useServerCall, } from 'lib/utils/action-utils.js'; +import CommIcon from '../components/comm-icon.react.js'; import SWMansionIcon from '../components/swmansion-icon.react.js'; import { getCompatibleMediaURI } from '../media/identifier-utils.js'; import type { MediaResult } from '../media/media-utils.js'; @@ -365,8 +367,39 @@ return removeThreadAvatar; } +function useENSUserAvatar(): [() => Promise, boolean] { + const dispatchActionPromise = useDispatchActionPromise(); + const updateUserAvatarCall = useServerCall(updateUserAvatar); + + const updateUserAvatarLoadingStatus: LoadingStatus = useSelector( + updateUserAvatarLoadingStatusSelector, + ); + + const saveENSUserAvatar = React.useCallback(async () => { + const ensAvatarRequest: ENSAvatarDBContent = { + type: 'ens', + }; + + dispatchActionPromise( + updateUserAvatarActionTypes, + (async () => { + try { + return await updateUserAvatarCall(ensAvatarRequest); + } catch { + Alert.alert('Avatar update failed', 'Unable to update avatar.'); + } + })(), + ); + }, [dispatchActionPromise, updateUserAvatarCall]); + + return React.useMemo( + () => [saveENSUserAvatar, updateUserAvatarLoadingStatus === 'loading'], + [saveENSUserAvatar, updateUserAvatarLoadingStatus], + ); +} + type ShowAvatarActionSheetOptions = { - +id: 'emoji' | 'image' | 'cancel' | 'remove', + +id: 'emoji' | 'image' | 'ens' | 'cancel' | 'remove', +onPress?: () => mixed, }; function useShowAvatarActionSheet( @@ -384,6 +417,8 @@ return 'Use Emoji'; } else if (option.id === 'image') { return 'Select image'; + } else if (option.id === 'ens') { + return 'Use ENS Avatar'; } else if (option.id === 'remove') { return 'Remove avatar'; } else { @@ -416,6 +451,14 @@ style={styles.bottomSheetIcon} /> ); + } else if (option.id === 'ens') { + return ( + + ); } else if (option.id === 'remove') { return ( state.currentUserInfo); + const ethAddress = React.useMemo( + () => getETHAddressForUserInfo(currentUserInfo), + [currentUserInfo], + ); + const ensAvatarURI = useENSAvatar(ethAddress); + const [selectFromGalleryAndUpdateUserAvatar, isGalleryAvatarUpdateLoading] = useSelectFromGalleryAndUpdateUserAvatar(); + const [saveENSUserAvatar, isENSAvatarUpdateLoading] = useENSUserAvatar(); const [removeUserAvatar, isRemoveAvatarUpdateLoading] = useRemoveUserAvatar(); const isAvatarUpdateInProgress = - isGalleryAvatarUpdateLoading || isRemoveAvatarUpdateLoading; + isGalleryAvatarUpdateLoading || + isRemoveAvatarUpdateLoading || + isENSAvatarUpdateLoading; - const actionSheetConfig = React.useMemo( - () => [ + const actionSheetConfig = React.useMemo(() => { + const configOptions = [ { id: 'emoji', onPress: onPressEmojiAvatarFlow }, { id: 'image', onPress: selectFromGalleryAndUpdateUserAvatar }, - { id: 'remove', onPress: removeUserAvatar }, - ], - [ - onPressEmojiAvatarFlow, - removeUserAvatar, - selectFromGalleryAndUpdateUserAvatar, - ], - ); + ]; + + if (ensAvatarURI) { + configOptions.push({ id: 'ens', onPress: saveENSUserAvatar }); + } + + configOptions.push({ id: 'remove', onPress: removeUserAvatar }); + + return configOptions; + }, [ + ensAvatarURI, + onPressEmojiAvatarFlow, + removeUserAvatar, + saveENSUserAvatar, + selectFromGalleryAndUpdateUserAvatar, + ]); const showAvatarActionSheet = useShowAvatarActionSheet(actionSheetConfig);