2022-03-31 12:38:20 +00:00
|
|
|
import React, { useEffect, useState } 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';
|
2020-06-15 19:35:45 +00:00
|
|
|
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';
|
2022-05-07 01:06:08 +00:00
|
|
|
import { TAnyMessageModel, TFrequentlyUsedEmojiModel } from '../../definitions';
|
2022-03-31 12:38:20 +00:00
|
|
|
|
|
|
|
type TItem = TFrequentlyUsedEmojiModel | string;
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
export interface IHeader {
|
|
|
|
handleReaction: (emoji: TItem, message: TAnyMessageModel) => void;
|
2021-09-13 20:41:05 +00:00
|
|
|
server: string;
|
2022-03-31 12:38:20 +00:00
|
|
|
message: TAnyMessageModel;
|
2021-09-13 20:41:05 +00:00
|
|
|
isMasterDetail: boolean;
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
type TOnReaction = ({ emoji }: { emoji: TItem }) => void;
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface THeaderItem {
|
2022-03-31 12:38:20 +00:00
|
|
|
item: TItem;
|
|
|
|
onReaction: TOnReaction;
|
2021-09-13 20:41:05 +00:00
|
|
|
server: string;
|
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-03-31 12:38:20 +00:00
|
|
|
const keyExtractor = (item: TItem) => {
|
|
|
|
const emojiModel = item as TFrequentlyUsedEmojiModel;
|
|
|
|
return (emojiModel.id ? emojiModel.content : item) as string;
|
|
|
|
};
|
2020-06-15 19:35:45 +00:00
|
|
|
|
|
|
|
const DEFAULT_EMOJIS = ['clap', '+1', 'heart_eyes', 'grinning', 'thinking_face', 'smiley'];
|
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
const HeaderItem = ({ item, onReaction, server, theme }: THeaderItem) => {
|
|
|
|
const emojiModel = item as TFrequentlyUsedEmojiModel;
|
|
|
|
const emoji = (emojiModel.id ? emojiModel.content : item) as string;
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
testID={`message-actions-emoji-${emoji}`}
|
|
|
|
onPress={() => onReaction({ emoji: `:${emoji}:` })}
|
|
|
|
style={[styles.headerItem, { backgroundColor: themes[theme].auxiliaryBackground }]}
|
2022-08-08 21:02:08 +00:00
|
|
|
theme={theme}
|
|
|
|
>
|
2022-03-31 12:38:20 +00:00
|
|
|
{emojiModel?.isCustom ? (
|
2022-05-07 01:06:08 +00:00
|
|
|
<CustomEmoji style={styles.customEmoji} emoji={emojiModel} baseUrl={server} />
|
2022-03-31 12:38:20 +00:00
|
|
|
) : (
|
|
|
|
<Text style={styles.headerIcon}>{shortnameToUnicode(`:${emoji}:`)}</Text>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
const HeaderFooter = ({ onReaction, theme }: THeaderFooter) => (
|
2020-06-15 19:35:45 +00:00
|
|
|
<Button
|
|
|
|
testID='add-reaction'
|
|
|
|
onPress={onReaction}
|
|
|
|
style={[styles.headerItem, { backgroundColor: themes[theme].auxiliaryBackground }]}
|
2022-08-08 21:02:08 +00:00
|
|
|
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>
|
2022-03-31 12:38:20 +00:00
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
const Header = React.memo(({ handleReaction, server, message, isMasterDetail }: IHeader) => {
|
|
|
|
const [items, setItems] = useState<TItem[]>([]);
|
|
|
|
const { width, height } = useDimensions();
|
|
|
|
const { theme } = useTheme();
|
2020-06-15 19:35:45 +00:00
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
// TODO: create custom hook to re-render based on screen size
|
2021-09-13 20:41:05 +00:00
|
|
|
const setEmojis = async () => {
|
2020-06-15 19:35:45 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const freqEmojiCollection = db.get('frequently_used_emojis');
|
2022-03-31 12:38:20 +00:00
|
|
|
let freqEmojis: TItem[] = await freqEmojiCollection.query().fetch();
|
2020-06-15 19:35:45 +00:00
|
|
|
|
|
|
|
const isLandscape = width > height;
|
2021-09-13 20:41:05 +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();
|
|
|
|
}, []);
|
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
const onReaction: TOnReaction = ({ emoji }) => handleReaction(emoji, message);
|
2020-06-15 19:35:45 +00:00
|
|
|
|
2022-03-31 12:38:20 +00:00
|
|
|
const renderItem = ({ item }: { item: TItem }) => (
|
|
|
|
<HeaderItem item={item} onReaction={onReaction} server={server} theme={theme} />
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
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
|
|
|
|
|
|
|
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
|
|
|
|
data={items}
|
|
|
|
renderItem={renderItem}
|
|
|
|
ListFooterComponent={renderFooter}
|
2022-03-31 12:38:20 +00:00
|
|
|
style={{ backgroundColor: themes[theme].focusedBackground }}
|
2020-06-15 19:35:45 +00:00
|
|
|
keyExtractor={keyExtractor}
|
|
|
|
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;
|