1 | import type { NavigationState, PartialState } from '@react-navigation/routers';
|
2 |
|
3 | export default function checkDuplicateRouteNames(state: NavigationState) {
|
4 | const duplicates: string[][] = [];
|
5 |
|
6 | const getRouteNames = (
|
7 | location: string,
|
8 | state: NavigationState | PartialState<NavigationState>
|
9 | ) => {
|
10 | state.routes.forEach((route: typeof state.routes[0]) => {
|
11 | const currentLocation = location
|
12 | ? `${location} > ${route.name}`
|
13 | : route.name;
|
14 |
|
15 | route.state?.routeNames?.forEach((routeName) => {
|
16 | if (routeName === route.name) {
|
17 | duplicates.push([
|
18 | currentLocation,
|
19 | `${currentLocation} > ${route.name}`,
|
20 | ]);
|
21 | }
|
22 | });
|
23 |
|
24 | if (route.state) {
|
25 | getRouteNames(currentLocation, route.state);
|
26 | }
|
27 | });
|
28 | };
|
29 |
|
30 | getRouteNames('', state);
|
31 |
|
32 | return duplicates;
|
33 | }
|