From 53fe13e23824b860da3c6c6975e4e4e93bcb0f33 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Tue, 8 Oct 2024 12:17:37 +0200 Subject: [PATCH] Cleanup linkTo and getMinimalAction --- src/libs/Navigation/Navigation.ts | 1 + .../Navigation/linkTo/getMinimalAction.ts | 9 +++- src/libs/Navigation/linkTo/index.ts | 2 +- .../Navigation/newLinkTo/getMinimalAction.ts | 47 ------------------- 4 files changed, 9 insertions(+), 50 deletions(-) delete mode 100644 src/libs/Navigation/newLinkTo/getMinimalAction.ts diff --git a/src/libs/Navigation/Navigation.ts b/src/libs/Navigation/Navigation.ts index c250f467a697..92b739ff8879 100644 --- a/src/libs/Navigation/Navigation.ts +++ b/src/libs/Navigation/Navigation.ts @@ -31,6 +31,7 @@ import getMinimalAction from './linkTo/getMinimalAction'; import navigationRef from './navigationRef'; import setNavigationActionToMicrotaskQueue from './setNavigationActionToMicrotaskQueue'; import type {NavigationPartialRoute, NavigationStateRoute, RootStackParamList, SplitNavigatorLHNScreen, SplitNavigatorParamListType, State, StateOrRoute} from './types'; +import createSplitNavigator from './linkingConfig/createSplitNavigator'; const SPLIT_NAVIGATOR_TO_SIDEBAR_MAP: Record = { [NAVIGATORS.REPORTS_SPLIT_NAVIGATOR]: SCREENS.HOME, diff --git a/src/libs/Navigation/linkTo/getMinimalAction.ts b/src/libs/Navigation/linkTo/getMinimalAction.ts index ff01b3b8333b..9eab2f6f8717 100644 --- a/src/libs/Navigation/linkTo/getMinimalAction.ts +++ b/src/libs/Navigation/linkTo/getMinimalAction.ts @@ -3,6 +3,11 @@ import type {Writable} from 'type-fest'; import type {State} from '@navigation/types'; import type {ActionPayload} from './types'; +type MinimalAction = { + action: Writable; + targetState: State | undefined; +}; + /** * Motivation for this function is described in NAVIGATION.md * @@ -10,7 +15,7 @@ import type {ActionPayload} from './types'; * @param state The root state * @returns minimalAction minimal action is the action that we should dispatch */ -function getMinimalAction(action: NavigationAction, state: NavigationState): Writable { +function getMinimalAction(action: NavigationAction, state: NavigationState): MinimalAction { let currentAction: NavigationAction = action; let currentState: State | undefined = state; let currentTargetKey: string | undefined; @@ -36,7 +41,7 @@ function getMinimalAction(action: NavigationAction, state: NavigationState): Wri target: currentTargetKey, }; } - return currentAction; + return {action: currentAction, targetState: currentState}; } export default getMinimalAction; diff --git a/src/libs/Navigation/linkTo/index.ts b/src/libs/Navigation/linkTo/index.ts index f8ac74d6e61f..40d51cd6ec9f 100644 --- a/src/libs/Navigation/linkTo/index.ts +++ b/src/libs/Navigation/linkTo/index.ts @@ -56,6 +56,6 @@ export default function linkTo(navigation: NavigationContainerRef; - targetState: State | undefined; -}; - -/** - * 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): MinimalAction { - 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 {action: currentAction, targetState: currentState}; -} - -export default getMinimalAction;