one
Version:
One is a React framework for web, iOS, and Android apps.
193 lines (185 loc) • 7.61 kB
JavaScript
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { DarkTheme, DefaultTheme } from "./fork/theme.native.js";
import { useUserScheme } from "@vxrn/color-scheme";
import { StrictMode, createContext, useContext, useId, useLayoutEffect, useState } from "react";
import { SERVER_CONTEXT_KEY, isNative } from "./constants.native.js";
import { initScreensFeatureFlags } from "./screensFeatureFlags.native.js";
import { SpaShellContext } from "./router/SpaShellContext.native.js";
import { NavigationContainer } from "./fork/NavigationContainer.native.js";
import { getURL } from "./getURL.native.js";
import { FlagsContext } from "./router/FlagsContext.native.js";
import { getResolvedLinking } from "./router/linkingConfig.native.js";
import { handleNavigationContainerStateChange } from "./router/router.native.js";
import { ServerLocationContext } from "./router/serverLocationContext.native.js";
import { useInitializeOneRouter } from "./router/useInitializeOneRouter.native.js";
import { useViteRoutes } from "./router/useViteRoutes.native.js";
import { evictOldest } from "./utils/evictOldest.native.js";
import { ServerRenderID } from "./useServerHeadInsertion.native.js";
import { PreloadLinks } from "./views/PreloadLinks.native.js";
import { SafeAreaProvider, initialWindowMetrics } from "./safe-area-context/index.native.js";
import { RootErrorBoundary } from "./views/RootErrorBoundary.native.js";
import { ScrollBehavior } from "./views/ScrollBehavior.native.js";
var ssrLocationCache = /* @__PURE__ */ new Map();
function getCachedSSRLocation(path) {
var url = ssrLocationCache.get(path);
if (url) return url;
url = new URL(path, getURL());
evictOldest(ssrLocationCache, 5e3, 1e3);
ssrLocationCache.set(path, url);
return url;
}
var ServerAsyncLocalIDContext = /* @__PURE__ */ createContext(null);
globalThis["__vxrnGetContextFromReactContext"] = function() {
return useContext(ServerAsyncLocalIDContext);
};
function Root(props) {
var _globalThis___vxrnrequestAsyncLocalStore;
if (isNative) {
initScreensFeatureFlags();
}
var { path, routes, routeOptions, isClient, navigationContainerProps, onRenderId, linking } = props;
var context = useViteRoutes(routes, props.routerRoot, routeOptions, globalThis["__vxrnVersion"]);
var location = isNative ? void 0 : typeof window !== "undefined" && window.location ? new URL(path || window.location.href || "/", window.location.href) : getCachedSSRLocation(path || "/");
var store = useInitializeOneRouter(context, location, linking);
var userScheme = useUserScheme();
var Component = store.rootComponent;
if (!Component) {
throw new Error(`No root component found`);
}
var id = useId();
onRenderId === null || onRenderId === void 0 ? void 0 : onRenderId(id);
var value = process.env.VERCEL ? globalThis["__oneGlobalContextId"] : ((_globalThis___vxrnrequestAsyncLocalStore = globalThis["__vxrnrequestAsyncLocalStore"]) === null || _globalThis___vxrnrequestAsyncLocalStore === void 0 ? void 0 : _globalThis___vxrnrequestAsyncLocalStore.getStore()) || null;
var deferredPreloads = props.deferredPreloads;
var contents = /* @__PURE__ */ jsx(ServerAsyncLocalIDContext.Provider, {
value,
children: /* @__PURE__ */ jsxs(ServerRenderID.Provider, {
value: id,
children: [
typeof window === "undefined" && (deferredPreloads === null || deferredPreloads === void 0 ? void 0 : deferredPreloads.map(function(src) {
return /* @__PURE__ */ jsx("link", {
rel: "modulepreload",
fetchPriority: "low",
href: src
}, src);
})),
/* @__PURE__ */ jsx(SafeAreaProvider, {
initialMetrics: initialWindowMetrics,
children: /* @__PURE__ */ jsx(NavigationContainer, {
ref: store.navigationRef,
initialState: store.initialState,
linking: getResolvedLinking(),
onUnhandledAction,
onStateChange: handleNavigationContainerStateChange,
theme: userScheme.value === "dark" ? DarkTheme : DefaultTheme,
documentTitle: { enabled: false },
...navigationContainerProps,
children: /* @__PURE__ */ jsx(ServerLocationContext.Provider, {
value: location,
children: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(ScrollBehavior, {}), /* @__PURE__ */ jsx(RootErrorBoundary, { children: /* @__PURE__ */ jsx(Component, {}) })] })
})
})
}),
typeof window !== "undefined" && /* @__PURE__ */ jsx(PreloadLinks, {}, "preload-links")
]
})
});
if (props.flags) {
contents = /* @__PURE__ */ jsx(FlagsContext.Provider, {
value: props.flags,
children: contents
});
}
if (!process.env.ONE_DISABLE_STRICT_MODE) {
contents = /* @__PURE__ */ jsx(StrictMode, { children: contents });
}
if (isClient) {
var _globalThis_SERVER_CONTEXT_KEY;
var serverMode = (_globalThis_SERVER_CONTEXT_KEY = globalThis[SERVER_CONTEXT_KEY]) === null || _globalThis_SERVER_CONTEXT_KEY === void 0 ? void 0 : _globalThis_SERVER_CONTEXT_KEY.mode;
if (serverMode === "spa") {
var [show, setShow] = useState(false);
useLayoutEffect(function() {
setShow(true);
}, []);
return show ? contents : null;
}
if (serverMode === "spa-shell") {
var [isSpaShell, setIsSpaShell] = useState(true);
useLayoutEffect(function() {
setIsSpaShell(false);
}, []);
useLayoutEffect(function() {
if (!isSpaShell) {
var initialPath = window.location.pathname + window.location.search;
requestAnimationFrame(function() {
var _store_navigationRef;
if (window.location.pathname + window.location.search !== initialPath) return;
var nav = (_store_navigationRef = store.navigationRef) === null || _store_navigationRef === void 0 ? void 0 : _store_navigationRef.current;
if (!nav) return;
var linking2 = getResolvedLinking();
if (linking2 === null || linking2 === void 0 ? void 0 : linking2.getStateFromPath) {
var freshState = linking2.getStateFromPath(initialPath, linking2.config);
if (freshState) {
nav.resetRoot(freshState);
}
}
});
}
}, [isSpaShell]);
return /* @__PURE__ */ jsx(SpaShellContext.Provider, {
value: isSpaShell,
children: contents
});
}
return contents;
}
if (props.mode === "spa-shell") {
return /* @__PURE__ */ jsx(SpaShellContext.Provider, {
value: true,
children: contents
});
}
return contents;
}
var onUnhandledAction;
if (process.env.NODE_ENV !== "production") {
onUnhandledAction = function onUnhandledAction2(action) {
var payload = action.payload;
var message = `The action '${action.type}'${payload ? ` with payload ${JSON.stringify(action.payload)}` : ""} was not handled by any navigator.`;
switch (action.type) {
case "NAVIGATE":
case "PUSH":
case "REPLACE":
case "JUMP_TO":
if (payload === null || payload === void 0 ? void 0 : payload.name) {
message += `
Do you have a route named '${payload.name}'?`;
} else {
message += `
You need to pass the name of the screen to navigate to. This may be a bug.`;
}
break;
case "GO_BACK":
case "POP":
case "POP_TO_TOP":
message += `
Is there any screen to go back to?`;
break;
case "OPEN_DRAWER":
case "CLOSE_DRAWER":
case "TOGGLE_DRAWER":
message += `
Is your screen inside a Drawer navigator?`;
break;
}
message += `
This is a development-only warning and won't be shown in production.`;
if (process.env.NODE_ENV === "test") {
throw new Error(message);
}
console.error(message);
};
} else {
onUnhandledAction = function onUnhandledAction2() {};
}
export { Root };
//# sourceMappingURL=Root.native.js.map