UNPKG

one

Version:

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

103 lines (100 loc) • 3.59 kB
import { getContextKey, matchDynamicName } from "./router/matchers.mjs"; const layoutRouteNamePrefix = "__one_layout:"; function convertDynamicRouteToReactNavigation(segment) { if (segment === "index") { return ""; } if (segment === "+not-found") { return "*not-found"; } const dynamicMatch = matchDynamicName(segment); if (dynamicMatch) { if (dynamicMatch.deep) { return "*" + dynamicMatch.name; } return `:${dynamicMatch.name}`; } return segment; } function parseRouteSegments(segments) { return segments.split("/").map(convertDynamicRouteToReactNavigation).filter(Boolean).join("/"); } function hasDescendantWithRoute(node, route) { return node.children.some((child) => child.route === route || hasDescendantWithRoute(child, route)); } function getReactNavigationRouteName(node) { if (node.children.length && hasDescendantWithRoute(node, node.route)) { return `${layoutRouteNamePrefix}${node.contextKey}`; } return node.route; } function focusedRoute(state) { return state.routes[state.index ?? state.routes.length - 1]; } function resolveInitialRouteNameFromState(contextKey, state) { const contextSegments = contextKey.split("/").filter(Boolean); let current = state; let consumedSegments = 0; while (consumedSegments < contextSegments.length) { if (!current?.routes.length) return void 0; const route = focusedRoute(current); if (!route?.state) return void 0; if (route.name.startsWith(layoutRouteNamePrefix)) { const routeContextSegments = getContextKey(route.name.slice(layoutRouteNamePrefix.length)).split("/").filter(Boolean); if (routeContextSegments.length <= consumedSegments || routeContextSegments.join("/") !== contextSegments.slice(0, routeContextSegments.length).join("/")) { return void 0; } consumedSegments = routeContextSegments.length; } else { const routeSegments = route.name.split("/").filter(Boolean); if (route.name !== contextSegments.slice(consumedSegments, consumedSegments + routeSegments.length).join("/")) { return void 0; } consumedSegments += routeSegments.length; } current = route.state; } return current?.routes.length ? focusedRoute(current)?.name : void 0; } function getReactNavigationInitialRouteName(nodes, initialRouteName) { if (!initialRouteName) return void 0; const initialRoute = nodes.find((node) => node.route === initialRouteName); return initialRoute ? getReactNavigationRouteName(initialRoute) : initialRouteName; } function convertRouteNodeToScreen(node, metaOnly) { const path = parseRouteSegments(node.route); if (!node.children.length) { if (!metaOnly) { return { path, screens: {}, _route: node }; } return path; } const screens = getReactNavigationScreensConfig(node.children, metaOnly); const screen = { path, screens, initialRouteName: getReactNavigationInitialRouteName(node.children, node.initialRouteName) }; if (!metaOnly) { screen._route = node; } return screen; } function getReactNavigationScreensConfig(nodes, metaOnly) { return Object.fromEntries(nodes.map((node) => [getReactNavigationRouteName(node), convertRouteNodeToScreen(node, metaOnly)])); } function getReactNavigationConfig(routes, metaOnly) { if (!routes) { return { screens: {} }; } return { initialRouteName: getReactNavigationInitialRouteName(routes.children, routes.initialRouteName), screens: getReactNavigationScreensConfig(routes.children, metaOnly) }; } export { getReactNavigationConfig, getReactNavigationRouteName, resolveInitialRouteNameFromState }; //# sourceMappingURL=getReactNavigationConfig.mjs.map