-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 #42335 from software-mansion-labs/search-v1-p2/fix…
…-search-multiple-calls [Search v1] Remove multiple API calls and clean up const
- Loading branch information
Showing
23 changed files
with
316 additions
and
187 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
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,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; |
This file was deleted.
Oops, something went wrong.
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,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
51
src/libs/Navigation/linkTo/getActionForBottomTabNavigator.ts
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,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; |
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,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; |
Oops, something went wrong.