import React, {useCallback} from 'react' import {Alert, Image, StyleSheet, TouchableOpacity, View} from 'react-native' import Svg, {Circle, Text, Defs, LinearGradient, Stop} from 'react-native-svg' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import { openCamera, openCropper, openPicker, PickedMedia, } from './images/image-crop-picker/ImageCropPicker' import {useStores} from '../../../state' import {colors, gradients} from '../../lib/styles' export function UserAvatar({ size, handle, avatar, displayName, onSelectNewAvatar, }: { size: number handle: string displayName: string | undefined avatar?: string | null onSelectNewAvatar?: (img: PickedMedia) => void }) { const store = useStores() const initials = getInitials(displayName || handle) const handleEditAvatar = useCallback(() => { Alert.alert('Select upload method', '', [ { text: 'Take a new photo', onPress: () => { openCamera(store, { mediaType: 'photo', width: 1000, height: 1000, cropperCircleOverlay: true, }).then(onSelectNewAvatar) }, }, { text: 'Select from gallery', onPress: () => { openPicker(store, { mediaType: 'photo', }).then(async items => { await openCropper(store, { mediaType: 'photo', path: items[0].path, width: 1000, height: 1000, cropperCircleOverlay: true, }).then(onSelectNewAvatar) }) }, }, ]) }, [store, onSelectNewAvatar]) const renderSvg = (svgSize: number, svgInitials: string) => ( {svgInitials} ) // onSelectNewAvatar is only passed as prop on the EditProfile component return onSelectNewAvatar ? ( {avatar ? ( ) : ( renderSvg(size, initials) )} ) : avatar ? ( ) : ( renderSvg(size, initials) ) } function getInitials(str: string): string { const tokens = str .toLowerCase() .replace(/[^a-z]/g, '') .split(' ') .filter(Boolean) .map(v => v.trim()) if (tokens.length >= 2 && tokens[0][0] && tokens[0][1]) { return tokens[0][0].toUpperCase() + tokens[1][0].toUpperCase() } if (tokens.length === 1 && tokens[0][0]) { return tokens[0][0].toUpperCase() } return 'X' } const styles = StyleSheet.create({ editButtonContainer: { position: 'absolute', width: 24, height: 24, bottom: 0, right: 0, borderRadius: 12, alignItems: 'center', justifyContent: 'center', backgroundColor: colors.gray5, }, avatarImage: { width: 80, height: 80, borderRadius: 40, }, })