diff --git a/striker-ui/hooks/useConfirmDialog.tsx b/striker-ui/hooks/useConfirmDialog.tsx new file mode 100644 index 00000000..8f24f4d9 --- /dev/null +++ b/striker-ui/hooks/useConfirmDialog.tsx @@ -0,0 +1,73 @@ +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; + setConfirmDialogOpen: (value: boolean) => void; + setConfirmDialogProps: Dispatch>; + finishConfirm: (title: ReactNode, message: Message) => void; +} => { + const { + initial: { actionProceedText = '', content = '', titleText = '' } = {}, + } = args; + + const confirmDialogRef = useRef( + null, + ); + + const [confirmDialogProps, setConfirmDialogProps] = + useState({ + actionProceedText, + content, + titleText, + }); + + 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], + ); + + return { + confirmDialog, + confirmDialogRef, + setConfirmDialogOpen, + setConfirmDialogProps, + finishConfirm, + }; +}; + +export default useConfirmDialog;