sanity
Version:
Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches
725 lines (724 loc) • 31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: !0 });
var jsxRuntime = require("react/jsx-runtime"), reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), _singletons = require("sanity/_singletons"), difference = require("lodash/difference.js"), intersection = require("lodash/intersection.js"), isPlainObject = require("lodash/isPlainObject.js"), pick = require("lodash/pick.js"), debug$1 = require("debug"), fromPairs = require("lodash/fromPairs.js"), partition = require("lodash/partition.js"), toPairs = require("lodash/toPairs.js"), identity = require("lodash/identity.js");
function _interopDefaultCompat(e) {
return e && typeof e == "object" && "default" in e ? e : { default: e };
}
var difference__default = /* @__PURE__ */ _interopDefaultCompat(difference), intersection__default = /* @__PURE__ */ _interopDefaultCompat(intersection), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject), pick__default = /* @__PURE__ */ _interopDefaultCompat(pick), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$1), fromPairs__default = /* @__PURE__ */ _interopDefaultCompat(fromPairs), partition__default = /* @__PURE__ */ _interopDefaultCompat(partition), toPairs__default = /* @__PURE__ */ _interopDefaultCompat(toPairs), identity__default = /* @__PURE__ */ _interopDefaultCompat(identity);
function useRouter() {
const router = React.useContext(_singletons.RouterContext);
if (!router)
throw new Error("Router: missing context value");
return router;
}
function isLeftClickEvent(event) {
return event.button === 0;
}
function isModifiedEvent(event) {
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
}
function useLink(options) {
const $ = reactCompilerRuntime.c(8), {
onClick: onClickProp,
href,
target,
replace: t0
} = options, replace = t0 === void 0 ? !1 : t0, {
navigateUrl
} = useRouter();
let t1;
$[0] !== href || $[1] !== navigateUrl || $[2] !== onClickProp || $[3] !== replace || $[4] !== target ? (t1 = (event) => {
event.isDefaultPrevented() || href && (onClickProp && onClickProp(event), !(isModifiedEvent(event) || !isLeftClickEvent(event)) && (target || (event.preventDefault(), navigateUrl({
path: href,
replace
}))));
}, $[0] = href, $[1] = navigateUrl, $[2] = onClickProp, $[3] = replace, $[4] = target, $[5] = t1) : t1 = $[5];
const onClick = t1;
let t2;
return $[6] !== onClick ? (t2 = {
onClick
}, $[6] = onClick, $[7] = t2) : t2 = $[7], t2;
}
function useIntentLink(options) {
const $ = reactCompilerRuntime.c(13), {
intent,
onClick: onClickProp,
params,
replace,
target,
searchParams
} = options, {
resolveIntentLink
} = useRouter();
let t0, t1;
$[0] !== intent || $[1] !== params || $[2] !== resolveIntentLink || $[3] !== searchParams ? (t1 = resolveIntentLink(intent, params, searchParams), $[0] = intent, $[1] = params, $[2] = resolveIntentLink, $[3] = searchParams, $[4] = t1) : t1 = $[4], t0 = t1;
const href = t0;
let t2;
$[5] !== href || $[6] !== onClickProp || $[7] !== replace || $[8] !== target ? (t2 = {
href,
onClick: onClickProp,
replace,
target
}, $[5] = href, $[6] = onClickProp, $[7] = replace, $[8] = target, $[9] = t2) : t2 = $[9];
const {
onClick
} = useLink(t2);
let t3;
return $[10] !== href || $[11] !== onClick ? (t3 = {
onClick,
href
}, $[10] = href, $[11] = onClick, $[12] = t3) : t3 = $[12], t3;
}
const IntentLink = React.forwardRef(function(props, ref) {
const $ = reactCompilerRuntime.c(18);
let intent, params, restProps, searchParams, target;
$[0] !== props ? ({
intent,
params,
target,
searchParams,
...restProps
} = props, $[0] = props, $[1] = intent, $[2] = params, $[3] = restProps, $[4] = searchParams, $[5] = target) : (intent = $[1], params = $[2], restProps = $[3], searchParams = $[4], target = $[5]);
let t0;
$[6] !== intent || $[7] !== params || $[8] !== props.onClick || $[9] !== searchParams || $[10] !== target ? (t0 = {
intent,
params,
target,
onClick: props.onClick,
searchParams
}, $[6] = intent, $[7] = params, $[8] = props.onClick, $[9] = searchParams, $[10] = target, $[11] = t0) : t0 = $[11];
const {
onClick,
href
} = useIntentLink(t0);
let t1;
return $[12] !== href || $[13] !== onClick || $[14] !== ref || $[15] !== restProps || $[16] !== target ? (t1 = /* @__PURE__ */ jsxRuntime.jsx("a", { ...restProps, href, onClick, ref, target }), $[12] = href, $[13] = onClick, $[14] = ref, $[15] = restProps, $[16] = target, $[17] = t1) : t1 = $[17], t1;
}), Link = React.forwardRef(function(props, ref) {
const $ = reactCompilerRuntime.c(17);
let href, onClickProp, replace, restProps, target;
$[0] !== props ? ({
onClick: onClickProp,
href,
target,
replace,
...restProps
} = props, $[0] = props, $[1] = href, $[2] = onClickProp, $[3] = replace, $[4] = restProps, $[5] = target) : (href = $[1], onClickProp = $[2], replace = $[3], restProps = $[4], target = $[5]);
let t0;
$[6] !== href || $[7] !== onClickProp || $[8] !== replace || $[9] !== target ? (t0 = {
onClick: onClickProp,
href,
target,
replace
}, $[6] = href, $[7] = onClickProp, $[8] = replace, $[9] = target, $[10] = t0) : t0 = $[10];
const {
onClick
} = useLink(t0);
let t1;
return $[11] !== href || $[12] !== onClick || $[13] !== ref || $[14] !== restProps || $[15] !== target ? (t1 = /* @__PURE__ */ jsxRuntime.jsx("a", { ...restProps, onClick, href, target, ref }), $[11] = href, $[12] = onClick, $[13] = ref, $[14] = restProps, $[15] = target, $[16] = t1) : t1 = $[16], t1;
}), VALID_PARAM_SEGMENT = /^[a-zA-Z0-9_-]+$/;
function createSegment(segment) {
if (!segment)
return null;
if (segment.startsWith(":")) {
const paramName = segment.slice(1);
if (!VALID_PARAM_SEGMENT.test(paramName)) {
const addendum = segment.includes("*") ? " Splats are not supported. Consider using child routes instead" : "";
console.error(new Error(`Warning: Param segments "${segment}" includes invalid characters.${addendum}`));
}
return {
type: "param",
name: paramName
};
}
return {
type: "dir",
name: segment
};
}
function _parseRoute(route2) {
const [pathname] = route2.split("?"), segments = pathname.split("/").map(createSegment).filter(Boolean);
return {
raw: route2,
segments
};
}
function arrayify(val) {
return Array.isArray(val) ? val : val ? [val] : [];
}
function isRecord(value) {
return isPlainObject__default.default(value);
}
function createMatchError(node, missingKeys, unmappableStateKeys) {
return {
type: "error",
node,
missingKeys,
unmappableStateKeys
};
}
function createMatchOk(node, matchedState, searchParams, child) {
return {
type: "ok",
node,
matchedState,
searchParams,
child
};
}
function _findMatchingRoutes(node, _state) {
if (!_state)
return createMatchOk(node, {}, []);
const scopedState = node.scope ? _state[node.scope] : _state, {
_searchParams: searchParams = [],
...state
} = scopedState || {}, requiredParams = node.route.segments.filter((seg) => seg.type === "param").map((seg) => seg.name), stateKeys = isRecord(state) ? Object.keys(state) : [], consumedParams = intersection__default.default(stateKeys, requiredParams), missingParams = difference__default.default(requiredParams, consumedParams), remainingParams = difference__default.default(stateKeys, consumedParams);
if (missingParams.length > 0)
return createMatchError(node, missingParams, []);
const scopedParams = searchParams.map(([key, value]) => [[key], value]), consumedState = pick__default.default(state, consumedParams);
if (remainingParams.length === 0)
return createMatchOk(node, consumedState, scopedParams);
const children = arrayify((typeof node.children == "function" ? node.children(isRecord(state) ? state : {}) : node.children) || []);
if (remainingParams.length > 0 && children.length === 0)
return createMatchError(node, [], remainingParams);
const remainingState = pick__default.default(state, remainingParams), found = children.map((childNode) => _findMatchingRoutes(childNode, remainingState)).find((res) => res.type === "ok");
return found ? createMatchOk(node, consumedState, scopedParams, found) : createMatchError(node, [], remainingParams);
}
function encodeURIComponentExcept(uriComponent, unescaped) {
const chars = [...String(uriComponent)];
let res = "";
for (let i = 0; i < chars.length; i++) {
const char = chars[i];
unescaped.includes(char) ? res += char : res += encodeURIComponent(char);
}
return res;
}
const debug = debug__default.default("state-router");
function _resolvePathFromState(node, _state) {
debug("Resolving path from state %o", _state);
const match = _findMatchingRoutes(node, _state);
if (match.type === "error") {
const unmappable = match.unmappableStateKeys;
if (unmappable.length > 0)
throw new Error(`Unable to find matching route for state. Could not map the following state key${unmappable.length == 1 ? "" : "s"} to a valid url: ${unmappable.map(quote).join(", ")}`);
const missingKeys = match.missingKeys;
throw new Error(`Unable to find matching route for state. State object is missing the following key${missingKeys.length == 1 ? "" : "s"} defined in route: ${missingKeys.map(quote).join(", ")}`);
}
const {
path,
searchParams
} = pathFromMatchResult(match), search = searchParams.length > 0 ? encodeParams$1(searchParams) : "";
return `/${path.join("/")}${search ? `?${search}` : ""}`;
}
function bracketify(value) {
return `[${value}]`;
}
function encodeParams$1(params) {
return params.flatMap(([key, value]) => value === void 0 ? [] : [encodeSearchParamKey(serializeScopedPath(key)), encodeSearchParamValue(value)].join("=")).join("&");
}
function serializeScopedPath(scopedPath) {
const [head, ...tail] = scopedPath;
return tail.length > 0 ? [head, ...tail.map(bracketify)].join("") : head;
}
function encodeSearchParamValue(value) {
return encodeURIComponentExcept(value, "/");
}
function encodeSearchParamKey(value) {
return encodeURIComponentExcept(value, "[]");
}
function pathFromMatchResult(match) {
const matchedState = match.matchedState, base = match.node.route.segments.map((segment) => {
if (segment.type === "dir")
return segment.name;
const transform = match.node.transform && match.node.transform[segment.name];
return transform ? transform.toPath(matchedState[segment.name]) : matchedState[segment.name];
}), childMatch = match.child ? pathFromMatchResult(match.child) : void 0, searchParams = childMatch?.searchParams ? [...match.searchParams, ...childMatch.searchParams] : match.searchParams;
return {
searchParams: addNodeScope(match.node, searchParams),
path: [...base || [], ...childMatch?.path || []]
};
}
function addNodeScope(node, searchParams) {
const scope = node.scope;
return scope && !node.__unsafe_disableScopedSearchParams ? searchParams.map(([namespaces, value]) => [[scope, ...namespaces], value]) : searchParams;
}
function quote(value) {
return `"${value}"`;
}
function parseScopedParams(params) {
return params.map(([key, value]) => [parse(key), value]);
}
const OPEN = 1, CLOSED = 0;
function parse(str) {
const result = [];
let i = 0, state = CLOSED;
for (; i < str.length; ) {
const nextBracketIdx = str.indexOf("[", i);
if (nextBracketIdx === -1) {
result.push(str.slice(i, str.length));
break;
}
if (state === OPEN)
throw new Error("Nested brackets not supported");
state = OPEN, nextBracketIdx > i && (result.push(str.slice(i, nextBracketIdx)), i = nextBracketIdx);
const nextClosing = str.indexOf("]", nextBracketIdx);
if (nextClosing === -1) {
if (state === OPEN)
throw new Error("Unclosed bracket");
break;
}
state = CLOSED, result.push(str.slice(i + 1, nextClosing)), i = nextClosing + 1;
}
return result;
}
function matchPath(node, path, searchParams) {
const parts = path.split("/").filter(Boolean), segmentsLength = node.route.segments.length;
if (parts.length < segmentsLength)
return null;
const state = {};
if (!node.route.segments.every((segment, i) => {
if (segment.type === "dir")
return segment.name === parts[i];
const transform = node.transform && node.transform[segment.name];
return state[segment.name] = transform ? transform.toState(parts[i]) : parts[i], !0;
}))
return null;
const rest = parts.slice(segmentsLength);
let childState = null;
const children = typeof node.children == "function" ? arrayify(node.children(state)) : node.children, unscopedParams = removeScope(node.scope, searchParams);
if (children.some((childNode) => {
if (childNode) {
const childParams = childNode.scope ? unscopedParams.filter(([namespaces]) => childNode.scope === namespaces[0]) : unscopedParams;
return childState = matchPath(childNode, rest.join("/"), childParams), childState;
}
}), rest.length > 0 && !childState)
return null;
const selfParams = unscopedParams.flatMap(([namespace, value]) => namespace.length === 1 ? [[namespace[0], value]] : []), mergedState = {
...state,
...childState === null ? {} : childState,
...selfParams.length > 0 ? {
_searchParams: selfParams
} : {}
};
return node.scope ? {
[node.scope]: mergedState
} : mergedState;
}
function _resolveStateFromPath(node, path) {
debug("resolving state from path %s", path);
const [pathname, search] = path.split("?"), urlSearchParams = Array.from(new URLSearchParams(search).entries()), pathMatch = matchPath(node, pathname, parseScopedParams(urlSearchParams));
return debug("resolved: %o", pathMatch || null), pathMatch || null;
}
function removeScope(scope, searchParams) {
return scope ? searchParams.map(([namespaces, value]) => [namespaces[0] === scope ? namespaces.slice(1) : namespaces, value]) : searchParams;
}
function encodeBase64Url(str) {
return encodeBase64(str).replace(/\//g, "_").replace(/\+/g, "-").replace(/[=]+$/, "");
}
function decodeBase64Url(str) {
return decodeBase64(str.replace(/-/g, "+").replace(/_/g, "/"));
}
function percentToByte(p) {
return String.fromCharCode(parseInt(p.slice(1), 16));
}
function encodeBase64(str) {
return btoa(encodeURIComponent(str).replace(/%[0-9A-F]{2}/g, percentToByte));
}
function byteToPercent(b) {
return `%${`00${b.charCodeAt(0).toString(16)}`.slice(-2)}`;
}
function decodeBase64(str) {
return decodeURIComponent(Array.from(atob(str), byteToPercent).join(""));
}
function decodeJsonParams(pathSegment = "") {
const segment = decodeURIComponent(pathSegment);
if (!segment)
return {};
try {
return JSON.parse(decodeBase64Url(segment));
} catch {
}
try {
return JSON.parse(atob(segment));
} catch {
}
try {
return JSON.parse(segment);
} catch {
console.warn("Failed to parse JSON parameters");
}
return {};
}
function encodeJsonParams(params) {
return params ? encodeBase64Url(JSON.stringify(params)) : "";
}
function decodeParams(pathSegment) {
return pathSegment.split(";").reduce((params, pair) => {
const [key, value] = pair.split("=");
return params[decodeURIComponent(key)] = decodeURIComponent(value), params;
}, {});
}
function encodeParams(params) {
return Object.entries(params).filter(([, value]) => value != null).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join(";");
}
const route = {
create: (routeOrOpts, childrenOrOpts, children) => _createNode(normalizeArgs(routeOrOpts, childrenOrOpts, children)),
intents: (base) => {
const basePath = normalize(base).join("/");
return route.create(`${basePath}/:intent`, [route.create(":params", {
transform: {
params: {
toState: decodeParams,
toPath: encodeParams
}
}
}, [route.create(":payload", {
transform: {
payload: {
toState: decodeJsonParams,
toPath: encodeJsonParams
}
}
})])]);
},
scope(scopeName, routeOrOpts, childrenOrOpts, children) {
const options = normalizeArgs(routeOrOpts, childrenOrOpts, children);
return _createNode({
...options,
scope: scopeName
});
}
};
function normalizeChildren(children) {
return Array.isArray(children) || typeof children == "function" ? children : children ? [children] : [];
}
function isRoute(val) {
return val && "_isRoute" in val;
}
function normalizeArgs(path, childrenOrOpts, children) {
return typeof path == "object" ? path : Array.isArray(childrenOrOpts) || typeof childrenOrOpts == "function" || isRoute(childrenOrOpts) ? {
path,
children: normalizeChildren(childrenOrOpts)
} : children ? {
path,
...childrenOrOpts,
children: normalizeChildren(children)
} : {
path,
...childrenOrOpts
};
}
function normalize(...paths) {
return paths.reduce((acc, path) => acc.concat(path.split("/")), []).filter(Boolean);
}
const EMPTY_STATE$1 = {};
function isRoot(pathname) {
return pathname.split("/").every((segment) => !segment);
}
function _createNode(options) {
const {
path,
scope,
transform,
children,
__unsafe_disableScopedSearchParams
} = options;
if (!path)
throw new TypeError("Missing path");
const parsedRoute = _parseRoute(path);
return {
_isRoute: !0,
// todo: make a Router class instead
scope,
// eslint-disable-next-line camelcase
__unsafe_disableScopedSearchParams,
route: parsedRoute,
children: children || [],
transform,
encode(state) {
return _resolvePathFromState(this, state);
},
decode(_path) {
return _resolveStateFromPath(this, _path);
},
isRoot,
isNotFound(pathname) {
return this.decode(pathname) === null;
},
getBasePath() {
return this.encode(EMPTY_STATE$1);
},
getRedirectBase(pathname) {
if (isRoot(pathname)) {
const basePath = this.getBasePath();
if (pathname !== basePath)
return basePath;
}
return null;
}
};
}
const STICKY_PARAMS = ["perspective", "excludedPerspectives"], isNavigateOptions = (maybeNavigateOptions) => {
if (typeof maybeNavigateOptions != "object" || maybeNavigateOptions === null || Array.isArray(maybeNavigateOptions) || !("replace" in maybeNavigateOptions || "stickyParams" in maybeNavigateOptions || "state" in maybeNavigateOptions))
return !1;
if ("state" in maybeNavigateOptions) {
const {
state
} = maybeNavigateOptions;
return state == null || typeof state == "object";
}
return !0;
};
function RouterProvider(props) {
const {
onNavigate,
router: routerProp,
state
} = props, resolveIntentLink = React.useCallback((intentName, parameters, _searchParams) => {
const [params, payload] = Array.isArray(parameters) ? parameters : [parameters];
return routerProp.encode({
intent: intentName,
params,
payload,
_searchParams: toPairs__default.default({
...fromPairs__default.default((state._searchParams ?? []).filter(([key]) => STICKY_PARAMS.includes(key))),
...fromPairs__default.default(_searchParams ?? [])
})
});
}, [routerProp, state._searchParams]), resolvePathFromState = React.useCallback((nextState) => {
const currentStateParams = state._searchParams || [], nextStateParams = nextState?._searchParams || [], nextParams = STICKY_PARAMS.reduce((acc, param) => replaceStickyParam(acc, param, findParam(nextStateParams, param) ?? findParam(currentStateParams, param)), nextStateParams || []);
return routerProp.encode({
...nextState,
_searchParams: nextParams
});
}, [routerProp, state]), navigate = React.useCallback((nextStateOrOptions, maybeOptions) => {
const options = isNavigateOptions(nextStateOrOptions) && !maybeOptions ? nextStateOrOptions : maybeOptions || {}, baseState = isNavigateOptions(nextStateOrOptions) ? getStateFromOptions(nextStateOrOptions, state) ?? state : nextStateOrOptions, currentParams = state._searchParams || [], nextStickyParams = options.stickyParams ?? Object.fromEntries(currentParams.filter(([key_0]) => STICKY_PARAMS.includes(key_0)));
validateStickyParams(nextStickyParams);
const nextParams_0 = baseState._searchParams || [], mergedParams = mergeStickyParams(nextParams_0, nextStickyParams);
onNavigate({
path: resolvePathFromState({
...baseState,
_searchParams: mergedParams
}),
replace: options.replace
});
}, [onNavigate, resolvePathFromState, state]), handleNavigateStickyParams = React.useCallback((params_0, options_0 = {}) => navigate({
stickyParams: params_0,
...options_0,
state: void 0
}), [navigate]), navigateIntent = React.useCallback((intentName_0, params_1, options_1 = {}) => {
onNavigate({
path: resolveIntentLink(intentName_0, params_1),
replace: options_1.replace
});
}, [onNavigate, resolveIntentLink]), [routerState, stickyParams] = React.useMemo(() => {
if (!state._searchParams)
return [state, null];
const {
_searchParams: _searchParams_0,
...rest
} = state, [sticky, restParams] = partition__default.default(_searchParams_0, ([key_1]) => STICKY_PARAMS.includes(key_1));
return sticky.length === 0 ? [state, null] : [{
...rest,
_searchParams: restParams
}, sticky];
}, [state]), stickyParamsByName = React.useMemo(() => Object.fromEntries(stickyParams || []), [stickyParams]), router = React.useMemo(() => ({
navigate,
navigateIntent,
navigateStickyParams: handleNavigateStickyParams,
navigateUrl: onNavigate,
resolveIntentLink,
resolvePathFromState,
state: routerState,
stickyParams: stickyParamsByName
}), [handleNavigateStickyParams, navigate, navigateIntent, onNavigate, resolveIntentLink, resolvePathFromState, routerState, stickyParamsByName]);
return /* @__PURE__ */ jsxRuntime.jsx(_singletons.RouterContext.Provider, { value: router, children: props.children });
}
function replaceStickyParam(current, param, value) {
const filtered = current.filter(([key]) => key !== param);
return value === void 0 || value == "" ? filtered : [...filtered, [param, value]];
}
function mergeStickyParams(currentParams, newParams) {
if (!newParams) return currentParams;
const filteredParams = currentParams.filter(([key]) => !Object.hasOwn(newParams, key)), isValidSearchParam = (entry) => entry[1] !== void 0, convertNullSearchParam = (entry) => [entry[0], entry[1] === null ? "" : entry[1]], newEntries = Object.entries(newParams).filter(isValidSearchParam).map(convertNullSearchParam);
return [...filteredParams, ...newEntries];
}
function findParam(searchParams, key) {
const entry = searchParams.find(([k]) => k === key);
return entry ? entry[1] : void 0;
}
function getStateFromOptions(nextStateOrOptions, state) {
return isNavigateOptions(nextStateOrOptions) ? nextStateOrOptions.state === null ? {} : nextStateOrOptions.state ?? state : null;
}
function validateStickyParams(nextStickyParams) {
if (Object.keys(nextStickyParams).some((param) => !STICKY_PARAMS.includes(param))) throw new Error("One or more parameters are not sticky");
}
function addScope(routerState, scope, scopedState) {
return scopedState && {
...routerState,
[scope]: scopedState
};
}
const RouteScope = function(props) {
const $ = reactCompilerRuntime.c(24), {
children,
scope,
__unsafe_disableScopedSearchParams
} = props, parentRouter = useRouter(), {
resolvePathFromState: parent_resolvePathFromState,
navigate: parent_navigate
} = parentRouter, parentStateRef = React.useRef(parentRouter.state);
let t0, t1;
$[0] !== parentRouter.state ? (t0 = () => {
parentStateRef.current = parentRouter.state;
}, t1 = [parentRouter.state], $[0] = parentRouter.state, $[1] = t0, $[2] = t1) : (t0 = $[1], t1 = $[2]), React.useEffect(t0, t1);
let t2;
$[3] !== __unsafe_disableScopedSearchParams || $[4] !== scope ? (t2 = (_nextState) => {
if (_nextState === null)
return null;
const {
_searchParams,
...nextState
} = _nextState || {}, nextParentState = addScope(parentStateRef.current, scope, nextState);
return __unsafe_disableScopedSearchParams ? nextParentState._searchParams = _searchParams : nextParentState[scope]._searchParams = _searchParams, nextParentState;
}, $[3] = __unsafe_disableScopedSearchParams, $[4] = scope, $[5] = t2) : t2 = $[5];
const resolveNextParentState = t2;
let t3;
$[6] !== parent_resolvePathFromState || $[7] !== resolveNextParentState ? (t3 = (nextState_0) => parent_resolvePathFromState(resolveNextParentState(nextState_0)), $[6] = parent_resolvePathFromState, $[7] = resolveNextParentState, $[8] = t3) : t3 = $[8];
const resolvePathFromState = t3;
let t4;
$[9] !== parent_navigate || $[10] !== resolveNextParentState ? (t4 = (nextStateOrOptions, maybeOptions) => {
if (isNavigateOptions(nextStateOrOptions) && !maybeOptions) {
const options = nextStateOrOptions, {
state
} = options;
if (state) {
const nextState_1 = resolveNextParentState(state);
return parent_navigate(nextState_1 === null ? {} : nextState_1, options);
}
return parent_navigate(options);
}
const nextState_2 = isNavigateOptions(nextStateOrOptions) ? resolveNextParentState(null) : resolveNextParentState(nextStateOrOptions);
return parent_navigate(nextState_2 === null ? {} : nextState_2, maybeOptions || {});
}, $[9] = parent_navigate, $[10] = resolveNextParentState, $[11] = t4) : t4 = $[11];
const navigate = t4;
let t5;
const parentState = parentRouter.state;
let childState;
$[12] !== __unsafe_disableScopedSearchParams || $[13] !== parentState || $[14] !== scope ? (childState = typeof parentState[scope] == "object" ? {
...parentState[scope]
} : {}, __unsafe_disableScopedSearchParams && (childState._searchParams = parentState._searchParams), $[12] = __unsafe_disableScopedSearchParams, $[13] = parentState, $[14] = scope, $[15] = childState) : childState = $[15];
let t6;
$[16] !== childState || $[17] !== navigate || $[18] !== parentRouter || $[19] !== resolvePathFromState ? (t6 = {
...parentRouter,
navigate,
resolvePathFromState,
state: childState
}, $[16] = childState, $[17] = navigate, $[18] = parentRouter, $[19] = resolvePathFromState, $[20] = t6) : t6 = $[20], t5 = t6;
const childRouter = t5;
let t7;
return $[21] !== childRouter || $[22] !== children ? (t7 = /* @__PURE__ */ jsxRuntime.jsx(_singletons.RouterContext.Provider, { value: childRouter, children }), $[21] = childRouter, $[22] = children, $[23] = t7) : t7 = $[23], t7;
};
RouteScope.displayName = "RouteScope";
const EMPTY_STATE = {};
function useStateLink(options) {
const $ = reactCompilerRuntime.c(11), {
onClick: onClickProp,
replace,
state,
target,
toIndex: t0
} = options, toIndex = t0 === void 0 ? !1 : t0;
if (state && toIndex)
throw new Error("Passing both `state` and `toIndex={true}` as props to StateLink is invalid");
!state && !toIndex && console.error(new Error("No state passed to StateLink. If you want to link to an empty state, its better to use the the `toIndex` property"));
const {
resolvePathFromState
} = useRouter();
let t1;
const t2 = toIndex ? EMPTY_STATE : state || EMPTY_STATE;
let t3;
$[0] !== resolvePathFromState || $[1] !== t2 ? (t3 = resolvePathFromState(t2), $[0] = resolvePathFromState, $[1] = t2, $[2] = t3) : t3 = $[2], t1 = t3;
const href = t1;
let t4;
$[3] !== href || $[4] !== onClickProp || $[5] !== replace || $[6] !== target ? (t4 = {
href,
onClick: onClickProp,
replace,
target
}, $[3] = href, $[4] = onClickProp, $[5] = replace, $[6] = target, $[7] = t4) : t4 = $[7];
const {
onClick
} = useLink(t4);
let t5;
return $[8] !== href || $[9] !== onClick ? (t5 = {
onClick,
href
}, $[8] = href, $[9] = onClick, $[10] = t5) : t5 = $[10], t5;
}
const StateLink = React.forwardRef(function(props, ref) {
const $ = reactCompilerRuntime.c(18);
let onClickProp, replace, restProps, state, t0, target;
$[0] !== props ? ({
onClick: onClickProp,
replace,
state,
target,
toIndex: t0,
...restProps
} = props, $[0] = props, $[1] = onClickProp, $[2] = replace, $[3] = restProps, $[4] = state, $[5] = t0, $[6] = target) : (onClickProp = $[1], replace = $[2], restProps = $[3], state = $[4], t0 = $[5], target = $[6]);
const toIndex = t0 === void 0 ? !1 : t0;
let t1;
$[7] !== onClickProp || $[8] !== replace || $[9] !== state || $[10] !== target || $[11] !== toIndex ? (t1 = {
onClick: onClickProp,
replace,
state,
target,
toIndex
}, $[7] = onClickProp, $[8] = replace, $[9] = state, $[10] = target, $[11] = toIndex, $[12] = t1) : t1 = $[12];
const {
onClick,
href
} = useStateLink(t1);
let t2;
return $[13] !== href || $[14] !== onClick || $[15] !== ref || $[16] !== restProps ? (t2 = /* @__PURE__ */ jsxRuntime.jsx("a", { ...restProps, href, onClick, ref }), $[13] = href, $[14] = onClick, $[15] = ref, $[16] = restProps, $[17] = t2) : t2 = $[17], t2;
});
function useRouterState(t0) {
const $ = reactCompilerRuntime.c(3), selector = t0 === void 0 ? identity__default.default : t0, {
state
} = useRouter();
let t1, t2;
return $[0] !== selector || $[1] !== state ? (t2 = selector(state), $[0] = selector, $[1] = state, $[2] = t2) : t2 = $[2], t1 = t2, t1;
}
function withRouter(Component) {
function WithRouter2(props) {
const $ = reactCompilerRuntime.c(3), router = useRouter(), t0 = props;
let t1;
return $[0] !== router || $[1] !== t0 ? (t1 = /* @__PURE__ */ jsxRuntime.jsx(Component, { ...t0, router }), $[0] = router, $[1] = t0, $[2] = t1) : t1 = $[2], t1;
}
return WithRouter2.displayName = `withRouter(${Component.displayName || Component.name})`, WithRouter2;
}
const WithRouter = withRouter((props) => props.children(props.router));
Object.defineProperty(exports, "RouterContext", {
enumerable: !0,
get: function() {
return _singletons.RouterContext;
}
});
exports.IntentLink = IntentLink;
exports.Link = Link;
exports.RouteScope = RouteScope;
exports.RouterProvider = RouterProvider;
exports.STICKY_PARAMS = STICKY_PARAMS;
exports.StateLink = StateLink;
exports.WithRouter = WithRouter;
exports._createNode = _createNode;
exports.decodeJsonParams = decodeJsonParams;
exports.encodeJsonParams = encodeJsonParams;
exports.route = route;
exports.useIntentLink = useIntentLink;
exports.useLink = useLink;
exports.useRouter = useRouter;
exports.useRouterState = useRouterState;
exports.useStateLink = useStateLink;
exports.withRouter = withRouter;
//# sourceMappingURL=router.js.map