Skip to content

Commit

Permalink
refactor: accept options in createPathConfigForStaticNavigation
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Mar 22, 2024
1 parent 3825046 commit bf6cafa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion example/src/Screens/Static.tsx
Expand Up @@ -105,7 +105,7 @@ export function StaticScreen() {
}

StaticScreen.title = 'Static config';
StaticScreen.linking = createPathConfigForStaticNavigation(RootStack);
StaticScreen.linking = createPathConfigForStaticNavigation(RootStack, {});

const styles = StyleSheet.create({
buttons: {
Expand Down
27 changes: 19 additions & 8 deletions packages/core/src/StaticNavigation.tsx
Expand Up @@ -470,6 +470,8 @@ type TreeForPathConfig = {
* Create a path config object from a static navigation config for deep linking.
*
* @param tree Static navigation config.
* @param options Additional options from `linking.config`.
* @param auto Whether to automatically generate paths for leaf screens.
* @returns Path config object to use in linking config.
*
* @example
Expand All @@ -485,13 +487,16 @@ type TreeForPathConfig = {
*/
export function createPathConfigForStaticNavigation(
tree: TreeForPathConfig,
auto?: boolean,
root?: boolean
options: {
initialRouteName?: string;
},
auto?: boolean
) {
let initialScreenConfig: PathConfig<ParamListBase> | undefined;

const createPathConfigForTree = (
t: TreeForPathConfig,
o: { initialRouteName?: string },
// If a screen is a leaf node, but inside a screen with path,
// It should not be used for initial detection
skipInitialDetection: boolean
Expand Down Expand Up @@ -537,6 +542,7 @@ export function createPathConfigForStaticNavigation(
if ('config' in item) {
screens = createPathConfigForTree(
item,
{},
skipInitialDetection || screenConfig.path != null
);
} else if (
Expand All @@ -546,12 +552,11 @@ export function createPathConfigForStaticNavigation(
) {
screens = createPathConfigForTree(
item.screen,
{},
skipInitialDetection || screenConfig.path != null
);
}

// Reset initial detection flag after exiting the nested screens

if (screens) {
screenConfig.screens = screens;
}
Expand Down Expand Up @@ -590,14 +595,20 @@ export function createPathConfigForStaticNavigation(
};

const screens = t.config.screens
? createPathConfigForScreens(t.config.screens, t.config.initialRouteName)
? createPathConfigForScreens(
t.config.screens,
o?.initialRouteName ?? t.config.initialRouteName
)
: {};

if (t.config.groups) {
Object.entries(t.config.groups).forEach(([, group]) => {
Object.assign(
screens,
createPathConfigForScreens(group.screens, t.config.initialRouteName)
createPathConfigForScreens(
group.screens,
o?.initialRouteName ?? t.config.initialRouteName
)
);
});
}
Expand All @@ -609,9 +620,9 @@ export function createPathConfigForStaticNavigation(
return screens;
};

const screens = createPathConfigForTree(tree, false);
const screens = createPathConfigForTree(tree, options, false);

if (auto && root && initialScreenConfig) {
if (auto && initialScreenConfig) {
initialScreenConfig.path = '';
}

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/__tests__/StaticNavigation.test.tsx
Expand Up @@ -337,7 +337,7 @@ it('creates linking configuration for static config', () => {
},
});

const screens = createPathConfigForStaticNavigation(Root);
const screens = createPathConfigForStaticNavigation(Root, {});

expect(screens).toMatchInlineSnapshot(`
{
Expand Down Expand Up @@ -473,7 +473,7 @@ it('returns undefined if there is no linking configuration', () => {
},
});

const screens = createPathConfigForStaticNavigation(Root);
const screens = createPathConfigForStaticNavigation(Root, {});

expect(screens).toBeUndefined();
});
Expand Down Expand Up @@ -553,7 +553,7 @@ it('automatically generates paths if auto is specified', () => {
},
});

const screens = createPathConfigForStaticNavigation(Root, true, true);
const screens = createPathConfigForStaticNavigation(Root, {}, true);

assert.ok(screens);

Expand Down Expand Up @@ -764,7 +764,7 @@ it('use initialRouteName for the automatic home screen', () => {
},
});

const screens = createPathConfigForStaticNavigation(Root, true, true);
const screens = createPathConfigForStaticNavigation(Root, {}, true);

assert.ok(screens);

Expand Down
19 changes: 4 additions & 15 deletions packages/native/src/createStaticNavigation.tsx
Expand Up @@ -54,25 +54,14 @@ export function createStaticNavigation(tree: StaticNavigation<any, any, any>) {
const screens = React.useMemo(() => {
if (tree.config.screens) {
return createPathConfigForStaticNavigation(
{
...tree,
...{
config: {
...tree.config,
initialRouteName:
linking?.config?.initialRouteName ??
typeof tree.config.initialRouteName,
screens: tree.config.screens,
},
},
},
linking?.enabled === 'auto',
true
tree,
{ initialRouteName: linking?.config?.initialRouteName },
linking?.enabled === 'auto'
);
}

return undefined;
}, [linking?.config?.initialRouteName, linking?.enabled]);
}, [linking?.config, linking?.enabled]);

if (linking?.enabled === true && screens == null) {
throw new Error(
Expand Down

0 comments on commit bf6cafa

Please sign in to comment.