one
Version:
One is a React framework for web, iOS, and Android apps.
111 lines (108 loc) • 3.83 kB
JavaScript
import React, { createContext, useContext } from "react";
import { isNative } from "./constants.mjs";
import { router } from "./router/imperative-api.mjs";
import { RouteParamsContext, useRouteNode } from "./router/Route.mjs";
import { mergeDynamicParams } from "./router/params.mjs";
import { getPathnameWithRecoveredDynamicSegment, normalizeRoutePathname } from "./router/path.mjs";
import { RouteInfoContext } from "./router/RouteInfoContext.mjs";
import { navigationRef, useStoreRootState, useStoreRouteInfo } from "./router/router.mjs";
import { getServerContext } from "./vite/one-server-only.mjs";
function useRootNavigationState() {
return useStoreRootState();
}
function useRouteInfo() {
const routeInfoFromContext = useContext(RouteInfoContext);
const routeInfoFromRootState = useStoreRouteInfo();
const routeNode = useRouteNode();
if (routeNode?.type === "layout") {
return routeInfoFromRootState;
}
return routeInfoFromContext || routeInfoFromRootState;
}
function useNavigationContainerRef() {
return navigationRef;
}
const FrozeContext = createContext(false);
function useRouter() {
return router;
}
function useUnstableGlobalHref() {
return useRouteInfo().unstable_globalHref;
}
function useSegments() {
return useRouteInfo().segments;
}
function usePathname() {
const routeInfoPathname = useRouteInfo().pathname;
if (import.meta.env.SSR) {
try {
const ctx = getServerContext();
if (ctx?.loaderProps?.path) {
return normalizeRoutePathname(ctx.loaderProps.path);
}
} catch {}
}
return getPathnameWithRecoveredDynamicSegment(routeInfoPathname);
}
function useActiveParams() {
return useRouteInfo().params;
}
const useLocalSearchParams = useParams;
const useGlobalSearchParams = useActiveParams;
function useParams() {
const contextParams = React.useContext(RouteParamsContext) ?? {};
const routeNode = useRouteNode();
const routeInfoParams = useRouteInfo().params;
const params = React.useMemo(() => isNative ? contextParams : mergeDynamicParams(contextParams, routeNode?.dynamic, routeInfoParams), [
contextParams,
routeInfoParams,
routeNode
]);
return React.useMemo(() => {
return Object.fromEntries(Object.entries(params).filter(([_, value]) => value !== void 0).map(([key, value]) => {
if (Array.isArray(value)) {
return [key, value.map((v) => {
try {
return decodeURIComponent(v);
} catch {
return v;
}
})];
}
try {
return [key, decodeURIComponent(value)];
} catch {
return [key, value];
}
}));
}, [params]);
}
var ReadOnlyURLSearchParams = class extends URLSearchParams {
set(_name, _value) {
throw new Error("useSearchParams returns a read-only URLSearchParams object");
}
append(_name, _value) {
throw new Error("useSearchParams returns a read-only URLSearchParams object");
}
delete(_name, _value) {
throw new Error("useSearchParams returns a read-only URLSearchParams object");
}
};
function useSearchParams({ global = false } = {}) {
const globalRef = React.useRef(global);
if (process.env.NODE_ENV !== "production") {
if (global !== globalRef.current) {
console.warn("useSearchParams: the 'global' option cannot change between renders");
}
}
const params = global ? useActiveParams() : useParams();
return React.useMemo(() => {
const entries = Object.entries(params).flatMap(([key, value]) => {
if (value === void 0) return [];
return Array.isArray(value) ? value.map((v) => [key, String(v)]) : [[key, String(value)]];
});
return new ReadOnlyURLSearchParams(entries);
}, [params]);
}
export { FrozeContext, useActiveParams, useGlobalSearchParams, useLocalSearchParams, useNavigationContainerRef, useParams, usePathname, useRootNavigationState, useRouteInfo, useRouter, useSearchParams, useSegments, useUnstableGlobalHref };
//# sourceMappingURL=hooks.mjs.map