2022-12-12 21:13:12 +00:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2022-12-20 16:40:53 +00:00
|
|
|
import { Text, View } from 'react-native';
|
2022-12-07 05:01:00 +00:00
|
|
|
|
2022-12-07 23:17:08 +00:00
|
|
|
import { IAvatar, IUser } from '../../definitions';
|
2022-12-12 21:13:12 +00:00
|
|
|
import { Services } from '../../lib/services';
|
2022-12-07 05:01:00 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import Avatar from '../../containers/Avatar';
|
|
|
|
import styles from './styles';
|
|
|
|
import { useTheme } from '../../theme';
|
2022-12-20 16:40:53 +00:00
|
|
|
import Touch from '../../containers/Touch';
|
2022-12-07 05:01:00 +00:00
|
|
|
|
2022-12-20 16:40:53 +00:00
|
|
|
const Item = ({ item, onPress, text, testID }: { item?: IAvatar; testID?: string; onPress: Function; text?: string }) => {
|
2022-12-07 05:01:00 +00:00
|
|
|
const { colors } = useTheme();
|
|
|
|
|
|
|
|
return (
|
2022-12-20 16:40:53 +00:00
|
|
|
<Touch
|
2022-12-07 23:17:08 +00:00
|
|
|
key={item?.service}
|
2022-12-14 22:09:14 +00:00
|
|
|
testID={testID}
|
2022-12-07 23:17:08 +00:00
|
|
|
onPress={() => onPress(item)}
|
|
|
|
style={[styles.avatarButton, { backgroundColor: colors.borderColor }]}
|
|
|
|
>
|
|
|
|
<Avatar avatar={item?.url} text={text} size={64} />
|
2022-12-20 16:40:53 +00:00
|
|
|
</Touch>
|
2022-12-07 05:01:00 +00:00
|
|
|
);
|
|
|
|
};
|
2022-12-07 23:17:08 +00:00
|
|
|
const AvatarSuggestion = ({
|
|
|
|
onPress,
|
|
|
|
user,
|
|
|
|
resetAvatar
|
|
|
|
}: {
|
2022-12-20 16:40:53 +00:00
|
|
|
onPress: (value: IAvatar | null) => void;
|
2022-12-07 23:17:08 +00:00
|
|
|
user?: IUser;
|
|
|
|
resetAvatar?: () => void;
|
2022-12-12 21:13:12 +00:00
|
|
|
}) => {
|
|
|
|
const [avatarSuggestions, setAvatarSuggestions] = useState<IAvatar[]>([]);
|
2022-12-20 16:40:53 +00:00
|
|
|
|
2022-12-15 12:51:26 +00:00
|
|
|
const { colors } = useTheme();
|
2022-12-20 16:40:53 +00:00
|
|
|
|
2022-12-12 21:13:12 +00:00
|
|
|
const getAvatarSuggestion = async () => {
|
|
|
|
const result = await Services.getAvatarSuggestion();
|
|
|
|
const suggestions = Object.keys(result).map(service => {
|
|
|
|
const { url, blob, contentType } = result[service];
|
|
|
|
return {
|
|
|
|
url,
|
|
|
|
data: blob,
|
|
|
|
service,
|
|
|
|
contentType
|
|
|
|
};
|
|
|
|
});
|
|
|
|
setAvatarSuggestions(suggestions);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getAvatarSuggestion();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={{ flex: 1 }}>
|
2022-12-15 12:51:26 +00:00
|
|
|
<Text style={[styles.itemLabel, { color: colors.titleText }]}>{I18n.t('Images_uploaded')}</Text>
|
2022-12-12 21:13:12 +00:00
|
|
|
<View style={styles.containerAvatarSuggestion}>
|
2022-12-14 22:09:14 +00:00
|
|
|
{user?.username && resetAvatar ? (
|
|
|
|
<Item text={`@${user.username}`} testID={`reset-avatar-suggestion`} onPress={resetAvatar} />
|
|
|
|
) : null}
|
2022-12-12 21:13:12 +00:00
|
|
|
{avatarSuggestions.slice(0, 7).map(item => (
|
2022-12-14 22:09:14 +00:00
|
|
|
<Item item={item} testID={`${item?.service}-avatar-suggestion`} onPress={onPress} />
|
2022-12-12 21:13:12 +00:00
|
|
|
))}
|
|
|
|
</View>
|
2022-12-07 23:17:08 +00:00
|
|
|
</View>
|
2022-12-12 21:13:12 +00:00
|
|
|
);
|
|
|
|
};
|
2022-12-07 05:01:00 +00:00
|
|
|
|
|
|
|
export default AvatarSuggestion;
|