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

95 lines
2.2 KiB
TypeScript

import { useEffect, useRef, useState } from 'react';
import { Modal } from './Modal';
import { useStore } from '../lib/store';
import { dialogStore, resolveConfirm, resolvePrompt } from '../lib/dialog';
import { t } from '../i18n';
// Renders the active custom dialog (confirm/prompt). Mounted once at the app
// root; see lib/dialog.ts for the confirmDialog/promptDialog helpers.
export function DialogHost() {
const { current } = useStore(dialogStore);
const [text, setText] = useState('');
const inputRef = useRef<HTMLInputElement>(null);
// Seed the prompt field with its initial value and focus it each time a
// new prompt opens.
useEffect(() => {
if (current && current.kind === 'prompt') {
setText(current.initial || '');
// Focus after the modal mounts.
setTimeout(() => inputRef.current?.focus(), 0);
}
}, [current]);
if (!current) return null;
if (current.kind === 'confirm') {
return (
<Modal
title={current.title}
onClose={() => resolveConfirm(false)}
footer={
<>
<button
className="btn btn-ghost"
onClick={() => resolveConfirm(false)}
>
{current.cancelLabel || t('Cancel')}
</button>
<button
className={
'btn ' +
(current.danger ? 'btn-danger' : 'btn-accent')
}
onClick={() => resolveConfirm(true)}
>
{current.confirmLabel || t('Confirm')}
</button>
</>
}
>
<p>{current.message}</p>
</Modal>
);
}
return (
<Modal
title={current.title}
onClose={() => resolvePrompt(null)}
footer={
<>
<button
className="btn btn-ghost"
onClick={() => resolvePrompt(null)}
>
{current.cancelLabel || t('Cancel')}
</button>
<button
className="btn btn-accent"
onClick={() => resolvePrompt(text.trim() || null)}
>
{current.confirmLabel || t('OK')}
</button>
</>
}
>
<form
onSubmit={(e) => {
e.preventDefault();
resolvePrompt(text.trim() || null);
}}
>
{current.message && <p>{current.message}</p>}
<input
ref={inputRef}
value={text}
onChange={(e) => setText(e.target.value)}
placeholder={current.placeholder}
autoComplete="off"
/>
</form>
</Modal>
);
}