Skip to content

Commit

Permalink
Merge pull request #42335 from software-mansion-labs/search-v1-p2/fix…
Browse files Browse the repository at this point in the history
…-search-multiple-calls

[Search v1] Remove multiple API calls and clean up const
  • Loading branch information
luacmartins authored May 27, 2024
2 parents 019c6b4 + acc7f1a commit c22945a
Show file tree
Hide file tree
Showing 23 changed files with 316 additions and 187 deletions.
2 changes: 0 additions & 2 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3501,7 +3501,6 @@ const CONST = {

NAVIGATION: {
TYPE: {
FORCED_UP: 'FORCED_UP',
UP: 'UP',
},
ACTION_TYPE: {
Expand Down Expand Up @@ -4780,7 +4779,6 @@ const CONST = {
},

SEARCH_RESULTS_PAGE_SIZE: 50,
SEARCH_BOTTOM_TAB_URL: '/Search_Bottom_Tab',

SEARCH_DATA_TYPES: {
TRANSACTION: 'transaction',
Expand Down
2 changes: 0 additions & 2 deletions src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Log from '@libs/Log';
import * as SearchUtils from '@libs/SearchUtils';
import Navigation from '@navigation/Navigation';
import EmptySearchView from '@pages/Search/EmptySearchView';
import useCustomBackHandler from '@pages/Search/useCustomBackHandler';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand All @@ -26,7 +25,6 @@ type SearchProps = {
function Search({query, policyIDs}: SearchProps) {
const {isOffline} = useNetwork();
const styles = useThemeStyles();
useCustomBackHandler();

const hash = SearchUtils.getQueryHash(query, policyIDs);
const [searchResults, searchResultsMeta] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`);
Expand Down
8 changes: 7 additions & 1 deletion src/hooks/useActiveWorkspaceFromNavigationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ function useActiveWorkspaceFromNavigationState() {
Log.warn('useActiveWorkspaceFromNavigationState should be called only from BottomTab screens');
}

return state.routes.at(-1)?.params?.policyID;
const policyID = state.routes.at(-1)?.params?.policyID;

if (!policyID) {
return undefined;
}

return policyID;
});

return activeWorkpsaceID;
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function goBack(fallbackRoute?: Route, shouldEnforceFallback = false, shouldPopT

// Allow CentralPane to use UP with fallback route if the path is not found in root navigator.
if (isCentralPaneFocused && fallbackRoute && distanceFromPathInRootNavigator === -1) {
navigate(fallbackRoute, CONST.NAVIGATION.TYPE.FORCED_UP);
navigate(fallbackRoute, CONST.NAVIGATION.TYPE.UP);
return;
}

Expand Down
30 changes: 30 additions & 0 deletions src/libs/Navigation/extrapolateStateFromParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type {InitialState} from '@react-navigation/native';

/**
* It's possible that information from params about which screen we want to open is not in the state yet.
* In that case this function will state with information from params.
* This will help us with comparing one state to another.
* e.g. making sure that we are not navigating to the same screen twice.
* @param state - react-navigation state
*/
function extrapolateStateFromParams(state: InitialState) {
let current: InitialState | undefined = state;

while (current?.routes != null) {
const topRoute: InitialState['routes'][0] = current.routes[current.index ?? 0];
const params = topRoute?.params;
if (topRoute.state != null) {
current = topRoute.state;
} else if (params != null && 'screen' in params && typeof params.screen === 'string') {
// eslint-disable-next-line @typescript-eslint/ban-types
topRoute.state = {routes: [{name: params.screen, params: 'params' in params ? (params.params as object) : undefined}]};
current = topRoute.state;
} else {
break;
}
}

return state;
}

export default extrapolateStateFromParams;
25 changes: 0 additions & 25 deletions src/libs/Navigation/getTopmostNestedRHPRoute.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/libs/Navigation/isSideModalNavigator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import NAVIGATORS from '@src/NAVIGATORS';

function isSideModalNavigator(targetNavigator?: string) {
return targetNavigator === NAVIGATORS.LEFT_MODAL_NAVIGATOR || targetNavigator === NAVIGATORS.RIGHT_MODAL_NAVIGATOR;
}

export default isSideModalNavigator;
51 changes: 51 additions & 0 deletions src/libs/Navigation/linkTo/getActionForBottomTabNavigator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type {NavigationAction, NavigationState} from '@react-navigation/native';
import type {Writable} from 'type-fest';
import type {RootStackParamList, StackNavigationAction} from '@libs/Navigation/types';
import getTopmostBottomTabRoute from '@navigation/getTopmostBottomTabRoute';
import CONST from '@src/CONST';
import type {ActionPayloadParams} from './types';

// Because we need to change the type to push, we also need to set target for this action to the bottom tab navigator.
function getActionForBottomTabNavigator(
action: StackNavigationAction,
state: NavigationState<RootStackParamList>,
policyID?: string,
shouldNavigate?: boolean,
): Writable<NavigationAction> | undefined {
const bottomTabNavigatorRoute = state.routes.at(0);

if (!bottomTabNavigatorRoute || bottomTabNavigatorRoute.state === undefined || !action || action.type !== CONST.NAVIGATION.ACTION_TYPE.NAVIGATE) {
return;
}

const params = action.payload.params as ActionPayloadParams;
let payloadParams = params.params as Record<string, string | undefined>;
const screen = params.screen;

if (!payloadParams) {
payloadParams = {policyID};
} else if (!('policyID' in payloadParams && !!payloadParams?.policyID)) {
payloadParams = {...payloadParams, policyID};
}

// Check if the current bottom tab is the same as the one we want to navigate to. If it is, we don't need to do anything.
const bottomTabCurrentTab = getTopmostBottomTabRoute(state);
const bottomTabParams = bottomTabCurrentTab?.params as Record<string, string | undefined>;

// Verify if the policyID is different than the one we are currently on. If it is, we need to navigate to the new policyID.
const isNewPolicy = bottomTabParams?.policyID !== payloadParams?.policyID;
if (bottomTabCurrentTab?.name === screen && !shouldNavigate && !isNewPolicy) {
return;
}

return {
type: CONST.NAVIGATION.ACTION_TYPE.PUSH,
payload: {
name: screen,
params: payloadParams,
},
target: bottomTabNavigatorRoute.state.key,
};
}

export default getActionForBottomTabNavigator;
42 changes: 42 additions & 0 deletions src/libs/Navigation/linkTo/getMinimalAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type {NavigationAction, NavigationState} from '@react-navigation/native';
import type {Writable} from 'type-fest';
import type {State} from '@navigation/types';
import type {ActionPayload} from './types';

/**
* Motivation for this function is described in NAVIGATION.md
*
* @param action action generated by getActionFromState
* @param state The root state
* @returns minimalAction minimal action is the action that we should dispatch
*/
function getMinimalAction(action: NavigationAction, state: NavigationState): Writable<NavigationAction> {
let currentAction: NavigationAction = action;
let currentState: State | undefined = state;
let currentTargetKey: string | undefined;

while (currentAction.payload && 'name' in currentAction.payload && currentState?.routes[currentState.index ?? -1].name === currentAction.payload.name) {
if (!currentState?.routes[currentState.index ?? -1].state) {
break;
}

currentState = currentState?.routes[currentState.index ?? -1].state;
currentTargetKey = currentState?.key;

const payload = currentAction.payload as ActionPayload;

// Creating new smaller action
currentAction = {
type: currentAction.type,
payload: {
name: payload?.params?.screen,
params: payload?.params?.params,
path: payload?.params?.path,
},
target: currentTargetKey,
};
}
return currentAction;
}

export default getMinimalAction;
Loading

0 comments on commit c22945a

Please sign in to comment.