-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pick only params from route before passing them to screen #44531
Merged
mountiny
merged 18 commits into
Expensify:main
from
software-mansion-labs:fix-too-many-params-passed-to-screens
Jul 19, 2024
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c0ebcb7
Pick only params from route before passing them to screen
jnowakow 21406bd
Merge branch 'main' into fix-too-many-params-passed-to-screens
jnowakow e39cb9c
Merge branch 'main' into fix-too-many-params-passed-to-screens
jnowakow 2acd090
Add normalized configs
jnowakow d86b8d1
Merge branch 'main' into fix-too-many-params-passed-to-screens
jnowakow 158e36e
Fix prettier
jnowakow 291e8a8
Merge branch 'main' into fix-too-many-params-passed-to-screens
jnowakow 9b2cce2
Merge branch 'main' into fix-too-many-params-passed-to-screens
jnowakow 2747d3f
Merge branch 'main' into fix-too-many-params-passed-to-screens
jnowakow 8e2e70a
Merge branch 'main' into fix-too-many-params-passed-to-screens
jnowakow 8a76e71
Merge branch 'main' into fix-too-many-params-passed-to-screens
jnowakow becbfff
Fix blinking profile page
jnowakow 131405e
Merge branch 'main' into fix-too-many-params-passed-to-screens
jnowakow d3b58bc
fix linter
jnowakow 30f1106
fix prettier
jnowakow ee5cb87
Merge branch 'main' into fix-too-many-params-passed-to-screens
jnowakow 5b92b73
Merge branch 'main' into fix-too-many-params-passed-to-screens
Kicu 6c69b9e
Merge branch 'main' into fix-too-many-params-passed-to-screens
Kicu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/libs/Navigation/linkingConfig/createNormalizedConfigs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
|
||
/* eslint-disable @typescript-eslint/default-param-last */ | ||
|
||
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */ | ||
|
||
/* eslint-disable no-param-reassign */ | ||
|
||
/* eslint-disable @typescript-eslint/no-unsafe-argument */ | ||
|
||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
|
||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
/* eslint-disable @typescript-eslint/ban-types */ | ||
// THOSE FUNCTIONS ARE COPIED FROM react-navigation/core IN ORDER TO AVOID PATCHING | ||
// THAT'S THE REASON WHY ESLINT IS DISABLED | ||
import type {PathConfigMap} from '@react-navigation/native'; | ||
|
||
type ParseConfig = Record<string, (value: string) => any>; | ||
|
||
type RouteConfig = { | ||
screen: string; | ||
regex?: RegExp; | ||
path: string; | ||
pattern: string; | ||
routeNames: string[]; | ||
parse?: ParseConfig; | ||
}; | ||
|
||
type InitialRouteConfig = { | ||
initialRouteName: string; | ||
parentScreens: string[]; | ||
}; | ||
|
||
const joinPaths = (...paths: string[]): string => | ||
([] as string[]) | ||
.concat(...paths.map((p) => p.split('/'))) | ||
.filter(Boolean) | ||
.join('/'); | ||
|
||
const createConfigItem = (screen: string, routeNames: string[], pattern: string, path: string, parse?: ParseConfig): RouteConfig => { | ||
// Normalize pattern to remove any leading, trailing slashes, duplicate slashes etc. | ||
pattern = pattern.split('/').filter(Boolean).join('/'); | ||
|
||
const regex = pattern | ||
? new RegExp( | ||
`^(${pattern | ||
.split('/') | ||
.map((it) => { | ||
if (it.startsWith(':')) { | ||
return `(([^/]+\\/)${it.endsWith('?') ? '?' : ''})`; | ||
} | ||
return `${it === '*' ? '.*' : escape(it)}\\/`; | ||
}) | ||
.join('')})`, | ||
) | ||
: undefined; | ||
|
||
return { | ||
screen, | ||
regex, | ||
pattern, | ||
path, | ||
// The routeNames array is mutated, so copy it to keep the current state | ||
routeNames: [...routeNames], | ||
parse, | ||
}; | ||
}; | ||
|
||
const createNormalizedConfigs = ( | ||
screen: string, | ||
routeConfig: PathConfigMap<object>, | ||
routeNames: string[] = [], | ||
initials: InitialRouteConfig[], | ||
parentScreens: string[], | ||
parentPattern?: string, | ||
): RouteConfig[] => { | ||
const configs: RouteConfig[] = []; | ||
|
||
routeNames.push(screen); | ||
|
||
parentScreens.push(screen); | ||
|
||
// @ts-expect-error: we can't strongly typecheck this for now | ||
const config = routeConfig[screen]; | ||
|
||
if (typeof config === 'string') { | ||
// If a string is specified as the value of the key(e.g. Foo: '/path'), use it as the pattern | ||
const pattern = parentPattern ? joinPaths(parentPattern, config) : config; | ||
|
||
configs.push(createConfigItem(screen, routeNames, pattern, config)); | ||
} else if (typeof config === 'object') { | ||
let pattern: string | undefined; | ||
|
||
// if an object is specified as the value (e.g. Foo: { ... }), | ||
// it can have `path` property and | ||
// it could have `screens` prop which has nested configs | ||
if (typeof config.path === 'string') { | ||
if (config.exact && config.path === undefined) { | ||
throw new Error("A 'path' needs to be specified when specifying 'exact: true'. If you don't want this screen in the URL, specify it as empty string, e.g. `path: ''`."); | ||
} | ||
|
||
pattern = config.exact !== true ? joinPaths(parentPattern || '', config.path || '') : config.path || ''; | ||
|
||
configs.push(createConfigItem(screen, routeNames, pattern!, config.path, config.parse)); | ||
} | ||
|
||
if (config.screens) { | ||
// property `initialRouteName` without `screens` has no purpose | ||
if (config.initialRouteName) { | ||
initials.push({ | ||
initialRouteName: config.initialRouteName, | ||
parentScreens, | ||
}); | ||
} | ||
|
||
Object.keys(config.screens).forEach((nestedConfig) => { | ||
const result = createNormalizedConfigs(nestedConfig, config.screens as PathConfigMap<object>, routeNames, initials, [...parentScreens], pattern ?? parentPattern); | ||
|
||
configs.push(...result); | ||
}); | ||
} | ||
} | ||
|
||
routeNames.pop(); | ||
|
||
return configs; | ||
}; | ||
|
||
export type {RouteConfig}; | ||
export default createNormalizedConfigs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kicu @adamgrzybowski Is it suggested to use this code permanently? Why can we not use this from react-navigation? Is it not exposed or did we need to make some changes to this method? If we cannot get this merged upstream, can we find a different way so we avoid duplicating this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not exposed. We can ask somebody from react-navigation to expose it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussing more here https://expensify.slack.com/archives/C04878MDF34/p1721389874173849?thread_ts=1721350042.643069&cid=C04878MDF34