-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from software-mansion-labs/ideal-nav/dismiss-m…
…odal Add dismissModalWithReport
- Loading branch information
Showing
17 changed files
with
127 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,74 @@ | ||
import {getActionFromState} from '@react-navigation/core'; | ||
import type {NavigationContainerRef} from '@react-navigation/native'; | ||
import {StackActions} from '@react-navigation/native'; | ||
import {findLastIndex} from 'lodash'; | ||
import Log from '@libs/Log'; | ||
import getPolicyMemberAccountIDs from '@libs/PolicyMembersUtils'; | ||
import {doesReportBelongToWorkspace} from '@libs/ReportUtils'; | ||
import NAVIGATORS from '@src/NAVIGATORS'; | ||
import ROUTES from '@src/ROUTES'; | ||
import SCREENS from '@src/SCREENS'; | ||
import type {Report} from '@src/types/onyx'; | ||
import type {EmptyObject} from '@src/types/utils/EmptyObject'; | ||
import {isEmptyObject} from '@src/types/utils/EmptyObject'; | ||
import getPolicyIdFromState from './getPolicyIdFromState'; | ||
import getStateFromPath from './getStateFromPath'; | ||
import getTopmostReportId from './getTopmostReportId'; | ||
import linkingConfig from './linkingConfig'; | ||
import switchPolicyID from './switchPolicyID'; | ||
import type {RootStackParamList, StackNavigationAction, State} from './types'; | ||
|
||
// This function is in a separate file than Navigation.js to avoid cyclic dependency. | ||
|
||
/** | ||
* Dismisses the last modal stack if there is any | ||
* | ||
* @param targetReportID - The reportID to navigate to after dismissing the modal | ||
*/ | ||
function dismissModalWithReport(targetReport: Report | EmptyObject, navigationRef: NavigationContainerRef<RootStackParamList>) { | ||
if (!navigationRef.isReady()) { | ||
return; | ||
} | ||
|
||
const state = navigationRef.getState(); | ||
const lastRoute = state.routes.at(-1); | ||
switch (lastRoute?.name) { | ||
case NAVIGATORS.FULL_SCREEN_NAVIGATOR: | ||
case NAVIGATORS.LEFT_MODAL_NAVIGATOR: | ||
case NAVIGATORS.RIGHT_MODAL_NAVIGATOR: | ||
case SCREENS.NOT_FOUND: | ||
case SCREENS.REPORT_ATTACHMENTS: | ||
// if we are not in the target report, we need to navigate to it after dismissing the modal | ||
if (targetReport.reportID !== getTopmostReportId(state)) { | ||
const reportState = getStateFromPath(ROUTES.REPORT_WITH_ID.getRoute(targetReport.reportID)); | ||
const policyID = getPolicyIdFromState(state as State<RootStackParamList>); | ||
const policyMemberAccountIDs = getPolicyMemberAccountIDs(policyID); | ||
const shouldOpenAllWorkspace = isEmptyObject(targetReport) ? true : !doesReportBelongToWorkspace(targetReport, policyMemberAccountIDs, policyID); | ||
|
||
if (shouldOpenAllWorkspace) { | ||
switchPolicyID(navigationRef, {route: ROUTES.HOME}); | ||
} else { | ||
switchPolicyID(navigationRef, {policyID, route: ROUTES.HOME}); | ||
} | ||
|
||
const action: StackNavigationAction = getActionFromState(reportState, linkingConfig.config); | ||
if (action) { | ||
action.type = 'REPLACE'; | ||
navigationRef.dispatch(action); | ||
} | ||
// If not-found page is in the route stack, we need to close it | ||
} else if (state.routes.some((route) => route.name === SCREENS.NOT_FOUND)) { | ||
const lastRouteIndex = state.routes.length - 1; | ||
const centralRouteIndex = findLastIndex(state.routes, (route) => route.name === NAVIGATORS.CENTRAL_PANE_NAVIGATOR); | ||
navigationRef.dispatch({...StackActions.pop(lastRouteIndex - centralRouteIndex), target: state.key}); | ||
} else { | ||
navigationRef.dispatch({...StackActions.pop(), target: state.key}); | ||
} | ||
break; | ||
default: { | ||
Log.hmmm('[Navigation] dismissModalWithReport failed because there is no modal stack to dismiss'); | ||
} | ||
} | ||
} | ||
|
||
export default dismissModalWithReport; |
This file contains 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 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 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 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 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 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 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 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
Oops, something went wrong.