one
Version:
One is a React framework for web, iOS, and Android apps.
99 lines (96 loc) • 3.36 kB
JavaScript
import { useNavigation as useNavigation$1 } from "@react-navigation/native";
import React from "react";
import { getReactNavigationRouteName } from "../getReactNavigationConfig.native.js";
import { getContextKey, getNameFromFilePath } from "./matchers.native.js";
import { useContextKey, useRouteNodes } from "./Route.native.js";
function useNavigation(parent) {
var navigation = useNavigation$1();
var contextKey = useContextKey();
var routeNodes = useRouteNodes();
var normalizedParent = React.useMemo(function() {
if (!parent) {
return null;
}
return resolveParentRouteName(routeNodes, contextKey, parent);
}, [
contextKey,
parent,
routeNodes
]);
if (parent && normalizedParent === void 0) {
throw new Error(`Could not find parent navigation with route "${parent}". (normalized context: ${resolveParentId(contextKey, parent)})`);
}
if (normalizedParent != null) {
if (normalizedParent === "") {
var rootNavigation = navigation;
var nextNavigation = rootNavigation.getParent();
while (nextNavigation) {
rootNavigation = nextNavigation;
nextNavigation = rootNavigation.getParent();
}
return rootNavigation;
}
var parentNavigation = navigation.getParent(normalizedParent);
if (!parentNavigation) {
throw new Error(`Could not find parent navigation with route "${parent}".` + (normalizedParent !== parent ? ` (normalized: ${normalizedParent})` : ""));
}
return parentNavigation;
}
return navigation;
}
function resolveParentId(contextKey, parentId) {
if (!parentId) {
return null;
}
if (parentId.startsWith(".")) {
return normalizeContextPath(relativePaths(contextKey, parentId));
}
return normalizeContextPath(parentId);
}
function resolveParentRouteName(routeNodes, contextKey, parentId) {
var parentContextKey = resolveParentId(contextKey, parentId);
if (parentContextKey == null) return null;
if (parentContextKey === "/") return "";
var parentRoute = routeNodes.find(function(node) {
return getContextKey(node.contextKey) === parentContextKey;
});
return parentRoute ? getReactNavigationRouteName(parentRoute) : void 0;
}
function normalizeContextPath(path) {
var normalized = getNameFromFilePath(path).replace(/^\/+|\/+$/g, "").replace(/\/_layout$/, "");
return normalized ? `/${normalized}` : "/";
}
function relativePaths(from, to) {
var fromParts = from.split("/").filter(Boolean);
var toParts = to.split("/").filter(Boolean);
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
try {
for (var _iterator = toParts[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var part = _step.value;
if (part === "..") {
if (fromParts.length === 0) {
throw new Error(`Cannot resolve path "${to}" relative to "${from}"`);
}
fromParts.pop();
} else if (part === ".") {} else {
fromParts.push(part);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return "/" + fromParts.join("/");
}
export { resolveParentId, resolveParentRouteName, useNavigation };
//# sourceMappingURL=useNavigation.native.js.map