one
Version:
One is a new React Framework that makes Vite serve both native and web.
89 lines (88 loc) • 2.93 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() {
if (currentNotFoundState !== null) {
currentNotFoundState = null;
notFoundListeners.forEach(listener => listener());
}
}
function useNotFoundState() {
const [, forceUpdate] = React.useReducer(x => x + 1, 0);
React.useEffect(() => {
notFoundListeners.add(forceUpdate);
return () => {
notFoundListeners.delete(forceUpdate);
};
}, []);
return 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);
}
if (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 || "";
const isDynamic = childRoute.startsWith("[");
const isMatch = childRoute === segment || isDynamic;
if (isMatch) {
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) {
const nodeContextKey = node.contextKey || "";
const contextKeyNormalized = nodeContextKey.replace(/^(\.?\/)+/, "").replace(/\.[^.]+$/, "");
if (contextKeyNormalized === 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.mjs.map