2018-01-30 19:48:26 +00:00
|
|
|
import React, { Component } from 'react';
|
2019-11-25 20:01:17 +00:00
|
|
|
import { View } from 'react-native';
|
2018-01-16 18:48:05 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ScrollableTabView from 'react-native-scrollable-tab-view';
|
2019-09-16 20:50:51 +00:00
|
|
|
import { shortnameToUnicode } from 'emoji-toolkit';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import orderBy from 'lodash/orderBy';
|
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2018-12-21 10:55:35 +00:00
|
|
|
|
2018-01-16 18:48:05 +00:00
|
|
|
import TabBar from './TabBar';
|
|
|
|
import EmojiCategory from './EmojiCategory';
|
|
|
|
import styles from './styles';
|
|
|
|
import categories from './categories';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../../lib/database';
|
2018-01-30 19:48:26 +00:00
|
|
|
import { emojisByCategory } from '../../emojis';
|
2018-05-18 17:55:08 +00:00
|
|
|
import protectedFunction from '../../lib/methods/helpers/protectedFunction';
|
2019-09-16 20:26:32 +00:00
|
|
|
import log from '../../utils/log';
|
2018-01-16 18:48:05 +00:00
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
const scrollProps = {
|
2018-02-08 14:08:50 +00:00
|
|
|
keyboardShouldPersistTaps: 'always',
|
|
|
|
keyboardDismissMode: 'none'
|
2018-01-30 19:48:26 +00:00
|
|
|
};
|
2018-01-16 18:48:05 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
class EmojiPicker extends Component {
|
2018-01-16 18:48:05 +00:00
|
|
|
static propTypes = {
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string.isRequired,
|
2019-09-16 20:26:32 +00:00
|
|
|
customEmojis: PropTypes.object,
|
2018-01-30 19:48:26 +00:00
|
|
|
onEmojiSelected: PropTypes.func,
|
2019-11-25 20:01:17 +00:00
|
|
|
tabEmojiStyle: PropTypes.object
|
2018-01-16 18:48:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
const customEmojis = Object.keys(props.customEmojis)
|
|
|
|
.filter(item => item === props.customEmojis[item].name)
|
|
|
|
.map(item => ({
|
|
|
|
content: props.customEmojis[item].name,
|
|
|
|
extension: props.customEmojis[item].extension,
|
|
|
|
isCustom: true
|
|
|
|
}));
|
2018-01-16 18:48:05 +00:00
|
|
|
this.state = {
|
|
|
|
frequentlyUsed: [],
|
2019-09-16 20:26:32 +00:00
|
|
|
customEmojis,
|
2019-11-25 20:01:17 +00:00
|
|
|
show: false,
|
|
|
|
width: null
|
2018-01-16 18:48:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
await this.updateFrequentlyUsed();
|
|
|
|
this.setState({ show: true });
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2019-11-25 20:01:17 +00:00
|
|
|
const { frequentlyUsed, show, width } = this.state;
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextState.show !== show) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-25 20:01:17 +00:00
|
|
|
if (nextState.width !== width) {
|
2018-12-21 10:55:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.frequentlyUsed, frequentlyUsed)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-02-08 14:08:50 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
onEmojiSelected = (emoji) => {
|
|
|
|
try {
|
|
|
|
const { onEmojiSelected } = this.props;
|
|
|
|
if (emoji.isCustom) {
|
|
|
|
this._addFrequentlyUsed({
|
|
|
|
content: emoji.content, extension: emoji.extension, isCustom: true
|
|
|
|
});
|
|
|
|
onEmojiSelected(`:${ emoji.content }:`);
|
|
|
|
} else {
|
|
|
|
const content = emoji;
|
|
|
|
this._addFrequentlyUsed({ content, isCustom: false });
|
|
|
|
const shortname = `:${ emoji }:`;
|
2019-09-16 20:50:51 +00:00
|
|
|
onEmojiSelected(shortnameToUnicode(shortname), shortname);
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2018-01-16 18:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-02 21:31:44 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2019-09-16 20:26:32 +00:00
|
|
|
_addFrequentlyUsed = protectedFunction(async(emoji) => {
|
|
|
|
const db = database.active;
|
|
|
|
const freqEmojiCollection = db.collections.get('frequently_used_emojis');
|
2019-10-08 12:36:15 +00:00
|
|
|
let freqEmojiRecord;
|
|
|
|
try {
|
|
|
|
freqEmojiRecord = await freqEmojiCollection.find(emoji.content);
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
await db.action(async() => {
|
2019-10-08 12:36:15 +00:00
|
|
|
if (freqEmojiRecord) {
|
2019-09-16 20:26:32 +00:00
|
|
|
await freqEmojiRecord.update((f) => {
|
|
|
|
f.count += 1;
|
|
|
|
});
|
2019-10-08 12:36:15 +00:00
|
|
|
} else {
|
|
|
|
await freqEmojiCollection.create((f) => {
|
|
|
|
f._raw = sanitizedRaw({ id: emoji.content }, freqEmojiCollection.schema);
|
|
|
|
Object.assign(f, emoji);
|
|
|
|
f.count = 1;
|
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2018-01-16 18:48:05 +00:00
|
|
|
});
|
2018-05-18 17:55:08 +00:00
|
|
|
})
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
updateFrequentlyUsed = async() => {
|
|
|
|
const db = database.active;
|
|
|
|
const frequentlyUsedRecords = await db.collections.get('frequently_used_emojis').query().fetch();
|
|
|
|
let frequentlyUsed = orderBy(frequentlyUsedRecords, ['count'], ['desc']);
|
|
|
|
frequentlyUsed = frequentlyUsed.map((item) => {
|
2018-01-16 18:48:05 +00:00
|
|
|
if (item.isCustom) {
|
2019-09-16 20:26:32 +00:00
|
|
|
return { content: item.content, extension: item.extension, isCustom: item.isCustom };
|
2018-01-16 18:48:05 +00:00
|
|
|
}
|
2019-09-16 20:50:51 +00:00
|
|
|
return shortnameToUnicode(`${ item.content }`);
|
2018-01-16 18:48:05 +00:00
|
|
|
});
|
|
|
|
this.setState({ frequentlyUsed });
|
|
|
|
}
|
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
onLayout = ({ nativeEvent: { layout: { width } } }) => this.setState({ width });
|
|
|
|
|
|
|
|
renderCategory(category, i, label) {
|
|
|
|
const { frequentlyUsed, customEmojis, width } = this.state;
|
|
|
|
const { baseUrl } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-01-16 18:48:05 +00:00
|
|
|
let emojis = [];
|
|
|
|
if (i === 0) {
|
2018-09-25 19:28:42 +00:00
|
|
|
emojis = frequentlyUsed;
|
2018-01-16 18:48:05 +00:00
|
|
|
} else if (i === 1) {
|
2018-09-25 19:28:42 +00:00
|
|
|
emojis = customEmojis;
|
2018-01-16 18:48:05 +00:00
|
|
|
} else {
|
|
|
|
emojis = emojisByCategory[category];
|
|
|
|
}
|
|
|
|
return (
|
2018-01-30 19:48:26 +00:00
|
|
|
<EmojiCategory
|
|
|
|
emojis={emojis}
|
|
|
|
onEmojiSelected={emoji => this.onEmojiSelected(emoji)}
|
|
|
|
style={styles.categoryContainer}
|
2018-09-25 19:28:42 +00:00
|
|
|
width={width}
|
|
|
|
baseUrl={baseUrl}
|
2019-11-25 20:01:17 +00:00
|
|
|
tabLabel={label}
|
2018-01-30 19:48:26 +00:00
|
|
|
/>
|
2018-01-16 18:48:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-06-27 16:16:05 +00:00
|
|
|
const { show, frequentlyUsed } = this.state;
|
2018-09-25 19:28:42 +00:00
|
|
|
const { tabEmojiStyle } = this.props;
|
|
|
|
|
|
|
|
if (!show) {
|
2018-01-30 19:48:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-01-16 18:48:05 +00:00
|
|
|
return (
|
2019-11-25 20:01:17 +00:00
|
|
|
<View onLayout={this.onLayout} style={{ flex: 1 }}>
|
|
|
|
<ScrollableTabView
|
|
|
|
renderTabBar={() => <TabBar tabEmojiStyle={tabEmojiStyle} />}
|
|
|
|
contentProps={scrollProps}
|
|
|
|
style={styles.background}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
categories.tabs.map((tab, i) => (
|
|
|
|
(i === 0 && frequentlyUsed.length === 0) ? null // when no frequentlyUsed don't show the tab
|
|
|
|
: (
|
|
|
|
this.renderCategory(tab.category, i, tab.tabLabel)
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
</ScrollableTabView>
|
|
|
|
</View>
|
2018-01-16 18:48:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
customEmojis: state.customEmojis
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(EmojiPicker);
|