Skip to content

Commit

Permalink
feat: remove path param from current url
Browse files Browse the repository at this point in the history
  • Loading branch information
mironiasty committed Dec 20, 2023
1 parent 66c1784 commit d874a58
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
21 changes: 16 additions & 5 deletions packages/navigation/src/buildWebScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@ import {

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

type LinkedParams = { baseURL: string; fullPath: string };
type LinkedParams = { baseURL?: string; fullPath?: string };

function getParams(
routes: PartialRoute<Route<string, object | undefined>>[] | undefined
) {
if (routes?.[0]?.state?.routes) {
return getParams(routes[0].state.routes);
): LinkedParams | undefined {
const firstRoute = routes?.[0];
if (firstRoute) {
if (firstRoute.state?.routes) {
return getParams(firstRoute.state.routes);
}
if (firstRoute.params) {
return firstRoute.params;
} else {
// route is readonly object (in types). But we need to hack it be non-empty
// @ts-expect-error
firstRoute.params = {};
return firstRoute.params;
}
}
return routes?.[0]?.params as LinkedParams | undefined;
return undefined;
}

export function getLinkingObject(
Expand Down
24 changes: 2 additions & 22 deletions packages/navigation/src/hooks/useWebviewNavigate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,6 @@ type To<
params: ParamList[RouteName];
});

const parseQueryStringFromPath = (path: string) => {
let pathWithoutQueryString = path;
let queryString = '';
const queryStringIndex = path.indexOf('?');

if (queryStringIndex !== -1) {
pathWithoutQueryString = path.slice(0, queryStringIndex);
queryString = path.slice(queryStringIndex + 1);
}

return { pathWithoutQueryString, queryString };
};

function isNavigateAction(
action: ReturnType<typeof getActionFromState>
): action is NavigateAction<NavigationState> {
Expand Down Expand Up @@ -113,16 +100,9 @@ export function useWebviewNavigate<
if (options?.prefixes && to.match(/^https?:\/\//)) {
path = extractPathFromURL(options.prefixes, to) ?? '';
}

/* We need to send the path name as screen param
to the screen this way cause it works also for nested navigators */
const { pathWithoutQueryString, queryString } =
parseQueryStringFromPath(path);
const pathWithScreenParams = `${pathWithoutQueryString}?${queryString}&path=${pathWithoutQueryString}`;

const state = options?.getStateFromPath
? options.getStateFromPath(pathWithScreenParams, options.config)
: getStateFromPath(pathWithScreenParams, options?.config);
? options.getStateFromPath(path, options.config)
: getStateFromPath(path, options?.config);

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

0 comments on commit d874a58

Please sign in to comment.