2023-06-14 12:37:41 +03:00
|
|
|
function EmojiText({ text, emojis }) {
|
|
|
|
if (!text) return '';
|
|
|
|
if (!emojis?.length) return text;
|
|
|
|
if (text.indexOf(':') === -1) return text;
|
2023-08-08 09:04:12 +03:00
|
|
|
const regex = new RegExp(
|
|
|
|
`:(${emojis.map((e) => e.shortcode).join('|')}):`,
|
|
|
|
'g',
|
|
|
|
);
|
|
|
|
const elements = text.split(regex).map((word) => {
|
|
|
|
const emoji = emojis.find((e) => e.shortcode === word);
|
|
|
|
if (emoji) {
|
|
|
|
return (
|
2023-06-14 12:37:41 +03:00
|
|
|
<img
|
2023-08-08 09:04:12 +03:00
|
|
|
key={word}
|
|
|
|
src={emoji.url}
|
|
|
|
alt={word}
|
2023-06-14 12:37:41 +03:00
|
|
|
class="shortcode-emoji emoji"
|
|
|
|
width="12"
|
|
|
|
height="12"
|
|
|
|
loading="lazy"
|
|
|
|
decoding="async"
|
2023-08-08 09:04:12 +03:00
|
|
|
/>
|
2023-06-14 12:37:41 +03:00
|
|
|
);
|
|
|
|
}
|
2023-08-08 09:04:12 +03:00
|
|
|
return word;
|
2023-06-14 12:37:41 +03:00
|
|
|
});
|
2023-08-08 09:04:12 +03:00
|
|
|
return elements;
|
2023-06-14 12:37:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default EmojiText;
|