one
Version:
One is a new React Framework that makes Vite serve both native and web.
100 lines (99 loc) • 4.33 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
mod
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var notFoundState_exports = {};
__export(notFoundState_exports, {
clearNotFoundState: () => clearNotFoundState,
findNearestNotFoundRoute: () => findNearestNotFoundRoute,
findRouteNodeByPath: () => findRouteNodeByPath,
getNotFoundState: () => getNotFoundState,
setNotFoundState: () => setNotFoundState,
useNotFoundState: () => useNotFoundState
});
module.exports = __toCommonJS(notFoundState_exports);
var React = __toESM(require("react"), 1);
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;
}
//# sourceMappingURL=notFoundState.js.map