2022-05-27 04:59:16 +03:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
|
|
|
import { createPicker } from 'picmo';
|
2022-05-06 00:43:40 +03:00
|
|
|
|
2022-05-27 04:59:16 +03:00
|
|
|
const CUSTOM_EMOJI_URL = '/api/emoji';
|
2022-05-12 09:31:31 +03:00
|
|
|
interface Props {
|
|
|
|
// eslint-disable-next-line react/no-unused-prop-types
|
|
|
|
onEmojiSelect: (emoji: string) => void;
|
2022-09-07 03:52:02 +03:00
|
|
|
onCustomEmojiSelect: (emoji: string) => void;
|
2022-05-12 09:31:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2022-09-07 10:00:28 +03:00
|
|
|
export const EmojiPicker = (props: Props) => {
|
2022-05-27 04:59:16 +03:00
|
|
|
const [customEmoji, setCustomEmoji] = useState([]);
|
2022-09-07 03:52:02 +03:00
|
|
|
const { onEmojiSelect, onCustomEmojiSelect } = props;
|
2022-05-06 00:43:40 +03:00
|
|
|
const ref = useRef();
|
|
|
|
|
2022-05-27 04:59:16 +03:00
|
|
|
const getCustomEmoji = async () => {
|
|
|
|
try {
|
|
|
|
const response = await fetch(CUSTOM_EMOJI_URL);
|
|
|
|
const emoji = await response.json();
|
|
|
|
setCustomEmoji(emoji);
|
|
|
|
} catch (e) {
|
|
|
|
console.error('cannot fetch custom emoji', e);
|
|
|
|
}
|
|
|
|
};
|
2022-05-06 00:43:40 +03:00
|
|
|
|
2022-05-27 04:59:16 +03:00
|
|
|
// Fetch the custom emoji on component mount.
|
|
|
|
useEffect(() => {
|
|
|
|
getCustomEmoji();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Recreate the emoji picker when the custom emoji changes.
|
|
|
|
useEffect(() => {
|
2022-09-07 03:52:02 +03:00
|
|
|
const e = customEmoji.map(emoji => ({
|
|
|
|
emoji: emoji.name,
|
|
|
|
label: emoji.name,
|
|
|
|
url: emoji.url,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const picker = createPicker({
|
|
|
|
rootElement: ref.current,
|
|
|
|
custom: e,
|
|
|
|
initialCategory: 'custom',
|
|
|
|
showPreview: false,
|
|
|
|
showRecents: true,
|
|
|
|
});
|
2022-05-27 04:59:16 +03:00
|
|
|
picker.addEventListener('emoji:select', event => {
|
2022-09-07 03:52:02 +03:00
|
|
|
if (event.url) {
|
|
|
|
onCustomEmojiSelect(event);
|
|
|
|
} else {
|
|
|
|
onEmojiSelect(event);
|
|
|
|
}
|
2022-05-27 04:59:16 +03:00
|
|
|
});
|
|
|
|
}, [customEmoji]);
|
2022-05-06 00:43:40 +03:00
|
|
|
|
2022-05-09 23:44:11 +03:00
|
|
|
return <div ref={ref} />;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|