bigbuffet-rw/app/soapbox/reducers/custom_emojis.ts

39 lines
1.4 KiB
TypeScript
Raw Normal View History

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