useConfirm.ts 430 B

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