Skip to content
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

Poc/split cleanup #138

Merged
merged 9 commits into from
Nov 18, 2024
6 changes: 3 additions & 3 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4531,14 +4531,14 @@ const CONST = {
SF_COORDINATES: [-122.4194, 37.7749],

NAVIGATION: {
TYPE: {
UP: 'UP',
},
ACTION_TYPE: {
REPLACE: 'REPLACE',
PUSH: 'PUSH',
NAVIGATE: 'NAVIGATE',

/** These action types are custom for RootNavigator */
SWITCH_POLICY_ID: 'SWITCH_POLICY_ID',
DISMISS_MODAL: 'DISMISS_MODAL',
},
},
TIME_PERIOD: {
Expand Down
18 changes: 10 additions & 8 deletions src/components/ActiveWorkspaceProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ function ActiveWorkspaceContextProvider({children}: ChildrenProps) {
return;
}

if (queryFromRouteParam) {
const queryJSON = SearchQueryUtils.buildSearchQueryJSON(queryFromRouteParam);
if (!queryJSON) {
setActiveWorkspaceID(undefined);
return;
}
setActiveWorkspaceID(SearchQueryUtils.getPolicyIDFromSearchQuery(queryJSON));
if (!queryFromRouteParam) {
setActiveWorkspaceID(undefined);
return;
}

setActiveWorkspaceID(undefined);
const queryJSON = SearchQueryUtils.buildSearchQueryJSON(queryFromRouteParam);

if (!queryJSON) {
setActiveWorkspaceID(undefined);
return;
}

setActiveWorkspaceID(SearchQueryUtils.getPolicyIDFromSearchQuery(queryJSON));
}, [policyIDFromRouteParam, queryFromRouteParam, setActiveWorkspaceID]);

const value = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function useModalScreenOptions(getScreenOptions?: (styles: ThemeStyles) => Stack
cardStyle: styles.navigationScreenCardStyle,
headerShown: false,
cardStyleInterpolator,
animationTypeForReplace: 'pop',
}),
[styles, cardStyleInterpolator],
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
import type {CommonActions, RouterConfigOptions, StackActionType, StackNavigationState} from '@react-navigation/native';
import {findFocusedRoute, StackActions, StackRouter} from '@react-navigation/native';
import {findFocusedRoute, StackRouter} from '@react-navigation/native';
import type {ParamListBase} from '@react-navigation/routers';
import useActiveWorkspace from '@hooks/useActiveWorkspace';
import * as Localize from '@libs/Localize';
import Log from '@libs/Log';
import getPolicyIDFromState from '@libs/Navigation/getPolicyIDFromState';
import isSideModalNavigator from '@libs/Navigation/isSideModalNavigator';
import type {RootStackParamList, State} from '@libs/Navigation/types';
import {isOnboardingFlowName} from '@libs/NavigationUtils';
import * as SearchQueryUtils from '@libs/SearchQueryUtils';
import * as Welcome from '@userActions/Welcome';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';
import * as GetStateForActionHandlers from './GetStateForActionHandlers';
import syncBrowserHistory from './syncBrowserHistory';
import type {ResponsiveStackNavigatorRouterOptions} from './types';

const MODAL_ROUTES_TO_DISMISS: string[] = [
NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR,
NAVIGATORS.LEFT_MODAL_NAVIGATOR,
NAVIGATORS.RIGHT_MODAL_NAVIGATOR,
NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR,
NAVIGATORS.FEATURE_TRANING_MODAL_NAVIGATOR,
SCREENS.NOT_FOUND,
SCREENS.ATTACHMENTS,
SCREENS.TRANSACTION_RECEIPT,
SCREENS.PROFILE_AVATAR,
SCREENS.WORKSPACE_AVATAR,
SCREENS.REPORT_AVATAR,
SCREENS.CONCIERGE,
];
import type {

Check failure on line 14 in src/libs/Navigation/AppNavigator/createCustomStackNavigator/CustomRouter.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Replace `⏎····CustomRouterActionType,⏎····CustomRouterAction,⏎····DismissModalActionType,⏎····PushActionType,⏎····SwitchPolicyIdActionType,⏎····ResponsiveStackNavigatorRouterOptions,⏎` with `CustomRouterAction,·CustomRouterActionType,·DismissModalActionType,·PushActionType,·ResponsiveStackNavigatorRouterOptions,·SwitchPolicyIdActionType`
CustomRouterActionType,
CustomRouterAction,
DismissModalActionType,
PushActionType,
SwitchPolicyIdActionType,
ResponsiveStackNavigatorRouterOptions,
} from './types';

function isSwitchPolicyIdAction(action: CustomRouterAction): action is SwitchPolicyIdActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.SWITCH_POLICY_ID;
}

function isPushAction(action: CustomRouterAction): action is PushActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.PUSH;
}

function isDismissModalAction(action: CustomRouterAction): action is DismissModalActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.DISMISS_MODAL;
}

