import React, { useCallback } from 'react'; import { useSelector } from './store'; import { builtinDialogButtonNo, builtinDialogButtonOk, builtinDialogButtonYes, DialogButton, DialogButtonType, DialogIcon, ModalTypeDialog } from './modal/dialog'; import { Modal } from './modal/modal'; import { useDispatch } from 'react-redux'; import { hideModal } from './store/slices/screen'; const getButtons = (button: DialogButtonType): DialogButton[] => { if (typeof button === 'object') return button; switch (button) { case 'ok': return [builtinDialogButtonOk]; case 'yesNo': return [builtinDialogButtonYes, builtinDialogButtonNo]; } }; const dialogIconPattern: Record = { error: 'bi bi-x-circle-fill text-danger', info: 'bi bi-info-circle-fill text-primary', question: 'bi bi-question-circle-fill text-primary', warning: 'bi bi-exclamation-circle-fill text-warning', }; const Dialog: React.VFC<{modal: ModalTypeDialog}> = ({modal}) => { const buttons = getButtons(modal.buttons ?? 'ok'); const dispatch = useDispatch(); const onClickButton = useCallback((i: number) => { dispatch(hideModal()); if (modal.onSelect) { modal.onSelect(i); } }, [dispatch, modal]); return (
{modal.icon &&
} {modal.title &&

{modal.title}

}

{modal.message}

{ buttons.map((b, i) => ( )) }
); }; const ModalInner = (modal: Modal) => { switch (modal.type) { case 'dialog': return ; case 'menu': return

Not Implemented

; } }; export const ModalComponent: React.VFC = () => { const shown = useSelector(state => state.screen.modalShown); const modal = useSelector(state => state.screen.modal); const dispatch = useDispatch(); if (!shown || !modal) return null; return (
dispatch(hideModal())}>
e.stopPropagation()}> { ModalInner(modal) }
); };