one
Version:
One is a new React Framework that makes Vite serve both native and web.
76 lines (75 loc) • 2.71 kB
JavaScript
import * as React from "react";
let currentNotFoundState = null;
const notFoundListeners = /* @__PURE__ */ new Set();
function getNotFoundState() {
return currentNotFoundState;
}
function setNotFoundState(state) {
currentNotFoundState = state, notFoundListeners.forEach((listener) => listener());
}
function clearNotFoundState() {
currentNotFoundState !== null && (currentNotFoundState = null, notFoundListeners.forEach((listener) => listener()));
}
function useNotFoundState() {
const [, forceUpdate] = React.useReducer((x) => x + 1, 0);
return React.useEffect(() => (notFoundListeners.add(forceUpdate), () => {
notFoundListeners.delete(forceUpdate);
}), []), currentNotFoundState;
}
function findNearestNotFoundRoute(pathname, rootNode) {
if (!rootNode) return null;
const pathParts = pathname.split("/").filter(Boolean);
function findNotFoundInNode(node) {
if (node.route === "+not-found")
return node;
for (const child of node.children || [])
if (child.route === "+not-found")
return child;
return null;
}
function traverse(node, depth, notFoundStack) {
const notFoundAtLevel = findNotFoundInNode(node);
if (notFoundAtLevel && notFoundStack.push(notFoundAtLevel), depth >= pathParts.length)
return notFoundStack.length > 0 ? notFoundStack[notFoundStack.length - 1] : null;
const segment = pathParts[depth];
for (const child of node.children || []) {
if (child.route === "+not-found") continue;
const childRoute = child.route || "", isDynamic = childRoute.startsWith("[");
if (childRoute === segment || isDynamic) {
const result = traverse(child, depth + 1, [...notFoundStack]);
if (result) return result;
}
}
return notFoundStack.length > 0 ? notFoundStack[notFoundStack.length - 1] : null;
}
return traverse(rootNode, 0, []);
}
function findRouteNodeByPath(notFoundPath, rootNode) {
if (!rootNode) return null;
const normalizedPath = notFoundPath.replace(/^(\.?\/)+|\/+$/g, "");
function searchNode(node) {
if ((node.contextKey || "").replace(/^(\.?\/)+/, "").replace(/\.[^.]+$/, "") === normalizedPath)
return node;
for (const child of node.children || []) {
const found2 = searchNode(child);
if (found2) return found2;
}
return null;
}
const found = searchNode(rootNode);
if (found) return found;
for (const child of rootNode.children || []) {
const found2 = searchNode(child);
if (found2) return found2;
}
return null;
}
export {
clearNotFoundState,
findNearestNotFoundRoute,
findRouteNodeByPath,
getNotFoundState,
setNotFoundState,
useNotFoundState
};
//# sourceMappingURL=notFoundState.js.map