react-router-manage
Version:
react-router-manage extends react-router(v6), it Including authentication, configuration, addition, deletion and modification
1,711 lines (1,710 loc) • 44.4 kB
JavaScript
/**
* React Router Manage v2.0.4
*
* Copyright (c) onshinpei Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
!(function(e, t) {
"object" == typeof exports && "undefined" != typeof module
? t(
exports,
require("react-router"),
require("react-router-dom"),
require("react"),
require("react-dom"),
require("@remix-run/router"),
require("query-string")
)
: "function" == typeof define && define.amd
? define([
"exports",
"react-router",
"react-router-dom",
"react",
"react-dom",
"@remix-run/router",
"query-string"
], t)
: t(
((e =
"undefined" != typeof globalThis
? globalThis
: e || self).ReactRouterDOM = {}),
e.ReactRouter,
e.reactRouterDom,
e.React,
e.reactDom,
e.Router,
e.queryString
);
})(this, function(e, t, n, r, o, u, a) {
"use strict";
function i(e) {
if (e && e.__esModule) return e;
var t = Object.create(null);
return (
e &&
Object.keys(e).forEach(function(n) {
if ("default" !== n) {
var r = Object.getOwnPropertyDescriptor(e, n);
Object.defineProperty(
t,
n,
r.get
? r
: {
enumerable: !0,
get: function() {
return e[n];
}
}
);
}
}),
(t.default = e),
Object.freeze(t)
);
}
var s = i(r);
function c() {
return (
(c = Object.assign
? Object.assign.bind()
: function(e) {
for (var t = 1; t < arguments.length; t++) {
var n = arguments[t];
for (var r in n)
Object.prototype.hasOwnProperty.call(n, r) && (e[r] = n[r]);
}
return e;
}),
c.apply(this, arguments)
);
}
const l = s.createContext(null);
function f() {
return s.useContext(l).history;
}
function p() {
return s.useContext(l).routeHooksRef;
}
function m({ basename: e, children: t, syncUpdateCurrentRoute: a }) {
const i = s.useRef(null),
c = s.useRef(null);
if (null == i.current) {
const e = u.createBrowserHistory({ window: window, v5Compat: !0 });
(i.current = { ...e, back: () => e.go(-1), forward: () => e.go(1) }),
(c.current = []);
}
const f = r.useMemo(
() => ({
history: i.current,
routeHooks: c.current,
routeHooksRef: c,
historyMethods: {
push: i.current.push,
replace: i.current.replace,
go: i.current.go,
back: i.current.back,
forward: i.current.forward
}
}),
[]
),
p = i.current,
[m, d] = s.useState({ action: p.action, location: p.location });
return (
s.useLayoutEffect(() => {
let e = !0;
const t = p.listen(t => {
const { location: n } = t;
e &&
o.unstable_batchedUpdates(() => {
d(t), a(n);
});
});
return () => {
(e = !1), t();
};
}, [p, a]),
s.createElement(
l.Provider,
{ value: f },
s.createElement(
n.Router,
{
basename: e,
location: m.location,
navigationType: m.action,
navigator: p
},
t
)
)
);
}
function d({ basename: e, children: t, syncUpdateCurrentRoute: a }) {
const i = s.useRef(null),
c = s.useRef(null);
if (null == i.current) {
const e = u.createHashHistory({ window: window, v5Compat: !0 });
(i.current = { ...e, back: () => e.go(-1), forward: () => e.go(1) }),
(c.current = []);
}
const f = r.useMemo(
() => ({
history: i.current,
routeHooks: c.current,
routeHooksRef: c,
historyMethods: {
push: i.current.push,
replace: i.current.replace,
go: i.current.go
}
}),
[]
),
p = i.current,
[m, d] = s.useState({ action: p.action, location: p.location });
return (
s.useLayoutEffect(() => {
let e = !0;
const t = p.listen(t => {
const { location: n } = t;
e &&
o.unstable_batchedUpdates(() => {
d(t), a(n);
});
});
return () => {
(e = !1), t();
};
}, [p, a]),
s.createElement(
l.Provider,
{ value: f },
s.createElement(
n.Router,
{
basename: e,
location: m.location,
navigationType: m.action,
navigator: p
},
t
)
)
);
}
l.displayName = "MRouterHistoryContext";
const h = {
401: {
title: "401 NO PERMISSION",
img: "//ysf.qiyukf.net/yx/9c9ce7793b3c0657da5d80e740a89681"
},
404: {
title: "404 NOT FOUND",
img: "https://ysf.nosdn.127.net/ysh/6be90dea7806767fe65e7b48982b7a61"
}
},
b = ({ code: e = "401" }) => {
const t = h[e];
return s.createElement(
"div",
{
style: {
display: "flex",
justifyContent: "center",
alignItems: "center",
background: "white",
height: "100%",
flexDirection: "column",
borderRadius: 8
}
},
s.createElement("img", { alt: t.title, width: 200, src: t?.img }),
s.createElement(
"div",
{
style: {
color: "#666666",
lineHeight: "22px",
textAlign: "center",
marginTop: 16
}
},
t.title
)
);
};
b.displayName = "NoAuth";
const R = ({ name: e, component: t }) => {
const { routesMap: r, currentRoute: o } = A(),
u = r[e];
return e === o.name
? t
? s.createElement(t, null)
: s.createElement(s.Fragment, null)
: s.createElement(n.Navigate, { to: u.path || "" });
},
y = ({ tip: e = "加载中" }) => s.createElement("div", null, e),
g = {
LoadingComponent: () =>
s.createElement(
s.Fragment,
null,
s.createElement(y, { tip: "应用正在加载中…" })
)
};
function E(e, t) {
let r;
return e
? (!(function(e) {
if (e instanceof Function) return !0;
return !1;
})(e)
? B(e.path)
? (r = function() {
return s.createElement(n.Navigate, { to: e.path });
})
: B(e.name) &&
(r = function() {
return s.createElement(R, { name: e.name, component: t });
})
: (r = e),
r)
: r;
}
const P = ({ beforeEnter: e, Component: t, beforeEachMount: n }) => {
const [o, u] = r.useState({ Component: void 0 }),
{ currentRoute: a } = A();
r.useLayoutEffect(() => {
let r = !0;
return (
n
? n(a, n => {
if (!r) return;
const o = E(n, t);
e
? e(a, e => {
if (!r) return;
const n = E(e, o || t);
u({ Component: n || o || t });
})
: u({ Component: o || t });
})
: e &&
e(a, e => {
if (!r) return;
const n = E(e, t);
u({ Component: n || t });
}),
() => {
r = !1;
}
);
}, [t, a, e, n]);
const i = g.LoadingComponent;
return o.Component
? s.createElement(o.Component, null)
: s.createElement(i, null);
},
O = () => s.createElement(b, { code: "404" });
const _ = () => {
const e = t.useNavigate();
return r.useCallback(
(n, r = {}) => {
if (
(r?.params &&
"string" == typeof n &&
(n = t.generatePath(n, r.params)),
r?.query && "string" == typeof n)
) {
let e = n;
const t = a.stringify(r.query);
(e = e.includes("?") ? `${e}&${t}` : `${e}?${t}`), (n = e);
}
return e(n, r);
},
[e]
);
};
function A() {
const e = t.useLocation(),
n = r.useRef({}),
{
routesMap: o,
inputRoutes: u,
currentRoute: i,
flattenRoutes: c,
authInputRoutes: l,
basename: f
} = s.useContext(G).state;
n.current !== o && (n.current = o);
const p = r.useMemo(() => a.parse(e.search), [e.search]),
m = t.useParams();
return {
navigate: _(),
routesMap: o,
query: p,
params: m,
routes: u,
authRoutes: l,
currentRoute: i,
flattenRoutes: c,
location: e,
basename: f
};
}
const v = () => {
const { currentRoute: e } = A(),
n = r.useMemo(() => {
if (!e) return "";
const t = e._itemsAndChildren || [],
n = t?.find(e => e._isHasAuth);
return n?.path || "";
}, [e]);
let o = !1;
return (
e.parent?.component || (o = !0),
n
? s.createElement(t.Navigate, { to: n, replace: o })
: s.createElement(b, null)
);
};
function M(e = "", t = "/", n) {
return e.startsWith(t)
? e
: e === t
? t
: e.startsWith("/")
? t.endsWith("/")
? `${t}${e.slice(1)}`
: `${t}${e}`
: n
? M(e, n)
: t.endsWith("/")
? `${t}${e}`
: `${t}/${e}`;
}
function j(e) {
const { routes: t, parent: n, basename: r = "/", _level: o = 1 } = e;
if (!t) return [];
return (function e(t, n, o = 1) {
return t.map(t => {
const { path: u, items: a, children: i, ...s } = t,
c = {
...s,
path: S(M(u, r, n?.path)),
parent: n,
_level: o,
_relativePath: u
};
return (
a && (c.items = e(a, c, o + 1)), i && (c.children = e(i, c, o + 1)), c
);
});
})(t, n, o);
}
function C(e) {
const {
inputRoutes: t,
permissionList: n,
permissionMode: r,
hasAuth: o,
beforeEachMount: u,
basename: a,
location: i
} = e,
s = H({
routes: t,
permissionList: n,
permissionMode: r,
hasAuth: o,
beforeEachMount: u
}),
c = N(s, void 0, !0);
"children" === r &&
o &&
c.forEach(e => {
const t = e.route;
if (!t._currentIsHasAuth) return;
let n = t;
for (; n; )
(n._isHasAuth = !0),
(n._component = n._currentComponent),
(n = n.parent);
}),
(function(e, t, n) {
const r = M("*", t),
o = e.some(({ path: e }) => e === r);
if (o) return;
const u = {
name: "notFound",
title: "notFound",
meta: {},
path: r,
component: O,
_isHasAuth: !0,
_component: O,
_relativePath: r,
_level: 0,
_route: {
_relativePath: r,
_level: 0,
name: "notFound",
title: "notFound",
meta: {},
path: r,
component: O
}
};
n.push(u), e.push({ path: r, score: e.length, route: u });
})(c, a, s),
c.sort((e, t) => t.score - e.score);
const l = c.map(e => e.route),
f = w(c),
p = D(i.pathname, f, l);
return {
authInputRoutes: s,
flattenRoutes: l,
routesMap: f,
currentRoute: p,
currentPathRoutes: x(p),
beforeEachMount: u
};
}
const T = /^:\w+$/,
U = e => "*" === e;
function L(e, t) {
let n = e.split("/"),
r = n.length;
return (
n.some(U) && (r += -2),
t && (r += 2),
n
.filter(e => !U(e))
.reduce((e, t) => e + (T.test(t) ? 3 : "" === t ? 1 : 10), r)
);
}
const N = (e, t, n) =>
e.reduce((e, r) => {
t && (r.parent = t);
const o = { path: r.path, route: r, score: L(r.path, !1) };
if (Array.isArray(r.items) || Array.isArray(r.children)) {
let t = e.concat(o);
return (
Array.isArray(r.items) && (t = t.concat(N(r.items, r, n))),
Array.isArray(r.children) &&
n &&
(t = t.concat(N(r.children, r, n))),
t
);
}
return e.concat(o);
}, []),
w = e => {
const t = {};
return (
(function(e, t, n = []) {
Object.keys(t)
.filter(e => !n.includes(e))
.forEach(n => {
Object.defineProperty(e, n, {
get: () => {
const e = t[n];
return Array.isArray(e) ? e[e.length - 1] : e;
}
});
});
})(
t,
e.reduce((e, t) => {
const { route: n } = t,
{ name: r, path: o } = n;
if (e[r])
throw new Error(
`Router config 'name' isn't unique, route name: "${r}", route path: "${o}"`
);
const u = e[o];
if (u) {
const t = u[u.length - 1];
if (!t?.name || t.name !== n.parentAbs?.name)
throw new Error(
`There are routes at the same level with the same path, route name: "${r}", route path: "${o}"`
);
e[o] = [...u, n];
} else e[o] = [n];
return (e[r] = n), e;
}, {})
),
t
);
};
function S(e) {
return e && e.endsWith("/") ? e.slice(0, -1) : e;
}
function D(e = window.location.pathname, n, r) {
let o = n[(e = S(e))];
if (!o) {
const n = r.find(n => !!t.matchPath({ path: n.path }, e));
if (n) return n;
}
return (
o ||
(o = {
_isHasAuth: !0,
_relativePath: "",
_level: 0,
name: "404",
path: e,
title: "404",
meta: {},
component: () => s.createElement(b, { code: "404" }),
_route: {
_level: 0,
_relativePath: "",
name: "404",
path: e,
title: "404",
component: () => s.createElement(b, { code: "404" })
}
}),
o
);
}
function x(e) {
const t = [];
let n = e;
for (; n; ) t.unshift(n), (n = n.parent);
return t;
}
function k(e, t) {
return t.includes(e);
}
function F({ code: e, permissionList: t, hasAuth: n, route: r }) {
return (
!n ||
!e ||
(Array.isArray(e)
? 0 === e.length || e.some(e => k(e, t))
: e instanceof Function
? !!(function(e, t) {
return e(t);
})(e, r)
: k(e, t))
);
}
function H(e) {
const {
routes: n,
permissionList: r = [],
permissionMode: o,
hasAuth: u,
beforeEachMount: a,
parent: i,
parentAbs: c
} = e;
return n.map(e => {
const {
component: n,
code: l = "",
children: f,
items: p,
beforeEnter: m,
redirect: d,
meta: h,
type: R
} = e;
let y = [],
g = [],
E = n;
E || (E = v);
const O = {};
(m || a) &&
((O.beforeEnter = m),
(O.beforeEachMount = a),
(O.key = e.name),
(O._route = e),
(O.Component = E),
(E = P));
const _ = F({ code: l, permissionList: r, hasAuth: u, route: e }),
A = !1 !== i?._isHasAuth && _,
M = {
...e,
parent: i,
parentAbs: c,
props: O,
meta: h || {},
items: [],
children: [],
_route: e,
_isHasAuth: A,
_currentComponent: E,
_currentIsHasAuth: _
};
f &&
(y = H({
routes: f,
permissionList: r,
permissionMode: o,
hasAuth: u,
beforeEachMount: a,
parent: M,
parentAbs: M
})),
p &&
(g = H({
routes: p,
permissionList: r,
permissionMode: o,
hasAuth: u,
beforeEachMount: a,
parent: M,
parentAbs: c
}));
const j = [...g, ...y];
if (!A) {
return {
...M,
children: y,
items: g,
_component: b,
_itemsAndChildren: j
};
}
if (d)
return {
...M,
children: y,
items: g,
_component: () => s.createElement(t.Navigate, { to: d, replace: !0 }),
_itemsAndChildren: j
};
if ("null" === R)
return {
...M,
children: y,
items: g,
_component: void 0,
_itemsAndChildren: j
};
if (E)
return {
...M,
children: y,
items: g,
_component: E,
_itemsAndChildren: j
};
{
const n = q(e, r, u);
if (n) {
let r = !1;
return (
e.parent?.component || (r = !0),
{
...M,
children: y,
items: g,
_component: () =>
s.createElement(t.Navigate, { to: n, replace: r }),
_itemsAndChildren: j
}
);
}
return { ...M, items: [], children: [], _component: b };
}
});
}
function I(e) {
return "/" === e ? e : e.replace(/\/*\**$/gm, "");
}
function $(e) {
if ("string" == typeof e) return I(e);
const { pathname: t } = e;
return t ? { ...e, pathname: I(t) } : e;
}
const q = (e, t, n, r) => {
const { items: o } = e;
if (!o) return "";
if (!n) return o[0].path;
const u = o.find(e => e.index);
if (u) return u.path;
let a = "";
for (let e = 0; e < o?.length; e++) {
const r = o[e],
{ code: u, path: i } = r;
if (!u) {
a = i;
break;
}
if (F({ code: u, permissionList: t, hasAuth: n, route: r })) {
a = i;
break;
}
}
return a;
};
function B(e) {
return "string" == typeof e;
}
let W = 0;
function V(e) {
return e
? e.reduce((e, t) => e.concat(Array.isArray(t) ? V(t) : t), [])
: [];
}
function z(e) {
const t = {};
return {
inputRoutes: (function e(n, r) {
return n
? n.map(n => {
const o = { ...n, items: [], children: [], parent: r };
return (
(o.items = e(n.items, o)),
(o.children = e(n.children, o)),
(t[n.name] = o),
o
);
}, [])
: [];
})(e),
routesMap: t
};
}
let Y = (function(e) {
return (
(e.UPDATE_INPUT_ROUTES = "UPDATE_INPUT_ROUTES"),
(e.UPDATE_CURRENT_ROUTE = "UPDATE_CURRENT_ROUTE"),
(e.UPDATE_STATE = "UPDATE_STATE"),
(e.ADD_ROUTES = "ADD_ROUTES"),
(e.REMOVE_ROUTES = "REMOVE_ROUTES"),
(e.UPDATE_ROUTES = "UPDATE_ROUTES"),
e
);
})({});
const G = s.createContext({
state: {
inputRoutes: [],
authInputRoutes: [],
permissionList: [],
routesMap: {},
flattenRoutes: []
},
methods: {}
});
function J(e, t) {
const { type: n, payload: r } = t;
switch (n) {
case Y.ADD_ROUTES:
return (function(e, t) {
let n = !1;
const r = t,
{ routesMap: o, inputRoutes: u } = z(e.inputRoutes);
if (
(r.forEach(e => {
const { path: t, name: r, parentName: a } = e;
if (o[t] || o[r])
throw new Error(`新增路由 ${r} ${t} 已经存在,请修改`);
if (a) {
const t = o[a];
if (t) {
const r = j({ routes: [e], parent: t, _level: t._level + 1 });
(t.items = t.items || []), t.items.push(r[0]), (n = !0);
}
} else {
const t = j({ routes: [e], _level: 0 });
u.push(t[0]), (n = !0);
}
}),
n)
) {
const t = e._getNewStateByNewInputRoutes(u);
return { ...e, ...t, inputRoutes: [...u] };
}
return e;
})(e, r);
case Y.REMOVE_ROUTES:
return (function(e, t) {
let n = !1;
const r = t,
{ routesMap: o, inputRoutes: u } = z(e.inputRoutes);
if (
(r.forEach(e => {
const t = o[e];
if (t) {
const e = t.parent;
if (!e) {
const e = u.indexOf(t);
e > -1 && (u.splice(e, 1), (n = !0));
}
if (e && e.items) {
const r = e.items.indexOf(t);
r > -1 && (e.items.splice(r, 1), (n = !0));
}
}
}),
n)
) {
const t = e._getNewStateByNewInputRoutes(u);
return { ...e, ...t, inputRoutes: [...u] };
}
return e;
})(e, r);
case Y.UPDATE_ROUTES:
return (function(e, t) {
let n = !1;
const r = t,
{ routesMap: o, inputRoutes: u } = z(e.inputRoutes);
if (
(r.forEach(({ routeName: t, routeData: r }) => {
const u = o[t];
if (u) {
const t = u.parent,
o = j({
routes: [{ ...u, ...r }],
parent: t,
_level: (t?._level || 0) + 1,
basename: e.basename
});
t || (Object.assign(u, o[0]), (n = !0)),
t && t.items
? (t.items.splice(t.items.indexOf(u), 1, o[0]), (n = !0))
: t &&
t.children &&
(t.children.splice(t.children.indexOf(u), 1, o[0]),
(n = !0));
}
}),
n)
) {
const t = e._getNewStateByNewInputRoutes(u);
return { ...e, ...t, inputRoutes: [...u] };
}
return e;
})(e, r);
case Y.UPDATE_INPUT_ROUTES:
return { ...e, inputRoutes: r };
case Y.UPDATE_CURRENT_ROUTE:
return { ...e, currentRoute: r };
case Y.UPDATE_STATE:
return { ...e, ...r };
default:
return { ...e };
}
}
G.displayName = "MRouterContext";
let K = new Map();
function Q(e) {
const t = e.map(e => {
let t,
n = [];
const {
_component: o,
path: u,
items: a,
children: i,
_isHasAuth: c,
props: l
} = e;
if (o) {
const e = g.LoadingComponent;
if (!c)
return {
path: u.endsWith("*") ? u : `${u}/*`,
element: s.createElement(
r.Suspense,
{ fallback: s.createElement(e, null) },
s.createElement(o, l)
)
};
(t = {
path: u,
element: s.createElement(
r.Suspense,
{ fallback: s.createElement(e, null) },
s.createElement(o, l)
)
}),
i && (t.children = Q(i)),
a && (n = Q(a));
}
return [t, ...n].filter(e => void 0 !== e);
});
return V(t);
}
const X = [],
Z = (
{
permissionList: e = X,
permissionMode: t = "parent",
routerConfig: o,
hasAuth: u,
children: a
},
i
) => {
const c = f(),
m = n.useLocation(),
d = r.useRef(m),
h = s.useContext(l).historyMethods,
b = p(),
R = _(),
{
routes: y = [],
basename: g = "/",
beforeEachMount: E,
autoDocumentTitle: P = !1,
_navigateRef: O
} = o;
O && (O.current = R);
const A = r.useMemo(() => j({ routes: y, basename: g }), [g, y]),
v = r.useRef(A),
M = r.useRef(null),
T = r.useMemo(
() => (
M.current ||
(M.current = (function(e) {
const {
inputRoutes: t,
hasAuth: n,
permissionList: r,
permissionMode: o,
beforeEachMount: u,
basename: a,
location: i,
_defineId: s
} = e,
c = K.get(s);
if (c) {
const t = (function(e, t) {
return (
e.basename === t.basename &&
e.hasAuth === t.hasAuth &&
e.beforeEachMount === t.beforeEachMount &&
e.inputRoutes === t.inputRoutes &&
e.permissionList === t.permissionList
);
})(c.queryData, e);
if (t) return c.initialData;
}
const l = C({
inputRoutes: t,
permissionList: r,
permissionMode: o,
hasAuth: n,
beforeEachMount: u,
basename: a,
location: i
});
return K.set(s, { queryData: e, initialData: l }), l;
})({
inputRoutes: A,
permissionList: e,
permissionMode: t,
hasAuth: u,
beforeEachMount: E,
basename: g,
location: d.current,
_defineId: o._defineId
})),
M.current
),
[g, E, u, A, e, t, o._defineId]
),
U = r.useRef(null),
L = r.useCallback(
n =>
C({
inputRoutes: n,
permissionList: e,
permissionMode: t,
hasAuth: u,
beforeEachMount: E,
basename: g,
location: m
}),
[g, E, u, m, e, t]
);
U.current = L;
const [N, w] = r.useReducer(J, {
...T,
inputRoutes: A,
permissionList: e,
permissionMode: t,
hasAuth: u,
basename: g,
_getNewStateByNewInputRoutes: U.current
});
r.useImperativeHandle(i, () => ({
updateCurrentRoute(e) {
const { pathname: t } = e,
n = N.currentRoute,
r = D(t, N.routesMap, N.flattenRoutes);
let o = N.currentPathRoutes;
r !== N.currentRoute && (o = x(r)),
w({
type: Y.UPDATE_STATE,
payload: { currentRoute: r, currentPathRoutes: o, prevRoute: n }
});
}
})),
r.useLayoutEffect(() => {
if (
N.permissionList === e &&
N.permissionMode === t &&
u === N.hasAuth &&
N.inputRoutes === A &&
N.beforeEachMount === E
)
return;
let n = N.inputRoutes;
if (
v.current === A &&
N.permissionList === e &&
N.permissionMode === t &&
u === N.hasAuth &&
N.beforeEachMount === E
)
return;
(v.current = A), (n = A);
const {
authInputRoutes: r,
flattenRoutes: o,
routesMap: a,
currentRoute: i,
currentPathRoutes: s
} = C({
inputRoutes: n,
permissionList: e,
permissionMode: t,
hasAuth: u,
beforeEachMount: E,
basename: g,
location: m
});
w({
type: Y.UPDATE_STATE,
payload: {
permissionList: e,
permissionMode: t,
authInputRoutes: r,
routesMap: a,
flattenRoutes: o,
currentRoute: i,
currentPathRoutes: s,
basename: g,
beforeEachMount: E,
inputRoutes: n
}
});
}, [
N.inputRoutes,
A,
e,
N.permissionList,
t,
N.permissionMode,
u,
N.hasAuth,
g,
E,
m,
N.beforeEachMount
]),
r.useEffect(() => {
if (!P) return;
let e = "";
"boolean" == typeof P
? (e = N.currentPathRoutes.map(e => e.title).join("-"))
: "function" == typeof P && (e = P(N.currentPathRoutes)),
(document.title = e);
}, [P, N.currentPathRoutes]);
const S = r.useCallback(
(e, t) => {
"string" != typeof t && (t = t?.pathname);
const n = window.location.pathname,
r = (function(e, t, n) {
return n.filter(n => n.name === e && n.pathname === t);
})("BeforeRouterLeave", n, b.current);
if (
(N.currentRoute?.beforeLeave &&
r.unshift({
name: "BeforeRouterLeave",
pathname: N.currentRoute.path,
fn: N.currentRoute.beforeLeave
}),
!r.length)
)
return e();
!(function(e) {
const { to: t, from: n, callbacks: r, finish: o } = e;
0 === r.length
? o()
: (function e(u = 0) {
const a = r[u];
if (!a) return o();
a.fn(t, n, () => {
e(u + 1);
});
})(0);
})({
to: D(t, N.routesMap, N.flattenRoutes),
from: D(n, N.routesMap, N.flattenRoutes),
callbacks: r,
finish: () => e()
});
},
[
b,
N.currentRoute.beforeLeave,
N.currentRoute.path,
N.routesMap,
N.flattenRoutes
]
);
r.useLayoutEffect(() => {
!(function(e, t, n) {
(e.go = e => {
t(() => n.go(e));
}),
(e.push = (e, r) => {
(e = $(e)), t(() => n.push(e, r), e);
}),
(e.replace = (e, r) => {
(e = $(e)), t(() => n.replace(e, r), e);
}),
(e.back = () => {
t(() => n.go(-1));
}),
(e.forward = () => {
t(() => n.go(1));
});
})(c, S, h);
}, [S, c, h]);
const k = r.useCallback(e => {
w({ type: Y.ADD_ROUTES, payload: e });
}, []),
F = r.useCallback(e => {
w({ type: Y.REMOVE_ROUTES, payload: e });
}, []),
H = r.useCallback(e => {
w({ type: Y.UPDATE_ROUTES, payload: e });
}, []),
I = r.useCallback(e => {
e && w({ type: Y.UPDATE_CURRENT_ROUTE, payload: e });
}, []),
q = r.useMemo(() => Q(N.authInputRoutes), [N.authInputRoutes]),
B = n.useRoutes(q),
W = r.useMemo(() => (a ? a(B) : B), [a, B]);
return s.createElement(
G.Provider,
{
value: {
state: N,
methods: {
addRoutes: k,
updateCurrentRoute: I,
removeRoutes: F,
updateRoutes: H
}
}
},
W
);
},
ee = s.forwardRef(Z);
ee.displayName = "MRouterContextProvider";
const te = ({
permissionList: e,
permissionMode: t,
wrapComponent: n,
hasAuth: o = !0,
routerConfig: u,
children: a,
RouterComponent: i
}) => {
const c = r.useRef(null),
l = r.useCallback(e => {
c.current?.updateCurrentRoute?.(e);
}, []),
f = r.useMemo(
() =>
a && "function" == typeof a
? a
: n
? ((n.displayName = "WrapComponent"),
function(e) {
return s.createElement(n, null, e);
})
: void 0,
[n, a]
);
return s.createElement(
i,
{ syncUpdateCurrentRoute: l },
s.createElement(
ee,
{
routerConfig: u,
permissionList: e,
permissionMode: t,
hasAuth: o,
ref: c
},
f
)
);
};
Object.defineProperty(e, "AbortedDeferredError", {
enumerable: !0,
get: function() {
return t.AbortedDeferredError;
}
}),
Object.defineProperty(e, "Await", {
enumerable: !0,
get: function() {
return t.Await;
}
}),
Object.defineProperty(e, "MemoryRouter", {
enumerable: !0,
get: function() {
return t.MemoryRouter;
}
}),
Object.defineProperty(e, "Navigate", {
enumerable: !0,
get: function() {
return t.Navigate;
}
}),
Object.defineProperty(e, "NavigationType", {
enumerable: !0,
get: function() {
return t.NavigationType;
}
}),
Object.defineProperty(e, "Outlet", {
enumerable: !0,
get: function() {
return t.Outlet;
}
}),
Object.defineProperty(e, "Route", {
enumerable: !0,
get: function() {
return t.Route;
}
}),
Object.defineProperty(e, "Router", {
enumerable: !0,
get: function() {
return t.Router;
}
}),
Object.defineProperty(e, "RouterProvider", {
enumerable: !0,
get: function() {
return t.RouterProvider;
}
}),
Object.defineProperty(e, "Routes", {
enumerable: !0,
get: function() {
return t.Routes;
}
}),
Object.defineProperty(e, "UNSAFE_DataRouterContext", {
enumerable: !0,
get: function() {
return t.UNSAFE_DataRouterContext;
}
}),
Object.defineProperty(e, "UNSAFE_DataRouterStateContext", {
enumerable: !0,
get: function() {
return t.UNSAFE_DataRouterStateContext;
}
}),
Object.defineProperty(e, "UNSAFE_LocationContext", {
enumerable: !0,
get: function() {
return t.UNSAFE_LocationContext;
}
}),
Object.defineProperty(e, "UNSAFE_NavigationContext", {
enumerable: !0,
get: function() {
return t.UNSAFE_NavigationContext;
}
}),
Object.defineProperty(e, "UNSAFE_RouteContext", {
enumerable: !0,
get: function() {
return t.UNSAFE_RouteContext;
}
}),
Object.defineProperty(e, "createMemoryRouter", {
enumerable: !0,
get: function() {
return t.createMemoryRouter;
}
}),
Object.defineProperty(e, "createPath", {
enumerable: !0,
get: function() {
return t.createPath;
}
}),
Object.defineProperty(e, "createRoutesFromChildren", {
enumerable: !0,
get: function() {
return t.createRoutesFromChildren;
}
}),
Object.defineProperty(e, "createRoutesFromElements", {
enumerable: !0,
get: function() {
return t.createRoutesFromElements;
}
}),
Object.defineProperty(e, "defer", {
enumerable: !0,
get: function() {
return t.defer;
}
}),
Object.defineProperty(e, "generatePath", {
enumerable: !0,
get: function() {
return t.generatePath;
}
}),
Object.defineProperty(e, "isRouteErrorResponse", {
enumerable: !0,
get: function() {
return t.isRouteErrorResponse;
}
}),
Object.defineProperty(e, "json", {
enumerable: !0,
get: function() {
return t.json;
}
}),
Object.defineProperty(e, "matchPath", {
enumerable: !0,
get: function() {
return t.matchPath;
}
}),
Object.defineProperty(e, "matchRoutes", {
enumerable: !0,
get: function() {
return t.matchRoutes;
}
}),
Object.defineProperty(e, "parsePath", {
enumerable: !0,
get: function() {
return t.parsePath;
}
}),
Object.defineProperty(e, "redirect", {
enumerable: !0,
get: function() {
return t.redirect;
}
}),
Object.defineProperty(e, "renderMatches", {
enumerable: !0,
get: function() {
return t.renderMatches;
}
}),
Object.defineProperty(e, "resolvePath", {
enumerable: !0,
get: function() {
return t.resolvePath;
}
}),
Object.defineProperty(e, "unstable_useBlocker", {
enumerable: !0,
get: function() {
return t.unstable_useBlocker;
}
}),
Object.defineProperty(e, "useActionData", {
enumerable: !0,
get: function() {
return t.useActionData;
}
}),
Object.defineProperty(e, "useAsyncError", {
enumerable: !0,
get: function() {
return t.useAsyncError;
}
}),
Object.defineProperty(e, "useAsyncValue", {
enumerable: !0,
get: function() {
return t.useAsyncValue;
}
}),
Object.defineProperty(e, "useHref", {
enumerable: !0,
get: function() {
return t.useHref;
}
}),
Object.defineProperty(e, "useInRouterContext", {
enumerable: !0,
get: function() {
return t.useInRouterContext;
}
}),
Object.defineProperty(e, "useLoaderData", {
enumerable: !0,
get: function() {
return t.useLoaderData;
}
}),
Object.defineProperty(e, "useLocation", {
enumerable: !0,
get: function() {
return t.useLocation;
}
}),
Object.defineProperty(e, "useMatch", {
enumerable: !0,
get: function() {
return t.useMatch;
}
}),
Object.defineProperty(e, "useMatches", {
enumerable: !0,
get: function() {
return t.useMatches;
}
}),
Object.defineProperty(e, "useNavigation", {
enumerable: !0,
get: function() {
return t.useNavigation;
}
}),
Object.defineProperty(e, "useNavigationType", {
enumerable: !0,
get: function() {
return t.useNavigationType;
}
}),
Object.defineProperty(e, "useOutlet", {
enumerable: !0,
get: function() {
return t.useOutlet;
}
}),
Object.defineProperty(e, "useOutletContext", {
enumerable: !0,
get: function() {
return t.useOutletContext;
}
}),
Object.defineProperty(e, "useParams", {
enumerable: !0,
get: function() {
return t.useParams;
}
}),
Object.defineProperty(e, "useResolvedPath", {
enumerable: !0,
get: function() {
return t.useResolvedPath;
}
}),
Object.defineProperty(e, "useRevalidator", {
enumerable: !0,
get: function() {
return t.useRevalidator;
}
}),
Object.defineProperty(e, "useRouteError", {
enumerable: !0,
get: function() {
return t.useRouteError;
}
}),
Object.defineProperty(e, "useRouteLoaderData", {
enumerable: !0,
get: function() {
return t.useRouteLoaderData;
}
}),
Object.defineProperty(e, "useRoutes", {
enumerable: !0,
get: function() {
return t.useRoutes;
}
}),
Object.defineProperty(e, "Form", {
enumerable: !0,
get: function() {
return n.Form;
}
}),
Object.defineProperty(e, "Link", {
enumerable: !0,
get: function() {
return n.Link;
}
}),
Object.defineProperty(e, "NavLink", {
enumerable: !0,
get: function() {
return n.NavLink;
}
}),
Object.defineProperty(e, "ScrollRestoration", {
enumerable: !0,
get: function() {
return n.ScrollRestoration;
}
}),
Object.defineProperty(e, "UNSAFE_useScrollRestoration", {
enumerable: !0,
get: function() {
return n.UNSAFE_useScrollRestoration;
}
}),
Object.defineProperty(e, "createBrowserRouter", {
enumerable: !0,
get: function() {
return n.createBrowserRouter;
}
}),
Object.defineProperty(e, "createHashRouter", {
enumerable: !0,
get: function() {
return n.createHashRouter;
}
}),
Object.defineProperty(e, "useBeforeUnload", {
enumerable: !0,
get: function() {
return n.useBeforeUnload;
}
}),
Object.defineProperty(e, "useFetcher", {
enumerable: !0,
get: function() {
return n.useFetcher;
}
}),
Object.defineProperty(e, "useFetchers", {
enumerable: !0,
get: function() {
return n.useFetchers;
}
}),
Object.defineProperty(e, "useFormAction", {
enumerable: !0,
get: function() {
return n.useFormAction;
}
}),
Object.defineProperty(e, "useLinkClickHandler", {
enumerable: !0,
get: function() {
return n.useLinkClickHandler;
}
}),
Object.defineProperty(e, "useSearchParams", {
enumerable: !0,
get: function() {
return n.useSearchParams;
}
}),
Object.defineProperty(e, "useSubmit", {
enumerable: !0,
get: function() {
return n.useSubmit;
}
}),
(e.MHRouter = e => s.createElement(te, c({}, e, { RouterComponent: d }))),
(e.MRouter = e => s.createElement(te, c({}, e, { RouterComponent: m }))),
(e.defineRouterConfig = function(e) {
const t = { current: null },
{ LoadingComponent: n, ...r } = e;
var o;
(W += 1),
n &&
((o = { LoadingComponent: n }),
Object.entries(o).forEach(([e, t]) => {
g[e] = t;
}));
const u = {
...r,
_isDefined: !0,
_defineId: W,
navigate: () => {
throw new Error("YSRouter navigate is not initialized");
},
_navigateRef: t
};
return (
Object.defineProperty(u, "navigate", {
get: () =>
t.current ||
(() => {
throw new Error("YSRouter navigate is not initialized");
})
}),
u
);
}),
(e.useAddRoutes = function() {
return s.useContext(G).methods.addRoutes;
}),
(e.useBeforeLeave = function(e, t) {
const n = window.location.pathname,
o = p(),
u = t?.beforeunload,
a = r.useRef(u);
(a.current = u),
r.useLayoutEffect(() => {
const t = o.current,
r = { name: "BeforeRouterLeave", pathname: n, fn: e };
t.push(r);
const u = a.current,
i = e => u?.(e);
return (
u && window.addEventListener("beforeunload", i),
() => {
const e = t.indexOf(r);
t.splice(e, 1),
u && window.removeEventListener("beforeunload", i);
}
);
}, [e, n, o]);
}),
(e.useHistory = f),
(e.useNavigate = _),
(e.useRemoveRoutes = function() {
return s.useContext(G).methods.removeRoutes;
}),
(e.useRouter = A),
(e.useUpdateRoutes = function() {
return s.useContext(G).methods.updateRoutes;
}),
Object.defineProperty(e, "__esModule", { value: !0 });
});