fedibird-fe/app/javascript/mastodon/containers/reaction_picker_dropdown_container.js

121 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-07-18 15:38:44 +00:00
import { openDropdownMenu, closeDropdownMenu } from '../actions/dropdown_menu';
import { fetchRelationships } from 'mastodon/actions/accounts';
import { openModal, closeModal } from '../actions/modal';
2021-05-19 05:58:27 +00:00
import { connect } from 'react-redux';
import ReactionPickerDropdown from '../components/reaction_picker_dropdown';
2021-07-18 15:38:44 +00:00
import { isUserTouching } from '../is_mobile';
2021-05-19 05:58:27 +00:00
import { changeSetting } from '../actions/settings';
import { createSelector } from 'reselect';
import { Map as ImmutableMap } from 'immutable';
import { useEmoji } from '../actions/emojis';
const perLine = 8;
const lines = 2;
const DEFAULTS = [
'+1',
'grinning',
'kissing_heart',
'heart_eyes',
'laughing',
'stuck_out_tongue_winking_eye',
'sweat_smile',
'joy',
'yum',
'disappointed',
'thinking_face',
'weary',
'sob',
'sunglasses',
'heart',
'ok_hand',
];
const getFrequentlyUsedEmojis = createSelector([
state => state.getIn(['settings', 'frequentlyUsedEmojis'], ImmutableMap()),
], emojiCounters => {
let emojis = emojiCounters
.keySeq()
.sort((a, b) => emojiCounters.get(a) - emojiCounters.get(b))
.reverse()
.slice(0, perLine * lines)
.toArray();
if (emojis.length < DEFAULTS.length) {
let uniqueDefaults = DEFAULTS.filter(emoji => !emojis.includes(emoji));
emojis = emojis.concat(uniqueDefaults.slice(0, DEFAULTS.length - emojis.length));
}
return emojis;
});
const getCustomEmojis = createSelector([
state => state.get('custom_emojis'),
], emojis => emojis.filter(e => e.get('visible_in_picker')).sort((a, b) => {
const aShort = a.get('shortcode').toLowerCase();
const bShort = b.get('shortcode').toLowerCase();
if (aShort < bShort) {
return -1;
} else if (aShort > bShort ) {
return 1;
} else {
return 0;
}
}));
2021-07-18 15:38:44 +00:00
const getState = (dispatch) => new Promise((resolve) => {
dispatch((dispatch, getState) => {resolve(getState())})
})
2021-05-19 05:58:27 +00:00
const mapStateToProps = state => ({
custom_emojis: getCustomEmojis(state),
skinTone: state.getIn(['settings', 'skinTone']),
frequentlyUsedEmojis: getFrequentlyUsedEmojis(state),
2021-07-18 15:38:44 +00:00
dropdownPlacement: state.getIn(['dropdown_menu', 'placement']),
openDropdownId: state.getIn(['dropdown_menu', 'openId']),
openedViaKeyboard: state.getIn(['dropdown_menu', 'keyboard']),
2021-05-19 05:58:27 +00:00
});
2021-07-18 15:38:44 +00:00
const mapDispatchToProps = (dispatch, { status, onPickEmoji, scrollKey }) => ({
2021-05-19 05:58:27 +00:00
onSkinTone: skinTone => {
dispatch(changeSetting(['skinTone'], skinTone));
},
onPickEmoji: emoji => {
dispatch(useEmoji(emoji));
if (onPickEmoji) {
onPickEmoji(emoji);
}
},
2021-07-18 15:38:44 +00:00
onOpen(id, dropdownPlacement, keyboard) {
dispatch((_, getState) => {
let state = getState();
if (status) {
dispatch(fetchRelationships([status.getIn(['account', 'id'])]));
}
dispatch(isUserTouching() ? openModal('REACTION', {
status: status,
onPickEmoji: onPickEmoji,
2021-08-28 05:57:34 +00:00
onSkinTone: skinTone => {
dispatch(changeSetting(['skinTone'], skinTone));
},
2021-07-18 15:38:44 +00:00
custom_emojis: getCustomEmojis(state),
skinTone: state.getIn(['settings', 'skinTone']),
frequentlyUsedEmojis: getFrequentlyUsedEmojis(state),
}) : openDropdownMenu(id, dropdownPlacement, keyboard, scrollKey));
});
},
onClose(id) {
dispatch(closeModal('REACTION'));
dispatch(closeDropdownMenu(id));
},
2021-05-19 05:58:27 +00:00
});
export default connect(mapStateToProps, mapDispatchToProps)(ReactionPickerDropdown);