import { ReactNode, useEffect, useState, useRef } from 'react'; import { Button } from '@@/buttons'; import { ButtonOptions, ModalType } from './types'; import { openModal } from './open-modal'; import { Modal, OnSubmit } from './Modal'; export interface DialogOptions { title?: ReactNode; message: ReactNode; modalType?: ModalType; buttons: Array>; } interface Props extends DialogOptions { onSubmit: OnSubmit; } export function Dialog({ buttons, message, title, onSubmit, modalType, }: Props) { const ariaLabel = requireString(title) || requireString(message) || 'Dialog'; const [count, setCount] = useState(0); const countRef = useRef(count); countRef.current = count; useEffect(() => { let retFn; // only countdown the first button with non-zero timeout for (let i = 0; i < buttons.length; i++) { const button = buttons[i]; if (button.timeout) { setCount(button.timeout as number); const intervalID = setInterval(() => { const count = countRef.current; setCount(count - 1); if (count === 1) { onSubmit(button.value); } }, 1000); retFn = () => clearInterval(intervalID); break; } } return retFn; }, [buttons, onSubmit]); return ( onSubmit()} aria-label={ariaLabel}> {title && } {message} {buttons.map((button, index) => ( ))} ); } function requireString(value: ReactNode) { return typeof value === 'string' ? value : undefined; } export async function openDialog(options: DialogOptions) { return openModal, T>(Dialog, options); }