2020-06-15 19:35:45 +00:00
|
|
|
import React, { useEffect, useState, useCallback } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {
|
2020-06-17 17:35:58 +00:00
|
|
|
View, Text, FlatList, StyleSheet
|
2020-06-15 19:35:45 +00:00
|
|
|
} from 'react-native';
|
|
|
|
|
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
|
|
|
import shortnameToUnicode from '../../utils/shortnameToUnicode';
|
|
|
|
import CustomEmoji from '../EmojiPicker/CustomEmoji';
|
|
|
|
import database from '../../lib/database';
|
|
|
|
import { Button } from '../ActionSheet';
|
2020-06-17 17:35:58 +00:00
|
|
|
import { useDimensions } from '../../dimensions';
|
2020-11-30 21:47:05 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2020-06-15 19:35:45 +00:00
|
|
|
|
|
|
|
export const HEADER_HEIGHT = 36;
|
2020-07-08 20:45:13 +00:00
|
|
|
const ITEM_SIZE = 36;
|
|
|
|
const CONTAINER_MARGIN = 8;
|
|
|
|
const ITEM_MARGIN = 8;
|
2020-06-15 19:35:45 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
alignItems: 'center',
|
2020-07-08 20:45:13 +00:00
|
|
|
marginHorizontal: CONTAINER_MARGIN
|
2020-06-15 19:35:45 +00:00
|
|
|
},
|
|
|
|
headerItem: {
|
2020-07-08 20:45:13 +00:00
|
|
|
height: ITEM_SIZE,
|
|
|
|
width: ITEM_SIZE,
|
|
|
|
borderRadius: ITEM_SIZE / 2,
|
|
|
|
marginHorizontal: ITEM_MARGIN,
|
2020-06-15 19:35:45 +00:00
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
headerIcon: {
|
2020-11-30 21:47:05 +00:00
|
|
|
...sharedStyles.textAlignCenter,
|
2020-06-15 19:35:45 +00:00
|
|
|
fontSize: 20,
|
|
|
|
color: '#fff'
|
|
|
|
},
|
|
|
|
customEmoji: {
|
|
|
|
height: 20,
|
|
|
|
width: 20
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const keyExtractor = item => item?.id || item;
|
|
|
|
|
|
|
|
const DEFAULT_EMOJIS = ['clap', '+1', 'heart_eyes', 'grinning', 'thinking_face', 'smiley'];
|
|
|
|
|
|
|
|
const HeaderItem = React.memo(({
|
|
|
|
item, onReaction, server, theme
|
|
|
|
}) => (
|
|
|
|
<Button
|
|
|
|
testID={`message-actions-emoji-${ item.content || item }`}
|
|
|
|
onPress={() => onReaction({ emoji: `:${ item.content || item }:` })}
|
|
|
|
style={[styles.headerItem, { backgroundColor: themes[theme].auxiliaryBackground }]}
|
|
|
|
theme={theme}
|
|
|
|
>
|
|
|
|
{item?.isCustom ? (
|
|
|
|
<CustomEmoji style={styles.customEmoji} emoji={item} baseUrl={server} />
|
|
|
|
) : (
|
|
|
|
<Text style={styles.headerIcon}>
|
|
|
|
{shortnameToUnicode(`:${ item.content || item }:`)}
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
));
|
|
|
|
HeaderItem.propTypes = {
|
|
|
|
item: PropTypes.string,
|
|
|
|
onReaction: PropTypes.func,
|
|
|
|
server: PropTypes.string,
|
|
|
|
theme: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
const HeaderFooter = React.memo(({ onReaction, theme }) => (
|
|
|
|
<Button
|
|
|
|
testID='add-reaction'
|
|
|
|
onPress={onReaction}
|
|
|
|
style={[styles.headerItem, { backgroundColor: themes[theme].auxiliaryBackground }]}
|
|
|
|
theme={theme}
|
|
|
|
>
|
2020-07-27 19:53:33 +00:00
|
|
|
<CustomIcon name='reaction-add' size={24} color={themes[theme].bodyText} />
|
2020-06-15 19:35:45 +00:00
|
|
|
</Button>
|
|
|
|
));
|
|
|
|
HeaderFooter.propTypes = {
|
|
|
|
onReaction: PropTypes.func,
|
|
|
|
theme: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
const Header = React.memo(({
|
2020-07-08 20:45:13 +00:00
|
|
|
handleReaction, server, message, isMasterDetail, theme
|
2020-06-15 19:35:45 +00:00
|
|
|
}) => {
|
|
|
|
const [items, setItems] = useState([]);
|
2020-06-17 17:35:58 +00:00
|
|
|
const { width, height } = useDimensions();
|
2020-06-15 19:35:45 +00:00
|
|
|
|
|
|
|
const setEmojis = async() => {
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
2021-03-15 20:16:34 +00:00
|
|
|
const freqEmojiCollection = db.get('frequently_used_emojis');
|
2020-06-15 19:35:45 +00:00
|
|
|
let freqEmojis = await freqEmojiCollection.query().fetch();
|
|
|
|
|
|
|
|
const isLandscape = width > height;
|
2020-07-08 20:45:13 +00:00
|
|
|
const size = (isLandscape || isMasterDetail ? width / 2 : width) - (CONTAINER_MARGIN * 2);
|
|
|
|
const quantity = (size / (ITEM_SIZE + (ITEM_MARGIN * 2))) - 1;
|
2020-06-15 19:35:45 +00:00
|
|
|
|
|
|
|
freqEmojis = freqEmojis.concat(DEFAULT_EMOJIS).slice(0, quantity);
|
|
|
|
setItems(freqEmojis);
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setEmojis();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onReaction = ({ emoji }) => handleReaction(emoji, message);
|
|
|
|
|
|
|
|
const renderItem = useCallback(({ item }) => <HeaderItem item={item} onReaction={onReaction} server={server} theme={theme} />);
|
|
|
|
|
|
|
|
const renderFooter = useCallback(() => <HeaderFooter onReaction={onReaction} theme={theme} />);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={[styles.container, { backgroundColor: themes[theme].focusedBackground }]}>
|
|
|
|
<FlatList
|
|
|
|
data={items}
|
|
|
|
renderItem={renderItem}
|
|
|
|
ListFooterComponent={renderFooter}
|
|
|
|
style={{ backgroundColor: themes[theme].focusedBackground }}
|
|
|
|
keyExtractor={keyExtractor}
|
|
|
|
showsHorizontalScrollIndicator={false}
|
|
|
|
scrollEnabled={false}
|
|
|
|
horizontal
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
Header.propTypes = {
|
|
|
|
handleReaction: PropTypes.func,
|
|
|
|
server: PropTypes.string,
|
|
|
|
message: PropTypes.object,
|
2020-07-08 20:45:13 +00:00
|
|
|
isMasterDetail: PropTypes.bool,
|
2020-06-15 19:35:45 +00:00
|
|
|
theme: PropTypes.string
|
|
|
|
};
|
|
|
|
export default withTheme(Header);
|