Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/ImportSpreadsheetConfirmModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ function ImportSpreadsheetConfirmModal({isVisible, closeImportPageAndModal, onMo
return;
}
showConfirmModal({
id: 'import-spreadsheet-confirm',
title: titleText,
prompt: promptText,
confirmText: translate('common.buttonConfirm'),
Expand Down
14 changes: 6 additions & 8 deletions src/components/Modal/Global/ModalContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import noop from 'lodash/noop';
import React, {useCallback, useContext, useMemo, useRef, useState} from 'react';
import React, {useContext, useRef, useState} from 'react';
import Log from '@libs/Log';
import CONST from '@src/CONST';

Expand Down Expand Up @@ -42,7 +42,7 @@ function ModalProvider({children}: {children: React.ReactNode}) {
const modalIDRef = useRef(1);
const modalPromisesStack = useRef<Record<string, CloseModalPromiseWithResolvers>>({});

const showModal: ModalContextType['showModal'] = useCallback(({component, props, id, isCloseable = true}) => {
const showModal: ModalContextType['showModal'] = ({component, props, id, isCloseable = true}) => {
// This is a promise that will resolve when the modal is closed
let closeModalPromise: CloseModalPromiseWithResolvers | null = id ? modalPromisesStack.current?.[id] : null;

Expand All @@ -63,9 +63,9 @@ function ModalProvider({children}: {children: React.ReactNode}) {
modalPromisesStack.current[newModalId] = closeModalPromise;

return closeModalPromise.promise;
}, []);
};

const closeModal: ModalContextType['closeModal'] = useCallback((data = {action: 'CLOSE'}) => {
const closeModal: ModalContextType['closeModal'] = (data = {action: 'CLOSE'}) => {
setModalStack((prevState) => {
const lastModalId = prevState.modals.at(-1)?.id;

Expand All @@ -86,15 +86,13 @@ function ModalProvider({children}: {children: React.ReactNode}) {
modals: prevState.modals.slice(0, -1),
};
});
}, []);

const contextValue = useMemo(() => ({showModal, closeModal}), [showModal, closeModal]);
};

const modalToRender = modalStack.modals.length > 0 ? modalStack.modals.at(modalStack.modals.length - 1) : null;
const ModalComponent = modalToRender?.component;

return (
<ModalContext.Provider value={contextValue}>
<ModalContext.Provider value={{showModal, closeModal}}>
{children}
{!!ModalComponent && (
<ModalComponent
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/useConfirmModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import ConfirmModalWrapper from '@components/Modal/Global/ConfirmModalWrapper';
import type {ModalProps} from '@components/Modal/Global/ModalContext';
import {useModal} from '@components/Modal/Global/ModalContext';

type ConfirmModalOptions = Omit<React.ComponentProps<typeof ConfirmModalWrapper>, keyof ModalProps> & {id?: string};
type ConfirmModalOptions = Omit<React.ComponentProps<typeof ConfirmModalWrapper>, keyof ModalProps>;

const useConfirmModal = () => {
const context = useModal();

const showConfirmModal = ({id, ...options}: ConfirmModalOptions) => {
const showConfirmModal = (options: ConfirmModalOptions) => {
return context.showModal({
component: ConfirmModalWrapper,
id,
props: {
shouldHandleNavigationBack: true,
...options,
Expand Down
Loading