UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

1 lines • 6.86 kB
{"version":3,"file":"getReactNavigationConfig.js","names":[],"sources":["esm/getReactNavigationConfig.js"],"sourcesContent":["import { getContextKey, matchDynamicName } from \"./router/matchers\";\nconst layoutRouteNamePrefix = \"__one_layout:\";\nfunction convertDynamicRouteToReactNavigation(segment) {\n if (segment === \"index\") {\n return \"\";\n }\n if (segment === \"+not-found\") {\n return \"*not-found\";\n }\n const dynamicMatch = matchDynamicName(segment);\n if (dynamicMatch) {\n if (dynamicMatch.deep) {\n return \"*\" + dynamicMatch.name;\n }\n return `:${dynamicMatch.name}`;\n }\n return segment;\n}\nfunction parseRouteSegments(segments) {\n return (\n // NOTE: When there are nested routes without layouts\n // the node.route will be something like `app/home/index`\n // this needs to be split to ensure each segment is parsed correctly.\n segments.split(\"/\").map(convertDynamicRouteToReactNavigation).filter(Boolean).join(\"/\")\n );\n}\nfunction hasDescendantWithRoute(node, route) {\n return node.children.some(\n (child) => child.route === route || hasDescendantWithRoute(child, route)\n );\n}\nfunction getReactNavigationRouteName(node) {\n if (node.children.length && hasDescendantWithRoute(node, node.route)) {\n return `${layoutRouteNamePrefix}${node.contextKey}`;\n }\n return node.route;\n}\nfunction focusedRoute(state) {\n return state.routes[state.index ?? state.routes.length - 1];\n}\nfunction resolveInitialRouteNameFromState(contextKey, state) {\n const contextSegments = contextKey.split(\"/\").filter(Boolean);\n let current = state;\n let consumedSegments = 0;\n while (consumedSegments < contextSegments.length) {\n if (!current?.routes.length) return void 0;\n const route = focusedRoute(current);\n if (!route?.state) return void 0;\n if (route.name.startsWith(layoutRouteNamePrefix)) {\n const routeContextSegments = getContextKey(\n route.name.slice(layoutRouteNamePrefix.length)\n ).split(\"/\").filter(Boolean);\n if (routeContextSegments.length <= consumedSegments || routeContextSegments.join(\"/\") !== contextSegments.slice(0, routeContextSegments.length).join(\"/\")) {\n return void 0;\n }\n consumedSegments = routeContextSegments.length;\n } else {\n const routeSegments = route.name.split(\"/\").filter(Boolean);\n if (route.name !== contextSegments.slice(consumedSegments, consumedSegments + routeSegments.length).join(\"/\")) {\n return void 0;\n }\n consumedSegments += routeSegments.length;\n }\n current = route.state;\n }\n return current?.routes.length ? focusedRoute(current)?.name : void 0;\n}\nfunction getReactNavigationInitialRouteName(nodes, initialRouteName) {\n if (!initialRouteName) return void 0;\n const initialRoute = nodes.find((node) => node.route === initialRouteName);\n return initialRoute ? getReactNavigationRouteName(initialRoute) : initialRouteName;\n}\nfunction convertRouteNodeToScreen(node, metaOnly) {\n const path = parseRouteSegments(node.route);\n if (!node.children.length) {\n if (!metaOnly) {\n return {\n path,\n screens: {},\n _route: node\n };\n }\n return path;\n }\n const screens = getReactNavigationScreensConfig(node.children, metaOnly);\n const screen = {\n path,\n screens,\n // NOTE: This is bad because it forces all Layout Routes\n // to be loaded into memory. We should move towards a system where\n // the initial route name is either loaded asynchronously in the Layout Route\n // or defined via a file system convention.\n initialRouteName: getReactNavigationInitialRouteName(\n node.children,\n node.initialRouteName\n )\n };\n if (!metaOnly) {\n screen._route = node;\n }\n return screen;\n}\nfunction getReactNavigationScreensConfig(nodes, metaOnly) {\n return Object.fromEntries(\n nodes.map(\n (node) => [\n getReactNavigationRouteName(node),\n convertRouteNodeToScreen(node, metaOnly)\n ]\n )\n );\n}\nfunction getReactNavigationConfig(routes, metaOnly) {\n if (!routes) {\n return { screens: {} };\n }\n return {\n initialRouteName: getReactNavigationInitialRouteName(\n routes.children,\n routes.initialRouteName\n ),\n screens: getReactNavigationScreensConfig(routes.children, metaOnly)\n };\n}\nexport {\n getReactNavigationConfig,\n getReactNavigationRouteName,\n resolveInitialRouteNameFromState\n};\n//# sourceMappingURL=getReactNavigationConfig.js.map\n"],"mappings":";;;AACA,MAAM,wBAAwB;AAC9B,SAAS,qCAAqC,SAAS;CACrD,IAAI,YAAY,SAAS;EACvB,OAAO;CACT;CACA,IAAI,YAAY,cAAc;EAC5B,OAAO;CACT;CACA,MAAM,eAAe,iBAAiB,OAA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