-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.native.js
231 lines (203 loc) · 8.24 KB
/
index.native.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import React, {useState, useMemo, useEffect} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import _ from 'underscore';
import Animated, {runOnUI, scrollTo, useAnimatedRef} from 'react-native-reanimated';
import useWindowDimensions from '../../../hooks/useWindowDimensions';
import compose from '../../../libs/compose';
import CONST from '../../../CONST';
import ONYXKEYS from '../../../ONYXKEYS';
import styles from '../../../styles/styles';
import emojis from '../../../../assets/emojis';
import EmojiPickerMenuItem from '../EmojiPickerMenuItem';
import Text from '../../Text';
import withLocalize, {withLocalizePropTypes} from '../../withLocalize';
import EmojiSkinToneList from '../EmojiSkinToneList';
import * as EmojiUtils from '../../../libs/EmojiUtils';
import * as User from '../../../libs/actions/User';
import TextInput from '../../TextInput';
import CategoryShortcutBar from '../CategoryShortcutBar';
import * as StyleUtils from '../../../styles/StyleUtils';
import useSingleExecution from '../../../hooks/useSingleExecution';
const propTypes = {
/** Function to add the selected emoji to the main compose text input */
onEmojiSelected: PropTypes.func.isRequired,
/** Stores user's preferred skin tone */
preferredSkinTone: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/** Stores user's frequently used emojis */
// eslint-disable-next-line react/forbid-prop-types
frequentlyUsedEmojis: PropTypes.arrayOf(PropTypes.object),
/** Props related to translation */
...withLocalizePropTypes,
};
const defaultProps = {
preferredSkinTone: CONST.EMOJI_DEFAULT_SKIN_TONE,
frequentlyUsedEmojis: [],
};
function EmojiPickerMenu({preferredLocale, onEmojiSelected, preferredSkinTone, translate, frequentlyUsedEmojis}) {
const emojiList = useAnimatedRef();
// eslint-disable-next-line react-hooks/exhaustive-deps
const allEmojis = useMemo(() => EmojiUtils.mergeEmojisWithFrequentlyUsedEmojis(emojis), [frequentlyUsedEmojis]);
const headerEmojis = useMemo(() => EmojiUtils.getHeaderEmojis(allEmojis), [allEmojis]);
const headerRowIndices = useMemo(() => _.map(headerEmojis, (headerEmoji) => Math.floor(headerEmoji.index / CONST.EMOJI_NUM_PER_ROW)), [headerEmojis]);
const [filteredEmojis, setFilteredEmojis] = useState(allEmojis);
const [headerIndices, setHeaderIndices] = useState(headerRowIndices);
const {windowWidth} = useWindowDimensions();
const {singleExecution} = useSingleExecution();
useEffect(() => {
setFilteredEmojis(allEmojis);
}, [allEmojis]);
useEffect(() => {
setHeaderIndices(headerRowIndices);
}, [headerRowIndices]);
const getItemLayout = (data, index) => ({length: CONST.EMOJI_PICKER_ITEM_HEIGHT, offset: CONST.EMOJI_PICKER_ITEM_HEIGHT * index, index});
/**
* Filter the entire list of emojis to only emojis that have the search term in their keywords
*
* @param {String} searchTerm
*/
const filterEmojis = _.debounce((searchTerm) => {
const normalizedSearchTerm = searchTerm.toLowerCase().trim().replaceAll(':', '');
if (emojiList.current) {
emojiList.current.scrollToOffset({offset: 0, animated: false});
}
if (normalizedSearchTerm === '') {
setFilteredEmojis(allEmojis);
setHeaderIndices(headerRowIndices);
return;
}
const newFilteredEmojiList = EmojiUtils.suggestEmojis(`:${normalizedSearchTerm}`, preferredLocale, allEmojis.length);
setFilteredEmojis(newFilteredEmojiList);
setHeaderIndices(undefined);
}, 300);
/**
* @param {String} emoji
* @param {Object} emojiObject
*/
const addToFrequentAndSelectEmoji = (emoji, emojiObject) => {
const frequentEmojiList = EmojiUtils.getFrequentlyUsedEmojis(emojiObject);
User.updateFrequentlyUsedEmojis(frequentEmojiList);
onEmojiSelected(emoji, emojiObject);
};
/**
* @param {Number} skinTone
*/
const updatePreferredSkinTone = (skinTone) => {
if (preferredSkinTone === skinTone) {
return;
}
User.updatePreferredSkinTone(skinTone);
};
const scrollToHeader = (headerIndex) => {
const calculatedOffset = Math.floor(headerIndex / CONST.EMOJI_NUM_PER_ROW) * CONST.EMOJI_PICKER_HEADER_HEIGHT;
emojiList.current.flashScrollIndicators();
runOnUI(() => {
'worklet';
scrollTo(emojiList, 0, calculatedOffset, true);
})();
};
/**
* Return a unique key for each emoji item
*
* @param {Object} item
* @param {Number} index
* @returns {String}
*/
const keyExtractor = (item, index) => `${index}${item.code}`;
/**
* Given an emoji item object, render a component based on its type.
* Items with the code "SPACER" return nothing and are used to fill rows up to 8
* so that the sticky headers function properly
*
* @param {Object} item
* @returns {*}
*/
const renderItem = ({item}) => {
const {code, types} = item;
if (item.spacer) {
return null;
}
if (item.header) {
return (
<View style={styles.emojiHeaderContainer}>
<Text style={styles.textLabelSupporting}>{translate(`emojiPicker.headers.${code}`)}</Text>
</View>
);
}
const emojiCode = types && types[preferredSkinTone] ? types[preferredSkinTone] : code;
return (
<EmojiPickerMenuItem
onPress={singleExecution((emoji) => addToFrequentAndSelectEmoji(emoji, item))}
emoji={emojiCode}
/>
);
};
const isFiltered = allEmojis.length !== filteredEmojis.length;
return (
<View style={styles.emojiPickerContainer}>
<View style={[styles.ph4, styles.pb1, styles.pt2]}>
<TextInput
label={translate('common.search')}
accessibilityLabel={translate('common.search')}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT}
onChangeText={filterEmojis}
/>
</View>
{!isFiltered && (
<CategoryShortcutBar
headerEmojis={headerEmojis}
onPress={scrollToHeader}
/>
)}
<Animated.FlatList
ref={emojiList}
keyboardShouldPersistTaps="handled"
data={filteredEmojis}
renderItem={renderItem}
keyExtractor={keyExtractor}
numColumns={CONST.EMOJI_NUM_PER_ROW}
style={[
StyleUtils.getEmojiPickerListHeight(isFiltered),
{
width: windowWidth,
},
]}
stickyHeaderIndices={headerIndices}
getItemLayout={getItemLayout}
showsVerticalScrollIndicator
// used because of a bug in RN where stickyHeaderIndices can't be updated after the list is rendered https://github.com/facebook/react-native/issues/25157
removeClippedSubviews={false}
contentContainerStyle={styles.flexGrow1}
ListEmptyComponent={<Text style={[styles.disabledText]}>{translate('common.noResultsFound')}</Text>}
alwaysBounceVertical={filteredEmojis.length !== 0}
/>
<EmojiSkinToneList
updatePreferredSkinTone={updatePreferredSkinTone}
preferredSkinTone={preferredSkinTone}
/>
</View>
);
}
EmojiPickerMenu.displayName = 'EmojiPickerMenu';
EmojiPickerMenu.propTypes = propTypes;
EmojiPickerMenu.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
preferredSkinTone: {
key: ONYXKEYS.PREFERRED_EMOJI_SKIN_TONE,
},
frequentlyUsedEmojis: {
key: ONYXKEYS.FREQUENTLY_USED_EMOJIS,
},
}),
)(
React.forwardRef((props, ref) => (
<EmojiPickerMenu
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
)),
);