import { useEffect, useRef, useState } from 'react'; import { useStore } from '../lib/store'; import { emojiStore, loadCustomEmojis } from '../lib/emoji'; import { safeCall, toast } from '../lib/app'; import { CloseIcon } from './icons'; import { t } from '../i18n'; // Admin custom-emoji manager (item 16): upload an image (≤64KB) under a // shortcode name, list existing ones, and remove them. The image is sent as // base64 to AddCustomEmoji and served back at /emoji/. export function EmojiManager() { const { custom } = useStore(emojiStore); const [name, setName] = useState(''); const [busy, setBusy] = useState(false); const fileRef = useRef(null); useEffect(() => { loadCustomEmojis(); }, []); const upload = async () => { const file = fileRef.current?.files?.[0]; const clean = name.trim().toLowerCase(); if (!/^[a-z0-9_]{1,32}$/.test(clean)) { toast(t('Emoji name must be 1-32 chars of a-z, 0-9, _')); return; } if (!file) { toast(t('Choose an image file')); return; } if (file.size > 64 * 1024) { toast(t('Emoji image too large (max ~64KB)')); return; } setBusy(true); const dataBase64 = await new Promise((resolve) => { const reader = new FileReader(); reader.onload = () => { const res = String(reader.result); resolve(res.slice(res.indexOf(',') + 1)); // strip data: prefix }; reader.readAsDataURL(file); }); const res = await safeCall('AddCustomEmoji', { name: clean, mime: file.type, dataBase64, }); setBusy(false); if (res) { toast(t('Emoji added')); setName(''); if (fileRef.current) fileRef.current.value = ''; loadCustomEmojis(); } }; return (

{t('Custom emojis')}{' '} {custom.length ? `· ${custom.length}` : ''}

setName(e.target.value)} placeholder={t('shortcode (a-z0-9_)')} maxLength={32} />
{custom.map((c) => (
{c.name} :{c.name}:
))}
); }