Skip to content

Commit c38906a

Browse files
WoLewickisatya164
authored andcommitted
fix: properly resolve initialRouteNames
1 parent d87857e commit c38906a

File tree

2 files changed

+147
-10
lines changed

2 files changed

+147
-10
lines changed

packages/core/src/__tests__/getStateFromPath.test.tsx

+116
Original file line numberDiff line numberDiff line change
@@ -2303,3 +2303,119 @@ it('throws if two screens map to the same pattern', () => {
23032303
})
23042304
).not.toThrow();
23052305
});
2306+
2307+
it('correctly applies initialRouteName for config with similar route names', () => {
2308+
const path = '/weekly-earnings';
2309+
2310+
const config = {
2311+
screens: {
2312+
RootTabs: {
2313+
screens: {
2314+
HomeTab: {
2315+
screens: {
2316+
Home: '',
2317+
WeeklyEarnings: 'weekly-earnings',
2318+
EventDetails: 'event-details/:eventId',
2319+
},
2320+
},
2321+
EarningsTab: {
2322+
initialRouteName: 'Earnings',
2323+
path: 'earnings',
2324+
screens: {
2325+
Earnings: '',
2326+
WeeklyEarnings: 'weekly-earnings',
2327+
},
2328+
},
2329+
},
2330+
},
2331+
},
2332+
};
2333+
2334+
const state = {
2335+
routes: [
2336+
{
2337+
name: 'RootTabs',
2338+
state: {
2339+
routes: [
2340+
{
2341+
name: 'HomeTab',
2342+
state: {
2343+
routes: [
2344+
{
2345+
name: 'WeeklyEarnings',
2346+
path,
2347+
},
2348+
],
2349+
},
2350+
},
2351+
],
2352+
},
2353+
},
2354+
],
2355+
};
2356+
2357+
expect(getStateFromPath(path, config)).toEqual(state);
2358+
expect(getStateFromPath(getPathFromState(state, config), config)).toEqual(
2359+
state
2360+
);
2361+
});
2362+
2363+
it('correctly applies initialRouteName for config with similar route names v2', () => {
2364+
const path = '/earnings/weekly-earnings';
2365+
2366+
const config = {
2367+
screens: {
2368+
RootTabs: {
2369+
screens: {
2370+
HomeTab: {
2371+
initialRouteName: 'Home',
2372+
screens: {
2373+
Home: '',
2374+
WeeklyEarnings: 'weekly-earnings',
2375+
},
2376+
},
2377+
EarningsTab: {
2378+
initialRouteName: 'Earnings',
2379+
path: 'earnings',
2380+
screens: {
2381+
Earnings: '',
2382+
WeeklyEarnings: 'weekly-earnings',
2383+
},
2384+
},
2385+
},
2386+
},
2387+
},
2388+
};
2389+
2390+
const state = {
2391+
routes: [
2392+
{
2393+
name: 'RootTabs',
2394+
state: {
2395+
routes: [
2396+
{
2397+
name: 'EarningsTab',
2398+
state: {
2399+
index: 1,
2400+
routes: [
2401+
{
2402+
name: 'Earnings',
2403+
},
2404+
{
2405+
name: 'WeeklyEarnings',
2406+
path,
2407+
},
2408+
],
2409+
},
2410+
},
2411+
],
2412+
},
2413+
},
2414+
],
2415+
};
2416+
2417+
expect(getStateFromPath(path, config)).toEqual(state);
2418+
expect(getStateFromPath(getPathFromState(state, config), config)).toEqual(
2419+
state
2420+
);
2421+
});

packages/core/src/getStateFromPath.tsx

