2022-10-21 18:27:55 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { FlatList, StyleSheet, Text, View } from 'react-native';
|
2020-06-15 19:35:45 +00:00
|
|
|
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, useTheme } from '../../theme';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2022-06-06 14:17:51 +00:00
|
|
|
import shortnameToUnicode from '../../lib/methods/helpers/shortnameToUnicode';
|
2022-10-21 18:27:55 +00:00
|
|
|
import { addFrequentlyUsed } from '../../lib/methods';
|
|
|
|
import { useFrequentlyUsedEmoji } from '../../lib/hooks';
|
2020-06-15 19:35:45 +00:00
|
|
|
import CustomEmoji from '../EmojiPicker/CustomEmoji';
|
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';
|
2022-10-21 18:27:55 +00:00
|
|
|
import { IEmoji, TAnyMessageModel } from '../../definitions';
|
2022-08-17 13:32:21 +00:00
|
|
|
import Touch from '../Touch';
|
2022-03-31 12:38:20 +00:00
|
|
|
|
|
|
|
export interface IHeader {
|
2022-10-21 18:27:55 +00:00
|
|
|
handleReaction: (emoji: IEmoji, message: TAnyMessageModel) => void;
|
2022-03-31 12:38:20 +00:00
|
|
|
message: TAnyMessageModel;
|
2021-09-13 20:41:05 +00:00
|
|
|
isMasterDetail: boolean;
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
type TOnReaction = ({ emoji }: { emoji: IEmoji }) => void;
|
2022-03-31 12:38:20 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface THeaderItem {
|
2022-10-21 18:27:55 +00:00
|
|
|
item: IEmoji;
|
2022-03-31 12:38:20 +00:00
|
|
|
onReaction: TOnReaction;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface THeaderFooter {
|
2022-03-31 12:38:20 +00:00
|
|
|
onReaction: TOnReaction;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
const HeaderItem = ({ item, onReaction, theme }: THeaderItem) => (
|
|
|
|
<Touch
|
|
|
|
testID={`message-actions-emoji-${item}`}
|
|
|
|
onPress={() => onReaction({ emoji: item })}
|
|
|
|
style={[styles.headerItem, { backgroundColor: themes[theme].auxiliaryBackground }]}
|
|
|
|
>
|
|
|
|
{typeof item === 'string' ? (
|
|
|
|
<Text style={styles.headerIcon}>{shortnameToUnicode(`:${item}:`)}</Text>
|
|
|
|
) : (
|
|
|
|
<CustomEmoji style={styles.customEmoji} emoji={item} />
|
|
|
|
)}
|
|
|
|
</Touch>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
const HeaderFooter = ({ onReaction, theme }: THeaderFooter) => (
|
2022-08-17 13:32:21 +00:00
|
|
|
<Touch
|
2020-06-15 19:35:45 +00:00
|
|
|
testID='add-reaction'
|
2022-08-17 13:32:21 +00:00
|
|
|
onPress={(param: any) => onReaction(param)}
|
2020-06-15 19:35:45 +00:00
|
|
|
style={[styles.headerItem, { backgroundColor: themes[theme].auxiliaryBackground }]}
|
2022-08-08 21:02:08 +00:00
|
|
|
>
|
2020-07-27 19:53:33 +00:00
|
|
|
<CustomIcon name='reaction-add' size={24} color={themes[theme].bodyText} />
|
2022-08-17 13:32:21 +00:00
|
|
|
</Touch>
|
2022-03-31 12:38:20 +00:00
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
const Header = React.memo(({ handleReaction, message, isMasterDetail }: IHeader) => {
|
2022-03-31 12:38:20 +00:00
|
|
|
const { width, height } = useDimensions();
|
|
|
|
const { theme } = useTheme();
|
2022-10-21 18:27:55 +00:00
|
|
|
const { frequentlyUsed, loaded } = useFrequentlyUsedEmoji(true);
|
|
|
|
const isLandscape = width > height;
|
|
|
|
const size = (isLandscape || isMasterDetail ? width / 2 : width) - CONTAINER_MARGIN * 2;
|
|
|
|
const quantity = Math.trunc(size / (ITEM_SIZE + ITEM_MARGIN * 2) - 1);
|
|
|
|
|
|
|
|
const onReaction: TOnReaction = ({ emoji }) => {
|
|
|
|
handleReaction(emoji, message);
|
|
|
|
addFrequentlyUsed(emoji);
|
2020-06-15 19:35:45 +00:00
|
|
|
};
|
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
const renderItem = ({ item }: { item: IEmoji }) => <HeaderItem item={item} onReaction={onReaction} theme={theme} />;
|
2020-06-15 19:35:45 +00:00
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
const renderFooter = () => <HeaderFooter onReaction={onReaction} theme={theme} />;
|
2020-06-15 19:35:45 +00:00
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
if (!loaded) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
return (
|
2022-03-31 12:38:20 +00:00
|
|
|
<View style={[styles.container, { backgroundColor: themes[theme].focusedBackground }]}>
|
2020-06-15 19:35:45 +00:00
|
|
|
<FlatList
|
2022-10-21 18:27:55 +00:00
|
|
|
data={frequentlyUsed.slice(0, quantity)}
|
2020-06-15 19:35:45 +00:00
|
|
|
renderItem={renderItem}
|
|
|
|
ListFooterComponent={renderFooter}
|
2022-03-31 12:38:20 +00:00
|
|
|
style={{ backgroundColor: themes[theme].focusedBackground }}
|
2022-10-21 18:27:55 +00:00
|
|
|
keyExtractor={item => (typeof item === 'string' ? item : item.name)}
|
2020-06-15 19:35:45 +00:00
|
|
|
showsHorizontalScrollIndicator={false}
|
|
|
|
scrollEnabled={false}
|
|
|
|
horizontal
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
export default Header;
|