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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function handleQueryWithPolicyID(query: SearchQueryString, activePolicyID?: stri
return query;
}

const policyID = queryJSON.policyID ?? activePolicyID;
const policyID = activePolicyID ?? queryJSON.policyID;
const policy = PolicyUtils.getPolicy(policyID);

// In case policy is missing or there is no policy currently selected via WorkspaceSwitcher we remove it
Expand Down
19 changes: 13 additions & 6 deletions src/libs/Navigation/getPolicyIDFromState.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import SCREENS from '@src/SCREENS';
import extractPolicyIDFromQuery from './extractPolicyIDFromQuery';
import getTopmostBottomTabRoute from './getTopmostBottomTabRoute';
import getTopmostCentralPaneRoute from './getTopmostCentralPaneRoute';
import type {RootStackParamList, State} from './types';

/**
* returns policyID value if one exists in navigation state
*
* PolicyID in this app can be stored in two ways:
* - on most screens but NOT Search as `policyID` param
* - on Search related screens as policyID filter inside `q` (SearchQuery) param
* - on most screens but NOT Search as `policyID` param (on bottom tab screens)
* - on Search related screens as policyID filter inside `q` (SearchQuery) param (only for SEARCH_CENTRAL_PANE)
*/
const getPolicyIDFromState = (state: State<RootStackParamList>): string | undefined => {
const topmostBottomTabRoute = getTopmostBottomTabRoute(state);

const policyID = topmostBottomTabRoute && topmostBottomTabRoute.params && 'policyID' in topmostBottomTabRoute.params && topmostBottomTabRoute.params?.policyID;
if (policyID) {
return topmostBottomTabRoute.params?.policyID as string;
if (!topmostBottomTabRoute) {
return;
}

if (topmostBottomTabRoute.name === SCREENS.SEARCH.BOTTOM_TAB) {
const topmostCentralPaneRoute = getTopmostCentralPaneRoute(state);
return extractPolicyIDFromQuery(topmostCentralPaneRoute);
}

return extractPolicyIDFromQuery(topmostBottomTabRoute);
const policyID = topmostBottomTabRoute && topmostBottomTabRoute.params && 'policyID' in topmostBottomTabRoute.params && topmostBottomTabRoute.params?.policyID;
return policyID ? (topmostBottomTabRoute.params?.policyID as string) : undefined;
};

export default getPolicyIDFromState;
18 changes: 8 additions & 10 deletions src/libs/Navigation/switchPolicyID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ function getActionForBottomTabNavigator(action: StackNavigationAction, state: Na

if (name === SCREENS.SEARCH.CENTRAL_PANE) {
name = SCREENS.SEARCH.BOTTOM_TAB;
}

if (!params) {
} else if (!params) {
params = {policyID};
} else {
params.policyID = policyID;
Expand Down Expand Up @@ -109,19 +107,19 @@ export default function switchPolicyID(navigation: NavigationContainerRef<RootSt
// If the layout is wide we need to push matching central pane route to the stack.
if (shouldAddToCentralPane) {
const params: CentralPaneRouteParams = {...topmostCentralPaneRoute?.params};

if (isOpeningSearchFromBottomTab && params.q) {
delete params.policyID;
const queryJSON = SearchUtils.buildSearchQueryJSON(params.q);

if (policyID) {
const queryJSON = SearchUtils.buildSearchQueryJSON(params.q);
if (queryJSON) {
queryJSON.policyID = policyID;
params.q = SearchUtils.buildSearchQueryString(queryJSON);
}
} else {
const queryJSON = SearchUtils.buildSearchQueryJSON(params.q);
if (queryJSON) {
delete queryJSON.policyID;
params.q = SearchUtils.buildSearchQueryString(queryJSON);
}
} else if (queryJSON) {
delete queryJSON.policyID;
params.q = SearchUtils.buildSearchQueryString(queryJSON);
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ type BackToAndForwardToParms = {
forwardTo?: Routes;
};

type SearchNavigatorParamList = {
[SCREENS.SEARCH.BOTTOM_TAB]: undefined;
[SCREENS.SEARCH.CENTRAL_PANE]: undefined;
[SCREENS.SEARCH.REPORT_RHP]: undefined;
};

type SettingsNavigatorParamList = {
[SCREENS.SETTINGS.SHARE_CODE]: undefined;
[SCREENS.SETTINGS.PROFILE.ROOT]: undefined;
Expand Down Expand Up @@ -1248,7 +1242,7 @@ type ExplanationModalNavigatorParamList = {

type BottomTabNavigatorParamList = {
[SCREENS.HOME]: {policyID?: string};
[SCREENS.SEARCH.BOTTOM_TAB]: CentralPaneScreensParamList[typeof SCREENS.SEARCH.CENTRAL_PANE];
[SCREENS.SEARCH.BOTTOM_TAB]: undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice change! 🚀

[SCREENS.SETTINGS.ROOT]: {policyID?: string};
};

Expand Down Expand Up @@ -1396,7 +1390,6 @@ export type {
RoomInviteNavigatorParamList,
RoomMembersNavigatorParamList,
RootStackParamList,
SearchNavigatorParamList,
SettingsNavigatorParamList,
SignInNavigatorParamList,
FeatureTrainingNavigatorParamList,
Expand Down
13 changes: 3 additions & 10 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as CurrencyUtils from './CurrencyUtils';
import DateUtils from './DateUtils';
import {translateLocal} from './Localize';
import navigationRef from './Navigation/navigationRef';
import type {AuthScreensParamList, BottomTabNavigatorParamList, RootStackParamList, State} from './Navigation/types';
import type {AuthScreensParamList, RootStackParamList, State} from './Navigation/types';
import * as searchParser from './SearchParser/searchParser';
import * as TransactionUtils from './TransactionUtils';
import * as UserUtils from './UserUtils';
Expand Down Expand Up @@ -292,16 +292,9 @@ function getSortedReportData(data: ReportListItemType[]) {
function getCurrentSearchParams() {
const rootState = navigationRef.getRootState() as State<RootStackParamList>;

const lastSearchCentralPaneRoute = rootState.routes.filter((route) => route.name === SCREENS.SEARCH.CENTRAL_PANE).at(-1);
const lastSearchBottomTabRoute = rootState.routes[0].state?.routes.filter((route) => route.name === SCREENS.SEARCH.BOTTOM_TAB).at(-1);
const lastSearchRoute = rootState.routes.filter((route) => route.name === SCREENS.SEARCH.CENTRAL_PANE).at(-1);

if (lastSearchCentralPaneRoute) {
return lastSearchCentralPaneRoute.params as AuthScreensParamList[typeof SCREENS.SEARCH.CENTRAL_PANE];
}

if (lastSearchBottomTabRoute) {
return lastSearchBottomTabRoute.params as BottomTabNavigatorParamList[typeof SCREENS.SEARCH.BOTTOM_TAB];
}
return lastSearchRoute ? (lastSearchRoute.params as AuthScreensParamList[typeof SCREENS.SEARCH.CENTRAL_PANE]) : undefined;
}

function isSearchResultsEmpty(searchResults: SearchResults) {
Expand Down