2024-01-25 14:11:07 +00:00
|
|
|
import React, { useContext, useState } from 'react';
|
2022-10-21 18:27:55 +00:00
|
|
|
import { View, Text, Pressable, FlatList, StyleSheet } from 'react-native';
|
|
|
|
|
2024-01-25 14:11:07 +00:00
|
|
|
import { MessageInnerContext, useMessageComposerApi, useShowEmojiSearchbar } from '../context';
|
|
|
|
import { useTheme } from '../../../theme';
|
|
|
|
import I18n from '../../../i18n';
|
|
|
|
import { CustomIcon } from '../../CustomIcon';
|
|
|
|
import { IEmoji } from '../../../definitions';
|
|
|
|
import { useFrequentlyUsedEmoji } from '../../../lib/hooks';
|
|
|
|
import { addFrequentlyUsed, searchEmojis } from '../../../lib/methods';
|
|
|
|
import { useDebounce } from '../../../lib/methods/helpers';
|
|
|
|
import sharedStyles from '../../../views/Styles';
|
|
|
|
import { PressableEmoji } from '../../EmojiPicker/PressableEmoji';
|
|
|
|
import { EmojiSearch } from '../../EmojiPicker/EmojiSearch';
|
|
|
|
import { EMOJI_BUTTON_SIZE } from '../../EmojiPicker/styles';
|
2022-10-21 18:27:55 +00:00
|
|
|
|
|
|
|
const BUTTON_HIT_SLOP = { top: 4, right: 4, bottom: 4, left: 4 };
|
|
|
|
|
2024-01-25 14:11:07 +00:00
|
|
|
export const EmojiSearchbar = (): React.ReactElement | null => {
|
2022-10-21 18:27:55 +00:00
|
|
|
const { colors } = useTheme();
|
|
|
|
const [searchText, setSearchText] = useState<string>('');
|
2024-01-25 14:11:07 +00:00
|
|
|
const showEmojiSearchbar = useShowEmojiSearchbar();
|
|
|
|
const { openEmojiKeyboard, closeEmojiKeyboard } = useMessageComposerApi();
|
|
|
|
const { onEmojiSelected } = useContext(MessageInnerContext);
|
2022-10-21 18:27:55 +00:00
|
|
|
const { frequentlyUsed } = useFrequentlyUsedEmoji(true);
|
|
|
|
const [emojis, setEmojis] = useState<IEmoji[]>([]);
|
|
|
|
|
|
|
|
const handleTextChange = useDebounce(async (text: string) => {
|
|
|
|
setSearchText(text);
|
|
|
|
const result = await searchEmojis(text);
|
|
|
|
setEmojis(result);
|
|
|
|
}, 300);
|
|
|
|
|
|
|
|
const handleEmojiSelected = (emoji: IEmoji) => {
|
|
|
|
onEmojiSelected(emoji);
|
|
|
|
addFrequentlyUsed(emoji);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderItem = ({ item }: { item: IEmoji }) => <PressableEmoji emoji={item} onPress={handleEmojiSelected} />;
|
|
|
|
|
2024-01-25 14:11:07 +00:00
|
|
|
if (!showEmojiSearchbar) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Use RNGH
|
2022-10-21 18:27:55 +00:00
|
|
|
return (
|
2024-01-25 14:11:07 +00:00
|
|
|
<View style={{ backgroundColor: colors.surfaceLight }}>
|
2022-10-21 18:27:55 +00:00
|
|
|
<FlatList
|
|
|
|
horizontal
|
|
|
|
data={searchText ? emojis : frequentlyUsed}
|
|
|
|
renderItem={renderItem}
|
|
|
|
showsHorizontalScrollIndicator={false}
|
|
|
|
ListEmptyComponent={() => (
|
|
|
|
<View style={styles.emptyContainer} testID='no-results-found'>
|
2024-01-25 14:11:07 +00:00
|
|
|
<Text style={[styles.emptyText, { color: colors.fontHint }]}>{I18n.t('No_results_found')}</Text>
|
2022-10-21 18:27:55 +00:00
|
|
|
</View>
|
|
|
|
)}
|
|
|
|
keyExtractor={item => (typeof item === 'string' ? item : item.name)}
|
|
|
|
contentContainerStyle={styles.listContainer}
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
<View style={styles.searchContainer}>
|
|
|
|
<Pressable
|
|
|
|
style={({ pressed }: { pressed: boolean }) => [styles.backButton, { opacity: pressed ? 0.7 : 1 }]}
|
2024-01-25 14:11:07 +00:00
|
|
|
onPress={openEmojiKeyboard}
|
2022-10-21 18:27:55 +00:00
|
|
|
hitSlop={BUTTON_HIT_SLOP}
|
|
|
|
testID='openback-emoji-keyboard'
|
|
|
|
>
|
2024-01-25 14:11:07 +00:00
|
|
|
<CustomIcon name='chevron-left' size={24} color={colors.fontHint} />
|
2022-10-21 18:27:55 +00:00
|
|
|
</Pressable>
|
|
|
|
<View style={styles.inputContainer}>
|
2024-01-25 14:11:07 +00:00
|
|
|
<EmojiSearch onBlur={closeEmojiKeyboard} onChangeText={handleTextChange} />
|
2022-10-21 18:27:55 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-01-25 14:11:07 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
listContainer: {
|
|
|
|
height: EMOJI_BUTTON_SIZE,
|
|
|
|
margin: 8,
|
|
|
|
flexGrow: 1
|
|
|
|
},
|
|
|
|
searchContainer: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
marginRight: 12,
|
|
|
|
marginBottom: 12
|
|
|
|
},
|
|
|
|
backButton: {
|
|
|
|
width: 32,
|
|
|
|
height: 32,
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
borderRadius: 4
|
|
|
|
},
|
|
|
|
emptyContainer: {
|
|
|
|
flex: 1,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
emptyText: {
|
|
|
|
...sharedStyles.textRegular,
|
|
|
|
fontSize: 16
|
|
|
|
},
|
|
|
|
inputContainer: {
|
|
|
|
flex: 1
|
|
|
|
}
|
|
|
|
});
|