All files / lib/providers/Modal index.tsx

0% Statements 0/7
0% Branches 0/4
0% Functions 0/4
0% Lines 0/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                                                                             
import { Modal, useModal } from '@lib/components/Modal';
import React, { createContext } from 'react';
 
interface ModalData {
  title: string;
  content: string;
  onConfirm: () => void;
}
 
const ModalContext = createContext<{
  openModal: (data: ModalData) => Promise<unknown>;
}>({
  openModal: async () => {
    // pass
  }
});
 
export const useModalContext = () => React.useContext(ModalContext);
 
export function ModalProvider({ children }: { children: React.ReactNode }) {
  const { props: modalProps, openModalWithAwait: openModal } = useModal();
 
  const value = React.useMemo(() => ({ openModal }), [openModal]);
 
  return (
    <ModalContext.Provider value={value}>
      {children}
      {modalProps.title && modalProps.content && modalProps.onConfirm && (
        <Modal
          {...modalProps}
          title={modalProps.title}
          content={modalProps.content}
          onConfirm={modalProps.onConfirm}
        />
      )}
    </ModalContext.Provider>
  );
}