Local modifications to ClusterLabs/Anvil by Alteeve
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

95 lines
2.1 KiB

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<ConfirmDialogProps>;
} = {},
): {
confirmDialog: ReactElement;
confirmDialogRef: MutableRefObject<ConfirmDialogForwardedRefContent | null>;
setConfirmDialogLoading: (value: boolean) => void;
setConfirmDialogOpen: (value: boolean) => void;
setConfirmDialogProps: Dispatch<SetStateAction<ConfirmDialogProps>>;
finishConfirm: (title: ReactNode, message: Message) => void;
} => {
const {
initial: {
actionProceedText = '',
content = '',
titleText = '',
...restInitialProps
} = {},
} = args;
const confirmDialogRef = useRef<ConfirmDialogForwardedRefContent | null>(
null,
);
const [confirmDialogProps, setConfirmDialogProps] =
useState<ConfirmDialogProps>({
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: <MessageBox {...message} />,
showActionArea: false,
showClose: true,
titleText: title,
}),
[],
);
const confirmDialog = useMemo<ReactElement>(
() => (
<ConfirmDialog
{...restInitialProps}
{...confirmDialogProps}
ref={confirmDialogRef}
/>
),
[confirmDialogProps, restInitialProps],
);
return {
confirmDialog,
confirmDialogRef,
setConfirmDialogLoading,
setConfirmDialogOpen,
setConfirmDialogProps,
finishConfirm,
};
};
export default useConfirmDialog;