Repository navigation
Consolidate ConfirmModal instances into a global component #68190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
roryabraham
merged 21 commits into
Expensify:main
from
callstack-internal:ft/promise-modal
Sep 2, 2025
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a4be4c8
add promise modal
zirgulis 67d94a3
refactor export and delete modals to use promise modal
zirgulis 87030ba
fix modal cotext types
zirgulis 34454b6
fix modal action types
zirgulis b42c7e6
fix formatting
zirgulis 80439d9
simplify types
zirgulis d8bcba0
update mocks
zirgulis f8d9184
add local promise defer and immutable state
zirgulis d5b9762
memoize callbacks, update comment
zirgulis d6114dc
move new modals to /Global directory
zirgulis 3750942
fix naming and default values
zirgulis fbb18a4
fix modal state naming
zirgulis 8d01b68
rely on prevState to check for existingModal
zirgulis c6f4851
add Promise.withResolvers polyfill
zirgulis 4d550ef
remove export
zirgulis fc75100
fix naming, cleanup types
zirgulis d5182ac
notify bugbot when modal promise is not created
zirgulis 2c01a5e
use constant
zirgulis 1518b64
Merge branch 'main' of github.com:callstack-internal/Expensify-App in…
zirgulis 32b941f
Merge branch 'main' of github.com:callstack-internal/Expensify-App in…
zirgulis 7163f3b
fix import
zirgulis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import React, {useCallback} from 'react'; | ||
| import type {ConfirmModalProps} from '@components/ConfirmModal'; | ||
| import ConfirmModal from '@components/ConfirmModal'; | ||
| import type {ModalProps} from './ModalContext'; | ||
|
|
||
| type ConfirmModalWrapperProps = ModalProps & Omit<ConfirmModalProps, 'onConfirm' | 'onCancel' | 'isVisible'>; | ||
|
|
||
| // This wrapper bridges the ConfirmModal API with the global modal system, providing handlers for the onConfirm and onCancel callbacks to ConfirmModal. | ||
| // TODOS after migrating all ConfirmModal instances to use showConfirmModal: | ||
| // - handle closeModal inside ConfirmModal | ||
| // - remove ConfirmModalWrapper | ||
|
|
||
| function ConfirmModalWrapper({ | ||
| closeModal, | ||
| title = '', | ||
| prompt = '', | ||
| confirmText = '', | ||
| cancelText = '', | ||
| success = true, | ||
| danger = false, | ||
| shouldDisableConfirmButtonWhenOffline = false, | ||
| shouldShowCancelButton = true, | ||
| shouldSetModalVisibility = true, | ||
| shouldShowDismissIcon = false, | ||
| shouldCenterContent = false, | ||
| shouldStackButtons = true, | ||
| shouldReverseStackedButtons = false, | ||
| isConfirmLoading = false, | ||
| }: ConfirmModalWrapperProps) { | ||
| const handleConfirm = useCallback(() => { | ||
| closeModal({action: 'CONFIRM'}); | ||
| }, [closeModal]); | ||
|
|
||
| const handleCancel = useCallback(() => { | ||
| closeModal({action: 'CLOSE'}); | ||
| }, [closeModal]); | ||
|
|
||
| return ( | ||
| <ConfirmModal | ||
| title={title} | ||
| isVisible | ||
| onConfirm={handleConfirm} | ||
| onCancel={handleCancel} | ||
| prompt={prompt} | ||
| confirmText={confirmText} | ||
| cancelText={cancelText} | ||
| success={success} | ||
| danger={danger} | ||
| shouldDisableConfirmButtonWhenOffline={shouldDisableConfirmButtonWhenOffline} | ||
| shouldShowCancelButton={shouldShowCancelButton} | ||
| shouldSetModalVisibility={shouldSetModalVisibility} | ||
| shouldShowDismissIcon={shouldShowDismissIcon} | ||
| shouldCenterContent={shouldCenterContent} | ||
| shouldStackButtons={shouldStackButtons} | ||
| shouldReverseStackedButtons={shouldReverseStackedButtons} | ||
| isConfirmLoading={isConfirmLoading} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| ConfirmModalWrapper.displayName = 'ConfirmModalWrapper'; | ||
|
|
||
| export default ConfirmModalWrapper; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import noop from 'lodash/noop'; | ||
| import React, {useCallback, useContext, useMemo, useState} from 'react'; | ||
| import Log from '@libs/Log'; | ||
| import CONST from '@src/CONST'; | ||
|
|
||
| type ModalAction = 'CONFIRM' | 'CLOSE'; | ||
|
|
||
| type ModalStateChangePayload<A extends ModalAction = ModalAction> = {action: A}; | ||
|
|
||
| type ModalProps = { | ||
| closeModal: (param?: ModalStateChangePayload) => void; | ||
| }; | ||
|
|
||
| type ModalContextType = { | ||
| showModal<P extends ModalProps>(options: {component: React.FunctionComponent<P>; props?: Omit<P, 'closeModal'>; id?: string; isCloseable?: boolean}): Promise<ModalStateChangePayload>; | ||
| closeModal(data?: ModalStateChangePayload): void; | ||
| }; | ||
|
|
||
| const ModalContext = React.createContext<ModalContextType>({ | ||
| showModal: () => Promise.resolve({action: 'CLOSE'}), | ||
| closeModal: noop, | ||
| }); | ||
|
|
||
| const useModal = () => useContext(ModalContext); | ||
|
|
||
| let modalID = 1; | ||
|
|
||
| type ModalInfo = { | ||
| id: string; | ||
| component: React.FunctionComponent<ModalProps>; | ||
| props?: Record<string, unknown>; | ||
| promiseWithResolvers: ReturnType<typeof Promise.withResolvers<ModalStateChangePayload>>; | ||
| isCloseable: boolean; | ||
| }; | ||
|
|
||
| function PromiseModalProvider({children}: {children: React.ReactNode}) { | ||
| const [modalStack, setModalStack] = useState<{modals: ModalInfo[]}>({modals: []}); | ||
|
|
||
| const showModal = useCallback<ModalContextType['showModal']>(({component, props, id, isCloseable = true}) => { | ||
| // This is a promise that will resolve when the modal is closed | ||
| let closeModalPromise: Promise<ModalStateChangePayload> | null = null; | ||
|
|
||
| setModalStack((prevState) => { | ||
| // Check current state for existing modal | ||
| const existingModal = id ? prevState.modals.find((modal: ModalInfo) => modal.id === id) : undefined; | ||
| if (existingModal) { | ||
| // There is already a modal with this ID. Return the existing promise and don't modify state. | ||
| closeModalPromise = existingModal.promiseWithResolvers.promise; | ||
| return prevState; // No state change needed | ||
| } | ||
|
|
||
| // Create a new promise with resolvers to be resolved when the modal is closed | ||
| const promiseWithResolvers = Promise.withResolvers<ModalStateChangePayload>(); | ||
| closeModalPromise = promiseWithResolvers.promise; | ||
|
|
||
| return { | ||
| ...prevState, | ||
| modals: [...prevState.modals, {component: component as React.FunctionComponent<ModalProps>, props, promiseWithResolvers, isCloseable, id: id ?? String(modalID++)}], | ||
| }; | ||
| }); | ||
|
|
||
| // At this point, closeModalPromise should always be assigned | ||
| if (!closeModalPromise) { | ||
| Log.alert(`${CONST.ERROR.ENSURE_BUG_BOT} Failed to create modal promise. This should never happen.`); | ||
| throw new Error('Failed to create modal promise'); | ||
| } | ||
|
|
||
| return closeModalPromise; | ||
| }, []); | ||
|
|
||
| const closeModal = useCallback<ModalContextType['closeModal']>((data = {action: 'CLOSE'}) => { | ||
| setModalStack((prevState) => { | ||
| const lastModal = prevState.modals.at(-1); | ||
| lastModal?.promiseWithResolvers.resolve(data); | ||
| return { | ||
| ...prevState, | ||
| modals: prevState.modals.slice(0, -1), | ||
| }; | ||
| }); | ||
| }, []); | ||
|
|
||
| const contextValue = useMemo(() => ({showModal, closeModal}), [closeModal, showModal]); | ||
| const modalToRender = modalStack.modals.length > 0 ? modalStack.modals.at(modalStack.modals.length - 1) : null; | ||
| const ModalComponent = modalToRender?.component; | ||
|
|
||
| return ( | ||
| <ModalContext.Provider value={contextValue}> | ||
| {children} | ||
| {!!ModalComponent && ( | ||
| <ModalComponent | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...modalToRender.props} | ||
| key={modalToRender.id} | ||
| closeModal={closeModal} | ||
| /> | ||
| )} | ||
| </ModalContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export type {ModalProps}; | ||
| export {PromiseModalProvider, useModal}; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export {PromiseModalProvider, useModal} from './ModalContext'; | ||
| export {default as useModalHook} from './useModalHook'; | ||
| export {default as ConfirmModalWrapper} from './ConfirmModalWrapper'; | ||
| export type {ModalProps} from './ModalContext'; | ||
|
|
||
| export {PromiseModalProvider as ModalProvider} from './ModalContext'; | ||
| export {default as usePromiseModal} from './useModalHook'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import ConfirmModalWrapper from './ConfirmModalWrapper'; | ||
| import {useModal} from './ModalContext'; | ||
| import type {ModalProps} from './ModalContext'; | ||
|
|
||
| type ConfirmModalOptions = Omit<React.ComponentProps<typeof ConfirmModalWrapper>, keyof ModalProps>; | ||
|
|
||
| const useConfirmModal = () => { | ||
| const context = useModal(); | ||
|
|
||
| const showConfirmModal = (options: ConfirmModalOptions) => { | ||
| return context.showModal({ | ||
| component: ConfirmModalWrapper, | ||
| props: options, | ||
| }); | ||
| }; | ||
|
|
||
| return { | ||
| ...context, | ||
| showConfirmModal, | ||
| }; | ||
| }; | ||
|
|
||
| export default useConfirmModal; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.