UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

129 lines 4.56 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__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: true }) : target, mod)); var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), 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() { 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; }