-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check duplicate names only for immediate nested screens
- Loading branch information
Showing
3 changed files
with
99 additions
and
22 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,33 @@ | ||
import type { NavigationState, PartialState } from '@react-navigation/routers'; | ||
|
||
export default function checkDuplicateRouteNames(state: NavigationState) { | ||
const allRouteNames = new Map<string, string[]>(); | ||
const duplicates: string[][] = []; | ||
|
||
const getRouteNames = ( | ||
location: string, | ||
state: NavigationState | PartialState<NavigationState> | ||
) => { | ||
state.routeNames?.forEach((name) => { | ||
const current = allRouteNames.get(name); | ||
const currentLocation = location ? `${location} > ${name}` : name; | ||
state.routes.forEach((route: typeof state.routes[0]) => { | ||
const currentLocation = location | ||
? `${location} > ${route.name}` | ||
: route.name; | ||
|
||
if (current) { | ||
current.push(currentLocation); | ||
} else { | ||
allRouteNames.set(name, [currentLocation]); | ||
} | ||
}); | ||
route.state?.routeNames?.forEach((routeName) => { | ||
if (routeName === route.name) { | ||
duplicates.push([ | ||
currentLocation, | ||
`${currentLocation} > ${route.name}`, | ||
]); | ||
} | ||
}); | ||
|
||
state.routes.forEach((route: typeof state.routes[0]) => { | ||
if (route.state) { | ||
getRouteNames( | ||
location ? `${location} > ${route.name}` : route.name, | ||
route.state | ||
); | ||
getRouteNames(currentLocation, route.state); | ||
} | ||
}); | ||
}; | ||
|
||
getRouteNames('', state); | ||
|
||
const duplicates = Array.from(allRouteNames.entries()).filter( | ||
([_, value]) => value.length > 1 | ||
); | ||
|
||
return duplicates; | ||
} |