2021-03-16 08:11:31 -04:00
|
|
|
import { chunk, memoize, uniq } from 'lodash';
|
2022-02-01 16:12:02 -05:00
|
|
|
import { getCookie, setCookie } from '~/lib/utils/common_utils';
|
2021-03-11 07:09:28 -05:00
|
|
|
import { initEmojiMap, getEmojiCategoryMap } from '~/emoji';
|
2021-03-16 08:11:31 -04:00
|
|
|
import {
|
|
|
|
EMOJIS_PER_ROW,
|
|
|
|
EMOJI_ROW_HEIGHT,
|
|
|
|
CATEGORY_ROW_HEIGHT,
|
|
|
|
FREQUENTLY_USED_KEY,
|
|
|
|
FREQUENTLY_USED_COOKIE_KEY,
|
|
|
|
} from '../constants';
|
2021-03-11 07:09:28 -05:00
|
|
|
|
|
|
|
export const generateCategoryHeight = (emojisLength) =>
|
|
|
|
emojisLength * EMOJI_ROW_HEIGHT + CATEGORY_ROW_HEIGHT;
|
|
|
|
|
2021-03-16 08:11:31 -04:00
|
|
|
export const getFrequentlyUsedEmojis = () => {
|
2022-02-01 16:12:02 -05:00
|
|
|
const savedEmojis = getCookie(FREQUENTLY_USED_COOKIE_KEY);
|
2021-03-16 08:11:31 -04:00
|
|
|
|
|
|
|
if (!savedEmojis) return null;
|
|
|
|
|
|
|
|
const emojis = chunk(uniq(savedEmojis.split(',')), 9);
|
|
|
|
|
|
|
|
return {
|
|
|
|
frequently_used: {
|
|
|
|
emojis,
|
|
|
|
top: 0,
|
|
|
|
height: generateCategoryHeight(emojis.length),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const addToFrequentlyUsed = (emoji) => {
|
|
|
|
const frequentlyUsedEmojis = uniq(
|
2022-02-01 16:12:02 -05:00
|
|
|
(getCookie(FREQUENTLY_USED_COOKIE_KEY) || '')
|
2021-03-16 08:11:31 -04:00
|
|
|
.split(',')
|
|
|
|
.filter((e) => e)
|
|
|
|
.concat(emoji),
|
|
|
|
);
|
|
|
|
|
2022-02-01 16:12:02 -05:00
|
|
|
setCookie(FREQUENTLY_USED_COOKIE_KEY, frequentlyUsedEmojis.join(','));
|
2021-03-16 08:11:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const hasFrequentlyUsedEmojis = () => getFrequentlyUsedEmojis() !== null;
|
|
|
|
|
2021-03-11 07:09:28 -05:00
|
|
|
export const getEmojiCategories = memoize(async () => {
|
|
|
|
await initEmojiMap();
|
|
|
|
|
|
|
|
const categories = await getEmojiCategoryMap();
|
2021-03-16 08:11:31 -04:00
|
|
|
const frequentlyUsedEmojis = getFrequentlyUsedEmojis();
|
|
|
|
let top = frequentlyUsedEmojis
|
|
|
|
? frequentlyUsedEmojis.frequently_used.top + frequentlyUsedEmojis.frequently_used.height
|
|
|
|
: 0;
|
2021-03-11 07:09:28 -05:00
|
|
|
|
|
|
|
return Object.freeze(
|
2021-03-16 08:11:31 -04:00
|
|
|
Object.keys(categories)
|
|
|
|
.filter((c) => c !== FREQUENTLY_USED_KEY)
|
|
|
|
.reduce((acc, category) => {
|
|
|
|
const emojis = chunk(categories[category], EMOJIS_PER_ROW);
|
|
|
|
const height = generateCategoryHeight(emojis.length);
|
|
|
|
const newAcc = {
|
|
|
|
...acc,
|
|
|
|
[category]: { emojis, height, top },
|
|
|
|
};
|
|
|
|
top += height;
|
|
|
|
|
|
|
|
return newAcc;
|
|
|
|
}, frequentlyUsedEmojis || {}),
|
2021-03-11 07:09:28 -05:00
|
|
|
);
|
|
|
|
});
|