2018-01-30 19:48:26 +00:00
|
|
|
import React, { Component } from 'react';
|
2018-01-16 18:48:05 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-01-30 19:48:26 +00:00
|
|
|
import { ScrollView } from 'react-native';
|
2018-01-16 18:48:05 +00:00
|
|
|
import ScrollableTabView from 'react-native-scrollable-tab-view';
|
2019-10-07 20:56:30 +00:00
|
|
|
import { shortnameToUnicode } from 'emoji-toolkit';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2019-10-07 20:56:30 +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-10-07 20:56:30 +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-10-07 20:56:30 +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-10-07 20:56:30 +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-10-07 20:56:30 +00:00
|
|
|
customEmojis: PropTypes.object,
|
2018-01-30 19:48:26 +00:00
|
|
|
onEmojiSelected: PropTypes.func,
|
|
|
|
tabEmojiStyle: PropTypes.object,
|
|
|
|
emojisPerRow: PropTypes.number,
|
|
|
|
width: PropTypes.number
|
2018-01-16 18:48:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-10-07 20:56:30 +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-10-07 20:56:30 +00:00
|
|
|
customEmojis,
|
2018-12-21 10:55:35 +00:00
|
|
|
show: false
|
2018-01-16 18:48:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
await this.updateFrequentlyUsed();
|
|
|
|
this.setState({ show: true });
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2019-10-07 20:56:30 +00:00
|
|
|
const { frequentlyUsed, show } = this.state;
|
2018-12-21 10:55:35 +00:00
|
|
|
const { width } = this.props;
|
|
|
|
if (nextState.show !== show) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.width !== width) {
|
|
|
|
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-10-07 20:56:30 +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 }:`;
|
|
|
|
onEmojiSelected(shortnameToUnicode(shortname), shortname);
|
|
|
|
}
|
|
|
|
} 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-10-07 20:56:30 +00:00
|
|
|
_addFrequentlyUsed = protectedFunction(async(emoji) => {
|
|
|
|
const db = database.active;
|
|
|
|
const freqEmojiCollection = db.collections.get('frequently_used_emojis');
|
|
|
|
await db.action(async() => {
|
|
|
|
try {
|
|
|
|
const freqEmojiRecord = await freqEmojiCollection.find(emoji.content);
|
|
|
|
await freqEmojiRecord.update((f) => {
|
|
|
|
f.count += 1;
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
try {
|
|
|
|
await freqEmojiCollection.create((f) => {
|
|
|
|
f._raw = sanitizedRaw({ id: emoji.content }, freqEmojiCollection.schema);
|
|
|
|
Object.assign(f, emoji);
|
|
|
|
f.count = 1;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
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-10-07 20:56:30 +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-10-07 20:56:30 +00:00
|
|
|
return { content: item.content, extension: item.extension, isCustom: item.isCustom };
|
2018-01-16 18:48:05 +00:00
|
|
|
}
|
2019-10-07 20:56:30 +00:00
|
|
|
return shortnameToUnicode(`${ item.content }`);
|
2018-01-16 18:48:05 +00:00
|
|
|
});
|
|
|
|
this.setState({ frequentlyUsed });
|
|
|
|
}
|
|
|
|
|
|
|
|
renderCategory(category, i) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { frequentlyUsed, customEmojis } = this.state;
|
2019-10-07 20:56:30 +00:00
|
|
|
const {
|
|
|
|
emojisPerRow, width, 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
|
|
|
size={emojisPerRow}
|
|
|
|
width={width}
|
|
|
|
baseUrl={baseUrl}
|
2018-01-30 19:48:26 +00:00
|
|
|
/>
|
2018-01-16 18:48:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-07-15 17:24:48 +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 (
|
2018-01-30 19:48:26 +00:00
|
|
|
<ScrollableTabView
|
2018-09-25 19:28:42 +00:00
|
|
|
renderTabBar={() => <TabBar tabEmojiStyle={tabEmojiStyle} />}
|
2018-01-30 19:48:26 +00:00
|
|
|
contentProps={scrollProps}
|
2018-09-11 16:32:52 +00:00
|
|
|
style={styles.background}
|
2018-01-30 19:48:26 +00:00
|
|
|
>
|
|
|
|
{
|
|
|
|
categories.tabs.map((tab, i) => (
|
2019-07-15 17:24:48 +00:00
|
|
|
(i === 0 && frequentlyUsed.length === 0) ? null // when no frequentlyUsed don't show the tab
|
|
|
|
: (
|
|
|
|
<ScrollView
|
|
|
|
key={tab.category}
|
|
|
|
tabLabel={tab.tabLabel}
|
|
|
|
style={styles.background}
|
|
|
|
{...scrollProps}
|
|
|
|
>
|
|
|
|
{this.renderCategory(tab.category, i)}
|
|
|
|
</ScrollView>
|
|
|
|
)))
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
</ScrollableTabView>
|
2018-01-16 18:48:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 20:56:30 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
customEmojis: state.customEmojis
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(EmojiPicker);
|