+31-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type RouteConfig = {
2626

2727
type InitialRouteConfig = {
2828
initialRouteName: string;
29-
connectedRoutes: string[];
29+
parentScreens: string[];
3030
};
3131

3232
type ResultState = PartialState<NavigationState> & {
@@ -69,7 +69,7 @@ export default function getStateFromPath(
6969
if (options?.initialRouteName) {
7070
initialRoutes.push({
7171
initialRouteName: options.initialRouteName,
72-
connectedRoutes: Object.keys(options.screens),
72+
parentScreens: [],
7373
});
7474
}
7575

@@ -108,7 +108,8 @@ export default function getStateFromPath(
108108
key,
109109
screens as PathConfigMap,
110110
[],
111-
initialRoutes
111+
initialRoutes,
112+
[]
112113
)
113114
)
114115
)
@@ -309,12 +310,15 @@ const createNormalizedConfigs = (
309310
routeConfig: PathConfigMap,
310311
routeNames: string[] = [],
311312
initials: InitialRouteConfig[],
313+
parentScreens: string[],
312314
parentPattern?: string
313315
): RouteConfig[] => {
314316
const configs: RouteConfig[] = [];
315317

316318
routeNames.push(screen);
317319

320+
parentScreens.push(screen);
321+
318322
const config = routeConfig[screen];
319323

320324
if (typeof config === 'string') {
@@ -350,7 +354,7 @@ const createNormalizedConfigs = (
350354
if (config.initialRouteName) {
351355
initials.push({
352356
initialRouteName: config.initialRouteName,
353-
connectedRoutes: Object.keys(config.screens),
357+
parentScreens,
354358
});
355359
}
356360

@@ -360,6 +364,7 @@ const createNormalizedConfigs = (
360364
config.screens as PathConfigMap,
361365
routeNames,
362366
initials,
367+
[...parentScreens],
363368
pattern ?? parentPattern
364369
);
365370

@@ -425,13 +430,23 @@ const findParseConfigForRoute = (
425430
// Try to find an initial route connected with the one passed
426431
const findInitialRoute = (
427432
routeName: string,
433+
parentScreens: string[],
428434
initialRoutes: InitialRouteConfig[]
429435
): string | undefined => {
430436
for (const config of initialRoutes) {
431-
if (config.connectedRoutes.includes(routeName)) {
432-
return config.initialRouteName === routeName
433-
? undefined
434-
: config.initialRouteName;
437+
if (parentScreens.length === config.parentScreens.length) {
438+
let sameParents = true;
439+
for (let i = 0; i < parentScreens.length; i++) {
440+
if (parentScreens[i].localeCompare(config.parentScreens[i]) !== 0) {
441+
sameParents = false;
442+
break;
443+
}
444+
}
445+
if (sameParents) {
446+
return routeName !== config.initialRouteName
447+
? config.initialRouteName
448+
: undefined;
449+
}
435450
}
436451
}
437452
return undefined;
@@ -477,15 +492,19 @@ const createNestedStateObject = (
477492
) => {
478493
let state: InitialState;
479494
let route = routes.shift() as ParsedRoute;
480-
let initialRoute = findInitialRoute(route.name, initialRoutes);
495+
const parentScreens: string[] = [];
496+
497+
let initialRoute = findInitialRoute(route.name, parentScreens, initialRoutes);
498+
499+
parentScreens.push(route.name);
481500

482501
state = createStateObject(initialRoute, route, routes.length === 0);
483502

484503
if (routes.length > 0) {
485504
let nestedState = state;
486505

487506
while ((route = routes.shift() as ParsedRoute)) {
488-
initialRoute = findInitialRoute(route.name, initialRoutes);
507+
initialRoute = findInitialRoute(route.name, parentScreens, initialRoutes);
489508

490509
const nestedStateIndex =
491510
nestedState.index || nestedState.routes.length - 1;
@@ -500,6 +519,8 @@ const createNestedStateObject = (
500519
nestedState = nestedState.routes[nestedStateIndex]
501520
.state as InitialState;
502521
}
522+
523+
parentScreens.push(route.name);
503524
}
504525
}
505526

0 commit comments

Comments
 (0)