useConfirm.ts 482 B

1234567891011121314151617
  1. import { ConfirmContext } from 'components/contexts/ConfirmContext';
  2. import React, { useContext } from 'react';
  3. export const useConfirm = (danger = false) => {
  4. const context = useContext(ConfirmContext);
  5. return (
  6. message: React.ReactNode,
  7. callback: () => void | Promise<unknown>
  8. ) => {
  9. context?.setDangerButton(danger);
  10. context?.setContent(message);
  11. context?.setConfirm(() => async () => {
  12. await callback();
  13. context?.cancel();
  14. });
  15. };
  16. };