function shouldPreventReset(state: StackNavigationState<ParamListBase>, action: CommonActions.Action | StackActionType) {
if (action.type !== CONST.NAVIGATION_ACTIONS.RESET || !action?.payload) {
Expand Down Expand Up @@ -59,71 +60,33 @@
if (isSideModalNavigator(lastRoute?.name) && isSideModalNavigator(action.payload.name)) {
return true;
}

return false;
}

type CustomRootStackActionType =
| {
type: 'SWITCH_POLICY_ID';
payload: {
policyID: string;
};
}
| {type: 'DISMISS_MODAL'};

function CustomRouter(options: ResponsiveStackNavigatorRouterOptions) {
const stackRouter = StackRouter(options);
const {setActiveWorkspaceID} = useActiveWorkspace();

// @TODO: Make sure that everything works fine without compareAndAdaptState function. Probably with getMatchingFullScreenRoute.
return {
...stackRouter,
getStateForAction(state: StackNavigationState<ParamListBase>, action: CommonActions.Action | StackActionType | CustomRootStackActionType, configOptions: RouterConfigOptions) {
if (action.type === CONST.NAVIGATION.ACTION_TYPE.SWITCH_POLICY_ID) {
const lastRoute = state.routes.at(-1);
if (lastRoute?.name === SCREENS.SEARCH.CENTRAL_PANE) {
const currentParams = lastRoute.params as RootStackParamList[typeof SCREENS.SEARCH.CENTRAL_PANE];
const queryJSON = SearchQueryUtils.buildSearchQueryJSON(currentParams.q);
if (!queryJSON) {
return null;
}

if (action.payload.policyID) {
queryJSON.policyID = action.payload.policyID;
} else {
delete queryJSON.policyID;
}

const newAction = StackActions.push(SCREENS.SEARCH.CENTRAL_PANE, {
...currentParams,
q: SearchQueryUtils.buildSearchQueryString(queryJSON),
});

setActiveWorkspaceID(action.payload.policyID);
return stackRouter.getStateForAction(state, newAction, configOptions);
}
if (lastRoute?.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR) {
const newAction = StackActions.push(NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, {policyID: action.payload.policyID});

setActiveWorkspaceID(action.payload.policyID);
return stackRouter.getStateForAction(state, newAction, configOptions);
}

// We don't have other navigators that should handle switch policy action.
return null;
getStateForAction(state: StackNavigationState<ParamListBase>, action: CustomRouterAction, configOptions: RouterConfigOptions) {
if (isSwitchPolicyIdAction(action)) {
return GetStateForActionHandlers.handleSwitchPolicyID(state, action, configOptions, stackRouter, setActiveWorkspaceID);
}

if (action.type === 'DISMISS_MODAL') {
const lastRoute = state.routes.at(-1);
const newAction = StackActions.pop();
if (isDismissModalAction(action)) {
return GetStateForActionHandlers.handleDismissModalAction(state, action, configOptions, stackRouter);
}

if (!lastRoute?.name || !MODAL_ROUTES_TO_DISMISS.includes(lastRoute?.name)) {
Log.hmmm('[Navigation] dismissModal failed because there is no modal stack to dismiss');
return null;
if (isPushAction(action)) {
if (action.payload.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR) {
return GetStateForActionHandlers.handlePushReportAction(state, action, configOptions, stackRouter, setActiveWorkspaceID);
}

return stackRouter.getStateForAction(state, newAction, configOptions);
if (action.payload.name === SCREENS.SEARCH.CENTRAL_PANE) {
return GetStateForActionHandlers.handlePushSearchPageAction(state, action, configOptions, stackRouter, setActiveWorkspaceID);
}
}

// Don't let the user navigate back to a non-onboarding screen if they are currently on an onboarding screen and it's not finished.
Expand All @@ -132,65 +95,6 @@
return state;
}

if (action.type === 'PUSH' && action.payload.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR) {
const haveParamsPolicyID = action.payload.params && 'policyID' in action.payload.params;
let policyID;

if (haveParamsPolicyID) {
policyID = (action.payload.params as Record<string, string | undefined>)?.policyID;
setActiveWorkspaceID(policyID);
} else {
policyID = getPolicyIDFromState(state as State<RootStackParamList>);
}

const modifiedAction = {
...action,
payload: {
...action.payload,
params: {
...action.payload.params,
policyID,
},
},
};

return stackRouter.getStateForAction(state, modifiedAction, configOptions);
}

if (action.type === 'PUSH' && action.payload.name === SCREENS.SEARCH.CENTRAL_PANE) {
const currentParams = action.payload.params as RootStackParamList[typeof SCREENS.SEARCH.CENTRAL_PANE];
const queryJSON = SearchQueryUtils.buildSearchQueryJSON(currentParams.q);

if (!queryJSON) {
return null;
}

if (!queryJSON.policyID) {
const policyID = getPolicyIDFromState(state as State<RootStackParamList>);

if (policyID) {
queryJSON.policyID = policyID;
} else {
delete queryJSON.policyID;
}
} else {
setActiveWorkspaceID(queryJSON.policyID);
}

const modifiedAction = {
...action,
payload: {
...action.payload,
params: {
...action.payload.params,
q: SearchQueryUtils.buildSearchQueryString(queryJSON),
},
},
};

return stackRouter.getStateForAction(state, modifiedAction, configOptions);
}

if (shouldDismissSideModalNavigator(state, action)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this name to something else. Maybe isNavigatingToModalFromModal?

const modifiedState = {...state, routes: state.routes.slice(0, -1), index: state.index !== 0 ? state.index - 1 : 0};
return stackRouter.getStateForAction(modifiedState, action, configOptions);
Expand All @@ -202,4 +106,4 @@
}

export default CustomRouter;
export type {CustomRootStackActionType};
export type {CustomRouterActionType};
Loading
Loading