owncast/web/components/chat/ChatTextField/EmojiPicker.tsx

42 lines
1.2 KiB
TypeScript
Raw Normal View History

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;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default function EmojiPicker(props: Props) {
2022-05-27 04:59:16 +03:00
const [customEmoji, setCustomEmoji] = useState([]);
const { onEmojiSelect } = 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(() => {
const picker = createPicker({ rootElement: ref.current, custom: customEmoji });
picker.addEventListener('emoji:select', event => {
console.log('Emoji selected:', event.emoji);
onEmojiSelect(event);
});
}, [customEmoji]);
2022-05-06 00:43:40 +03:00
return <div ref={ref} />;
2022-05-06 00:43:40 +03:00
}