2022-06-12 11:15:34 -07:00
|
|
|
import { List as ImmutableList, Map as ImmutableMap, fromJS } from 'immutable';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-11-15 09:23:36 -08:00
|
|
|
import { emojis as emojiData } from 'soapbox/features/emoji/emoji-mart-data-light';
|
|
|
|
import { addCustomToPool } from 'soapbox/features/emoji/emoji-mart-search-light';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-01-10 14:01:24 -08:00
|
|
|
import { CUSTOM_EMOJIS_FETCH_SUCCESS } from '../actions/custom_emojis';
|
|
|
|
import { buildCustomEmojis } from '../features/emoji/emoji';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-12 11:15:34 -07:00
|
|
|
import type { AnyAction } from 'redux';
|
|
|
|
import type { APIEntity } from 'soapbox/types/entities';
|
|
|
|
|
2021-06-16 14:48:23 -07:00
|
|
|
const initialState = ImmutableList();
|
|
|
|
|
2021-06-16 15:39:03 -07:00
|
|
|
// Populate custom emojis for composer autosuggest
|
2022-06-12 11:15:34 -07:00
|
|
|
const autosuggestPopulate = (emojis: ImmutableList<ImmutableMap<string, string>>) => {
|
2021-06-16 15:39:03 -07:00
|
|
|
addCustomToPool(buildCustomEmojis(emojis));
|
|
|
|
};
|
|
|
|
|
2022-06-12 11:15:34 -07:00
|
|
|
const importEmojis = (customEmojis: APIEntity[]) => {
|
|
|
|
const emojis = (fromJS(customEmojis) as ImmutableList<ImmutableMap<string, string>>).filter((emoji) => {
|
2021-06-16 14:48:23 -07:00
|
|
|
// If a custom emoji has the shortcode of a Unicode emoji, skip it.
|
|
|
|
// Otherwise it breaks EmojiMart.
|
2022-09-04 13:15:54 -07:00
|
|
|
// https://gitlab.com/soapbox-pub/soapbox/-/issues/610
|
2021-06-16 14:48:23 -07:00
|
|
|
const shortcode = emoji.get('shortcode', '').toLowerCase();
|
|
|
|
return !emojiData[shortcode];
|
|
|
|
});
|
2021-06-16 15:39:03 -07:00
|
|
|
|
|
|
|
autosuggestPopulate(emojis);
|
|
|
|
return emojis;
|
2021-06-16 14:48:23 -07:00
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-12 11:15:34 -07:00
|
|
|
export default function custom_emojis(state = initialState, action: AnyAction) {
|
2021-06-16 14:48:23 -07:00
|
|
|
if (action.type === CUSTOM_EMOJIS_FETCH_SUCCESS) {
|
2022-06-12 11:15:34 -07:00
|
|
|
return importEmojis(action.custom_emojis);
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|