import { Dispatch, MutableRefObject, ReactElement, ReactNode, SetStateAction, useCallback, useMemo, useRef, useState, } from 'react'; import ConfirmDialog from '../components/ConfirmDialog'; import MessageBox from '../components/MessageBox'; const useConfirmDialog = ( args: { initial?: Partial; } = {}, ): { confirmDialog: ReactElement; confirmDialogRef: MutableRefObject; setConfirmDialogLoading: (value: boolean) => void; setConfirmDialogOpen: (value: boolean) => void; setConfirmDialogProps: Dispatch>; finishConfirm: (title: ReactNode, message: Message) => void; } => { const { initial: { actionProceedText = '', content = '', titleText = '', ...restInitialProps } = {}, } = args; const confirmDialogRef = useRef( null, ); const [confirmDialogProps, setConfirmDialogProps] = useState({ actionProceedText, content, titleText, }); const setConfirmDialogLoading = useCallback( (value: boolean) => setConfirmDialogProps(({ loading, ...rest }) => ({ ...rest, loading: value, })), [], ); const setConfirmDialogOpen = useCallback( (value: boolean) => confirmDialogRef?.current?.setOpen?.call(null, value), [], ); const finishConfirm = useCallback( (title: ReactNode, message: Message) => setConfirmDialogProps({ actionProceedText: '', content: , showActionArea: false, showClose: true, titleText: title, }), [], ); const confirmDialog = useMemo( () => ( ), [confirmDialogProps, restInitialProps], ); return { confirmDialog, confirmDialogRef, setConfirmDialogLoading, setConfirmDialogOpen, setConfirmDialogProps, finishConfirm, }; }; export default useConfirmDialog;