one
Version:
One is a new React Framework that makes Vite serve both native and web.
53 lines (52 loc) • 1.74 kB
JavaScript
import { useNavigation as useUpstreamNavigation } from "@react-navigation/native";
import React from "react";
import { getNameFromFilePath } from "./matchers.mjs";
import { useContextKey } from "./Route.mjs";
function useNavigation(parent) {
const navigation = useUpstreamNavigation();
const contextKey = useContextKey();
const normalizedParent = React.useMemo(() => {
if (!parent) {
return null;
}
const normalized = getNameFromFilePath(parent);
if (parent.startsWith(".")) {
return relativePaths(contextKey, parent);
}
return normalized;
}, [contextKey, parent]);
if (normalizedParent != null) {
const 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 getNameFromFilePath(relativePaths(contextKey, parentId));
}
return getNameFromFilePath(parentId);
}
function relativePaths(from, to) {
const fromParts = from.split("/").filter(Boolean);
const toParts = to.split("/").filter(Boolean);
for (const part of toParts) {
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);
}
}
return "/" + fromParts.join("/");
}
export { resolveParentId, useNavigation };
//# sourceMappingURL=useNavigation.mjs.map