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

115 lines
3.0 KiB
TypeScript

import { useState } from 'react';
import { safeCall, toast } from '../lib/app';
import { renderTsMarkup } from '../lib/tsMarkup';
import { Modal } from './Modal';
import { t } from '../i18n';
// Announce composer with a live TS6-markdown/BBCode preview (item 14).
// Replaces the old prompt(). A small toolbar wraps the selection in common
// formatting tags; the preview renders exactly what subscribers will see.
const TOOLBAR: Array<{ label: string; before: string; after: string }> = [
{ label: 'B', before: '**', after: '**' },
{ label: 'I', before: '*', after: '*' },
{ label: 'U', before: '[u]', after: '[/u]' },
{ label: 'S', before: '~~', after: '~~' },
{ label: '</>', before: '`', after: '`' },
{ label: '🔗', before: '[', after: '](https://)' },
{ label: 'H', before: '# ', after: '' },
{ label: '❝', before: '> ', after: '' },
];
export function AnnounceModal({ onClose }: { onClose: () => void }) {
const [text, setText] = useState('');
const [sending, setSending] = useState(false);
let ref: HTMLTextAreaElement | null = null;
const wrap = (before: string, after: string) => {
const el = ref;
if (!el) return;
const start = el.selectionStart;
const end = el.selectionEnd;
const selected = text.slice(start, end);
const next =
text.slice(0, start) + before + selected + after + text.slice(end);
setText(next);
// Restore focus with the caret after the inserted opening tag.
queueMicrotask(() => {
el.focus();
const pos = start + before.length + selected.length;
el.setSelectionRange(pos, pos);
});
};
return (
<Modal
title={t('Compose announcement')}
onClose={onClose}
wide
footer={
<>
<button className="btn btn-ghost" onClick={onClose}>
{t('Cancel')}
</button>
<button
className="btn btn-accent"
disabled={!text.trim() || sending}
onClick={async () => {
setSending(true);
const res = await safeCall<{ message?: string }>(
'Announce',
{ text: text.trim() }
);
setSending(false);
if (res) {
toast(res.message || t('Announcement sent'));
onClose();
}
}}
>
{t('Send announcement')}
</button>
</>
}
>
<div className="announce-toolbar">
{TOOLBAR.map((b) => (
<button
key={b.label}
className="btn btn-ghost"
type="button"
onClick={() => wrap(b.before, b.after)}
>
{b.label}
</button>
))}
</div>
<div className="announce-grid">
<textarea
ref={(el) => {
ref = el;
}}
className="announce-input"
value={text}
onChange={(e) => setText(e.target.value)}
placeholder={t('Announcement to all subscribers:')}
rows={10}
/>
<div className="announce-preview">
<div className="announce-preview-label">
{t('Preview')}
</div>
<div className="ts-markup">
{text.trim() ? (
renderTsMarkup(text)
) : (
<span className="muted">
{t('Nothing to preview yet')}
</span>
)}
</div>
</div>
</div>
</Modal>
);
}