Files
Echoion/EchoionWeb/frontend/src/components/EmojiManager.tsx
T
faumaray 24fecc90f3 making WEB
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-11 00:47:52 +03:00

113 lines
2.8 KiB
TypeScript

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/<name>.
export function EmojiManager() {
const { custom } = useStore(emojiStore);
const [name, setName] = useState('');
const [busy, setBusy] = useState(false);
const fileRef = useRef<HTMLInputElement>(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<string>((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 (
<div className="card">
<div className="card-head">
<h2>
{t('Custom emojis')}{' '}
<span className="muted">
{custom.length ? `· ${custom.length}` : ''}
</span>
</h2>
</div>
<div className="emoji-manage-row">
<input
value={name}
onChange={(e) => setName(e.target.value)}
placeholder={t('shortcode (a-z0-9_)')}
maxLength={32}
/>
<input ref={fileRef} type="file" accept="image/*" />
<button
className="btn btn-accent"
disabled={busy}
onClick={upload}
>
{t('Add emoji')}
</button>
</div>
<div className="emoji-manage-grid">
{custom.map((c) => (
<div key={c.name} className="emoji-manage-item">
<img
className="emoji-img-lg"
src={`/emoji/${c.name}`}
alt={c.name}
/>
<span className="mono">:{c.name}:</span>
<button
className="icon-btn danger"
title={t('Remove')}
onClick={async () => {
await safeCall('RemoveCustomEmoji', {
name: c.name,
});
toast(t('Emoji removed'));
loadCustomEmojis();
}}
>
<CloseIcon />
</button>
</div>
))}
</div>
</div>
);
}