Skip to content

Commit

Permalink
fix: use proper state for replace action
Browse files Browse the repository at this point in the history
  • Loading branch information
mironiasty committed Dec 20, 2023
1 parent d874a58 commit 75bc8cb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -608,4 +608,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 85b06d0ed137749e786ec852f3aa1ff84759e876

COCOAPODS: 1.13.0
COCOAPODS: 1.14.2
14 changes: 7 additions & 7 deletions packages/navigation/src/buildWebScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Route,
getStateFromPath,
} from '@react-navigation/native';
import { unpackState } from './utils/unpackState';

type Options = Parameters<typeof getStateFromPath>[1];

Expand All @@ -14,9 +15,6 @@ function getParams(
): LinkedParams | undefined {
const firstRoute = routes?.[0];
if (firstRoute) {
if (firstRoute.state?.routes) {
return getParams(firstRoute.state.routes);
}
if (firstRoute.params) {
return firstRoute.params;
} else {
Expand All @@ -38,10 +36,12 @@ export function getLinkingObject(
config: linking,
getStateFromPath(path: string, options?: Options) {
const state = getStateFromPath(path, options);
const params = getParams(state?.routes);
if (params) {
params.baseURL = baseURL;
params.fullPath = path;
if (state) {
const params = getParams(unpackState(state)?.routes);
if (params) {
params.baseURL = baseURL;
params.fullPath = path;
}
}
return state;
},
Expand Down
26 changes: 9 additions & 17 deletions packages/navigation/src/hooks/useWebviewNavigate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
getActionFromState,
getStateFromPath,
NavigationContainerRefContext,
StackActions,
useNavigation,
} from '@react-navigation/native';
import * as React from 'react';
import LinkingContext from '@react-navigation/native/src/LinkingContext';
Expand All @@ -13,6 +13,7 @@ import type {
NavigatorScreenParams,
} from '@react-navigation/core';
import type { Action } from 'react-native-turbo';
import { unpackState } from '../utils/unpackState';

type NavigateAction<State extends NavigationState> = {
type: 'NAVIGATE';
Expand Down Expand Up @@ -46,14 +47,9 @@ function isNavigateAction(

function getAction(
action: NavigateAction<NavigationState>,
actionType: Action | undefined,
path: string,
isModal: boolean
actionType: Action | undefined
) {
if (actionType === 'replace') {
if (isModal) {
return CommonActions.setParams({ path });
}
return StackActions.replace(action.payload.name, {
...action.payload.params,
__disable_animation: true,
Expand All @@ -77,7 +73,7 @@ function getAction(
export function useWebviewNavigate<
ParamList extends ReactNavigation.RootParamList
>() {
const navigation = React.useContext(NavigationContainerRefContext);
const navigation = useNavigation();
const linking = React.useContext(LinkingContext);

const linkTo = React.useCallback(
Expand Down Expand Up @@ -105,19 +101,15 @@ export function useWebviewNavigate<
: getStateFromPath(path, options?.config);

if (state) {
const action = getActionFromState(state, options?.config);

const actionState =
actionType === 'replace' ? unpackState(state) : state;
const action = getActionFromState(actionState, options?.config);
if (isNavigateAction(action)) {
const currentOptions = navigation.getCurrentOptions();
const isModal =
!!currentOptions &&
'presentation' in currentOptions &&
currentOptions.presentation === 'modal';

const actionToDispatch = getAction(action, actionType, path, isModal);
const actionToDispatch = getAction(action, actionType);

navigation.dispatch(actionToDispatch);
} else if (action === undefined) {
// @ts-expect-error
navigation.reset(state);
}
} else {
Expand Down
12 changes: 12 additions & 0 deletions packages/navigation/src/utils/unpackState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getStateFromPath } from '@react-navigation/core';

type ResultState = NonNullable<ReturnType<typeof getStateFromPath>>;

export function unpackState(state: ResultState) {
const nestedState = state?.routes[0]?.state;
if (nestedState) {
return unpackState(nestedState);
} else {
return state;
}
}

0 comments on commit 75bc8cb

Please sign in to comment.