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 @@ -1,7 +1,9 @@ // @flow +import { useActionSheet } from '@expo/react-native-action-sheet'; import * as ImagePicker from 'expo-image-picker'; import * as React from 'react'; +import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { uploadMultimedia } from 'lib/actions/upload-actions.js'; import { @@ -114,8 +116,63 @@ return selectAndUploadFromGallery; } +type ShowAvatarActionSheetOptions = { + +id: string, + +text: string, + +onPress?: () => mixed, + +icon?: React.Node, + +isCancel?: boolean, +}; +function useShowAvatarActionSheet( + options: $ReadOnlyArray, +): () => void { + const insets = useSafeAreaInsets(); + const { showActionSheetWithOptions } = useActionSheet(); + + const showAvatarActionSheet = React.useCallback(() => { + const texts = options.map( + (option: ShowAvatarActionSheetOptions) => option.text, + ); + + const cancelButtonIndex = options.findIndex(option => option.isCancel); + + const containerStyle = { + paddingBotton: insets.bottom, + }; + + const icons = options.map(option => option.icon); + + const onPressAction = (selectedIndex: ?number) => { + if ( + selectedIndex === null || + selectedIndex === undefined || + selectedIndex < 0 + ) { + return; + } + const option = options[selectedIndex]; + if (option.onPress) { + option.onPress(); + } + }; + + showActionSheetWithOptions( + { + options: texts, + cancelButtonIndex, + containerStyle, + icons, + }, + onPressAction, + ); + }, [insets.bottom, options, showActionSheetWithOptions]); + + return showAvatarActionSheet; +} + export { useUploadProcessedMedia, useProcessSelectedMedia, useSelectAndUploadFromGallery, + useShowAvatarActionSheet, }; diff --git a/native/avatars/edit-thread-avatar.react.js b/native/avatars/edit-thread-avatar.react.js --- a/native/avatars/edit-thread-avatar.react.js +++ b/native/avatars/edit-thread-avatar.react.js @@ -1,11 +1,12 @@ // @flow -import { useActionSheet } from '@expo/react-native-action-sheet'; import * as React from 'react'; import { TouchableOpacity, Platform } from 'react-native'; -import { useSafeAreaInsets } from 'react-native-safe-area-context'; -import { useSelectAndUploadFromGallery } from './avatar-hooks.js'; +import { + useSelectAndUploadFromGallery, + useShowAvatarActionSheet, +} from './avatar-hooks.js'; import EditAvatarBadge from './edit-avatar-badge.react.js'; import SWMansionIcon from '../components/swmansion-icon.react.js'; import { useStyles } from '../themes/colors.js'; @@ -17,7 +18,6 @@ }; function EditThreadAvatar(props: Props): React.Node { const { onPressEmojiAvatarFlow, children, disabled } = props; - const { showActionSheetWithOptions } = useActionSheet(); const styles = useStyles(unboundStyles); @@ -65,45 +65,7 @@ styles.bottomSheetIcon, ]); - const insets = useSafeAreaInsets(); - - const onPressEditAvatar = React.useCallback(() => { - const texts = editAvatarOptions.map(option => option.text); - - const cancelButtonIndex = editAvatarOptions.findIndex( - option => option.isCancel, - ); - - const containerStyle = { - paddingBottom: insets.bottom, - }; - - const icons = editAvatarOptions.map(option => option.icon); - - const onPressAction = (selectedIndex: ?number) => { - if ( - selectedIndex === null || - selectedIndex === undefined || - selectedIndex < 0 - ) { - return; - } - const option = editAvatarOptions[selectedIndex]; - if (option.onPress) { - option.onPress(); - } - }; - - showActionSheetWithOptions( - { - options: texts, - cancelButtonIndex, - containerStyle, - icons, - }, - onPressAction, - ); - }, [editAvatarOptions, insets.bottom, showActionSheetWithOptions]); + const showAvatarActionSheet = useShowAvatarActionSheet(editAvatarOptions); let editBadge; if (!disabled) { @@ -111,7 +73,7 @@ } return ( - + {children} {editBadge} diff --git a/native/avatars/edit-user-avatar.react.js b/native/avatars/edit-user-avatar.react.js --- a/native/avatars/edit-user-avatar.react.js +++ b/native/avatars/edit-user-avatar.react.js @@ -1,11 +1,12 @@ // @flow -import { useActionSheet } from '@expo/react-native-action-sheet'; import * as React from 'react'; import { TouchableOpacity, Platform } from 'react-native'; -import { useSafeAreaInsets } from 'react-native-safe-area-context'; -import { useSelectAndUploadFromGallery } from './avatar-hooks.js'; +import { + useSelectAndUploadFromGallery, + useShowAvatarActionSheet, +} from './avatar-hooks.js'; import EditAvatarBadge from './edit-avatar-badge.react.js'; import SWMansionIcon from '../components/swmansion-icon.react.js'; import { useStyles } from '../themes/colors.js'; @@ -17,7 +18,6 @@ }; function EditUserAvatar(props: Props): React.Node { const { onPressEmojiAvatarFlow, children, disabled } = props; - const { showActionSheetWithOptions } = useActionSheet(); const styles = useStyles(unboundStyles); @@ -65,45 +65,7 @@ styles.bottomSheetIcon, ]); - const insets = useSafeAreaInsets(); - - const onPressEditAvatar = React.useCallback(() => { - const texts = editAvatarOptions.map(option => option.text); - - const cancelButtonIndex = editAvatarOptions.findIndex( - option => option.isCancel, - ); - - const containerStyle = { - paddingBottom: insets.bottom, - }; - - const icons = editAvatarOptions.map(option => option.icon); - - const onPressAction = (selectedIndex: ?number) => { - if ( - selectedIndex === null || - selectedIndex === undefined || - selectedIndex < 0 - ) { - return; - } - const option = editAvatarOptions[selectedIndex]; - if (option.onPress) { - option.onPress(); - } - }; - - showActionSheetWithOptions( - { - options: texts, - cancelButtonIndex, - containerStyle, - icons, - }, - onPressAction, - ); - }, [editAvatarOptions, insets.bottom, showActionSheetWithOptions]); + const showAvatarActionSheet = useShowAvatarActionSheet(editAvatarOptions); let editBadge; if (!disabled) { @@ -111,7 +73,7 @@ } return ( - + {children} {editBadge}