1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | import { invariant, resolveTo, joinPaths, matchPath, warning, parsePath, matchRoutes, Action, isRouteErrorResponse, createMemoryHistory, stripBasename, AbortedDeferredError, createRouter } from '@remix-run/router';
|
12 | export { AbortedDeferredError, Action as NavigationType, createPath, defer, generatePath, isRouteErrorResponse, json, matchPath, matchRoutes, parsePath, redirect, resolvePath } from '@remix-run/router';
|
13 | import * as React from 'react';
|
14 |
|
15 | function _extends() {
|
16 | _extends = Object.assign ? Object.assign.bind() : function (target) {
|
17 | for (var i = 1; i < arguments.length; i++) {
|
18 | var source = arguments[i];
|
19 |
|
20 | for (var key in source) {
|
21 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
22 | target[key] = source[key];
|
23 | }
|
24 | }
|
25 | }
|
26 |
|
27 | return target;
|
28 | };
|
29 | return _extends.apply(this, arguments);
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function isPolyfill(x, y) {
|
44 | return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y
|
45 | ;
|
46 | }
|
47 |
|
48 | const is = typeof Object.is === "function" ? Object.is : isPolyfill;
|
49 |
|
50 |
|
51 | const {
|
52 | useState,
|
53 | useEffect,
|
54 | useLayoutEffect,
|
55 | useDebugValue
|
56 | } = React;
|
57 | let didWarnOld18Alpha = false;
|
58 | let didWarnUncachedGetSnapshot = false;
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function useSyncExternalStore$2(subscribe, getSnapshot, // Note: The shim does not use getServerSnapshot, because pre-18 versions of
|
70 | // React do not expose a way to check if we're hydrating. So users of the shim
|
71 |
|
72 |
|
73 | getServerSnapshot) {
|
74 | if (process.env.NODE_ENV !== "production") {
|
75 | if (!didWarnOld18Alpha) {
|
76 | if ("startTransition" in React) {
|
77 | didWarnOld18Alpha = true;
|
78 | console.error("You are using an outdated, pre-release alpha of React 18 that " + "does not support useSyncExternalStore. The " + "use-sync-external-store shim will not work correctly. Upgrade " + "to a newer pre-release.");
|
79 | }
|
80 | }
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | const value = getSnapshot();
|
88 |
|
89 | if (process.env.NODE_ENV !== "production") {
|
90 | if (!didWarnUncachedGetSnapshot) {
|
91 | const cachedValue = getSnapshot();
|
92 |
|
93 | if (!is(value, cachedValue)) {
|
94 | console.error("The result of getSnapshot should be cached to avoid an infinite loop");
|
95 | didWarnUncachedGetSnapshot = true;
|
96 | }
|
97 | }
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | const [{
|
115 | inst
|
116 | }, forceUpdate] = useState({
|
117 | inst: {
|
118 | value,
|
119 | getSnapshot
|
120 | }
|
121 | });
|
122 |
|
123 |
|
124 |
|
125 | useLayoutEffect(() => {
|
126 | inst.value = value;
|
127 | inst.getSnapshot = getSnapshot;
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | if (checkIfSnapshotChanged(inst)) {
|
133 |
|
134 | forceUpdate({
|
135 | inst
|
136 | });
|
137 | }
|
138 |
|
139 | }, [subscribe, value, getSnapshot]);
|
140 | useEffect(() => {
|
141 |
|
142 |
|
143 | if (checkIfSnapshotChanged(inst)) {
|
144 |
|
145 | forceUpdate({
|
146 | inst
|
147 | });
|
148 | }
|
149 |
|
150 | const handleStoreChange = () => {
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | if (checkIfSnapshotChanged(inst)) {
|
158 |
|
159 | forceUpdate({
|
160 | inst
|
161 | });
|
162 | }
|
163 | };
|
164 |
|
165 |
|
166 | return subscribe(handleStoreChange);
|
167 | }, [subscribe]);
|
168 | useDebugValue(value);
|
169 | return value;
|
170 | }
|
171 |
|
172 | function checkIfSnapshotChanged(inst) {
|
173 | const latestGetSnapshot = inst.getSnapshot;
|
174 | const prevValue = inst.value;
|
175 |
|
176 | try {
|
177 | const nextValue = latestGetSnapshot();
|
178 | return !is(prevValue, nextValue);
|
179 | } catch (error) {
|
180 | return true;
|
181 | }
|
182 | }
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | function useSyncExternalStore$1(subscribe, getSnapshot, getServerSnapshot) {
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | return getSnapshot();
|
198 | }
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | const canUseDOM = !!(typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined");
|
206 | const isServerEnvironment = !canUseDOM;
|
207 | const shim = isServerEnvironment ? useSyncExternalStore$1 : useSyncExternalStore$2;
|
208 | const useSyncExternalStore = "useSyncExternalStore" in React ? (module => module.useSyncExternalStore)(React) : shim;
|
209 |
|
210 |
|
211 | const DataStaticRouterContext = React.createContext(null);
|
212 |
|
213 | if (process.env.NODE_ENV !== "production") {
|
214 | DataStaticRouterContext.displayName = "DataStaticRouterContext";
|
215 | }
|
216 |
|
217 | const DataRouterContext = React.createContext(null);
|
218 |
|
219 | if (process.env.NODE_ENV !== "production") {
|
220 | DataRouterContext.displayName = "DataRouter";
|
221 | }
|
222 |
|
223 | const DataRouterStateContext = React.createContext(null);
|
224 |
|
225 | if (process.env.NODE_ENV !== "production") {
|
226 | DataRouterStateContext.displayName = "DataRouterState";
|
227 | }
|
228 |
|
229 | const AwaitContext = React.createContext(null);
|
230 |
|
231 | if (process.env.NODE_ENV !== "production") {
|
232 | AwaitContext.displayName = "Await";
|
233 | }
|
234 |
|
235 | const NavigationContext = React.createContext(null);
|
236 |
|
237 | if (process.env.NODE_ENV !== "production") {
|
238 | NavigationContext.displayName = "Navigation";
|
239 | }
|
240 |
|
241 | const LocationContext = React.createContext(null);
|
242 |
|
243 | if (process.env.NODE_ENV !== "production") {
|
244 | LocationContext.displayName = "Location";
|
245 | }
|
246 |
|
247 | const RouteContext = React.createContext({
|
248 | outlet: null,
|
249 | matches: []
|
250 | });
|
251 |
|
252 | if (process.env.NODE_ENV !== "production") {
|
253 | RouteContext.displayName = "Route";
|
254 | }
|
255 |
|
256 | const RouteErrorContext = React.createContext(null);
|
257 |
|
258 | if (process.env.NODE_ENV !== "production") {
|
259 | RouteErrorContext.displayName = "RouteError";
|
260 | }
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | function useHref(to, _temp) {
|
270 | let {
|
271 | relative
|
272 | } = _temp === void 0 ? {} : _temp;
|
273 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
274 |
|
275 | "useHref() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
276 | let {
|
277 | basename,
|
278 | navigator
|
279 | } = React.useContext(NavigationContext);
|
280 | let {
|
281 | hash,
|
282 | pathname,
|
283 | search
|
284 | } = useResolvedPath(to, {
|
285 | relative
|
286 | });
|
287 | let joinedPathname = pathname;
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | if (basename !== "/") {
|
293 | joinedPathname = pathname === "/" ? basename : joinPaths([basename, pathname]);
|
294 | }
|
295 |
|
296 | return navigator.createHref({
|
297 | pathname: joinedPathname,
|
298 | search,
|
299 | hash
|
300 | });
|
301 | }
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 | function useInRouterContext() {
|
309 | return React.useContext(LocationContext) != null;
|
310 | }
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | function useLocation() {
|
323 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
324 |
|
325 | "useLocation() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
326 | return React.useContext(LocationContext).location;
|
327 | }
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | function useNavigationType() {
|
336 | return React.useContext(LocationContext).navigationType;
|
337 | }
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | function useMatch(pattern) {
|
347 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
348 |
|
349 | "useMatch() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
350 | let {
|
351 | pathname
|
352 | } = useLocation();
|
353 | return React.useMemo(() => matchPath(pattern, pathname), [pathname, pattern]);
|
354 | }
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 | function getPathContributingMatches(matches) {
|
381 | return matches.filter((match, index) => index === 0 || !match.route.index && match.pathnameBase !== matches[index - 1].pathnameBase);
|
382 | }
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 | function useNavigate() {
|
392 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
393 |
|
394 | "useNavigate() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
395 | let {
|
396 | basename,
|
397 | navigator
|
398 | } = React.useContext(NavigationContext);
|
399 | let {
|
400 | matches
|
401 | } = React.useContext(RouteContext);
|
402 | let {
|
403 | pathname: locationPathname
|
404 | } = useLocation();
|
405 | let routePathnamesJson = JSON.stringify(getPathContributingMatches(matches).map(match => match.pathnameBase));
|
406 | let activeRef = React.useRef(false);
|
407 | React.useEffect(() => {
|
408 | activeRef.current = true;
|
409 | });
|
410 | let navigate = React.useCallback(function (to, options) {
|
411 | if (options === void 0) {
|
412 | options = {};
|
413 | }
|
414 |
|
415 | process.env.NODE_ENV !== "production" ? warning(activeRef.current, "You should call navigate() in a React.useEffect(), not when " + "your component is first rendered.") : void 0;
|
416 | if (!activeRef.current) return;
|
417 |
|
418 | if (typeof to === "number") {
|
419 | navigator.go(to);
|
420 | return;
|
421 | }
|
422 |
|
423 | let path = resolveTo(to, JSON.parse(routePathnamesJson), locationPathname, options.relative === "path");
|
424 |
|
425 |
|
426 |
|
427 |
|
428 | if (basename !== "/") {
|
429 | path.pathname = path.pathname === "/" ? basename : joinPaths([basename, path.pathname]);
|
430 | }
|
431 |
|
432 | (!!options.replace ? navigator.replace : navigator.push)(path, options.state, options);
|
433 | }, [basename, navigator, routePathnamesJson, locationPathname]);
|
434 | return navigate;
|
435 | }
|
436 | const OutletContext = React.createContext(null);
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 | function useOutletContext() {
|
444 | return React.useContext(OutletContext);
|
445 | }
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 | function useOutlet(context) {
|
454 | let outlet = React.useContext(RouteContext).outlet;
|
455 |
|
456 | if (outlet) {
|
457 | return React.createElement(OutletContext.Provider, {
|
458 | value: context
|
459 | }, outlet);
|
460 | }
|
461 |
|
462 | return outlet;
|
463 | }
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 | function useParams() {
|
472 | let {
|
473 | matches
|
474 | } = React.useContext(RouteContext);
|
475 | let routeMatch = matches[matches.length - 1];
|
476 | return routeMatch ? routeMatch.params : {};
|
477 | }
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 | function useResolvedPath(to, _temp2) {
|
485 | let {
|
486 | relative
|
487 | } = _temp2 === void 0 ? {} : _temp2;
|
488 | let {
|
489 | matches
|
490 | } = React.useContext(RouteContext);
|
491 | let {
|
492 | pathname: locationPathname
|
493 | } = useLocation();
|
494 | let routePathnamesJson = JSON.stringify(getPathContributingMatches(matches).map(match => match.pathnameBase));
|
495 | return React.useMemo(() => resolveTo(to, JSON.parse(routePathnamesJson), locationPathname, relative === "path"), [to, routePathnamesJson, locationPathname, relative]);
|
496 | }
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 | function useRoutes(routes, locationArg) {
|
507 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
508 |
|
509 | "useRoutes() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
510 | let dataRouterStateContext = React.useContext(DataRouterStateContext);
|
511 | let {
|
512 | matches: parentMatches
|
513 | } = React.useContext(RouteContext);
|
514 | let routeMatch = parentMatches[parentMatches.length - 1];
|
515 | let parentParams = routeMatch ? routeMatch.params : {};
|
516 | let parentPathname = routeMatch ? routeMatch.pathname : "/";
|
517 | let parentPathnameBase = routeMatch ? routeMatch.pathnameBase : "/";
|
518 | let parentRoute = routeMatch && routeMatch.route;
|
519 |
|
520 | if (process.env.NODE_ENV !== "production") {
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 | let parentPath = parentRoute && parentRoute.path || "";
|
542 | warningOnce(parentPathname, !parentRoute || parentPath.endsWith("*"), "You rendered descendant <Routes> (or called `useRoutes()`) at " + ("\"" + parentPathname + "\" (under <Route path=\"" + parentPath + "\">) but the ") + "parent route path has no trailing \"*\". This means if you navigate " + "deeper, the parent won't match anymore and therefore the child " + "routes will never render.\n\n" + ("Please change the parent <Route path=\"" + parentPath + "\"> to <Route ") + ("path=\"" + (parentPath === "/" ? "*" : parentPath + "/*") + "\">."));
|
543 | }
|
544 |
|
545 | let locationFromContext = useLocation();
|
546 | let location;
|
547 |
|
548 | if (locationArg) {
|
549 | var _parsedLocationArg$pa;
|
550 |
|
551 | let parsedLocationArg = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
|
552 | !(parentPathnameBase === "/" || ((_parsedLocationArg$pa = parsedLocationArg.pathname) == null ? void 0 : _parsedLocationArg$pa.startsWith(parentPathnameBase))) ? process.env.NODE_ENV !== "production" ? invariant(false, "When overriding the location using `<Routes location>` or `useRoutes(routes, location)`, " + "the location pathname must begin with the portion of the URL pathname that was " + ("matched by all parent routes. The current pathname base is \"" + parentPathnameBase + "\" ") + ("but pathname \"" + parsedLocationArg.pathname + "\" was given in the `location` prop.")) : invariant(false) : void 0;
|
553 | location = parsedLocationArg;
|
554 | } else {
|
555 | location = locationFromContext;
|
556 | }
|
557 |
|
558 | let pathname = location.pathname || "/";
|
559 | let remainingPathname = parentPathnameBase === "/" ? pathname : pathname.slice(parentPathnameBase.length) || "/";
|
560 | let matches = matchRoutes(routes, {
|
561 | pathname: remainingPathname
|
562 | });
|
563 |
|
564 | if (process.env.NODE_ENV !== "production") {
|
565 | process.env.NODE_ENV !== "production" ? warning(parentRoute || matches != null, "No routes matched location \"" + location.pathname + location.search + location.hash + "\" ") : void 0;
|
566 | process.env.NODE_ENV !== "production" ? warning(matches == null || matches[matches.length - 1].route.element !== undefined, "Matched leaf route at location \"" + location.pathname + location.search + location.hash + "\" does not have an element. " + "This means it will render an <Outlet /> with a null value by default resulting in an \"empty\" page.") : void 0;
|
567 | }
|
568 |
|
569 | let renderedMatches = _renderMatches(matches && matches.map(match => Object.assign({}, match, {
|
570 | params: Object.assign({}, parentParams, match.params),
|
571 | pathname: joinPaths([parentPathnameBase, match.pathname]),
|
572 | pathnameBase: match.pathnameBase === "/" ? parentPathnameBase : joinPaths([parentPathnameBase, match.pathnameBase])
|
573 | })), parentMatches, dataRouterStateContext || undefined);
|
574 |
|
575 |
|
576 |
|
577 |
|
578 | if (locationArg) {
|
579 | return React.createElement(LocationContext.Provider, {
|
580 | value: {
|
581 | location: _extends({
|
582 | pathname: "/",
|
583 | search: "",
|
584 | hash: "",
|
585 | state: null,
|
586 | key: "default"
|
587 | }, location),
|
588 | navigationType: Action.Pop
|
589 | }
|
590 | }, renderedMatches);
|
591 | }
|
592 |
|
593 | return renderedMatches;
|
594 | }
|
595 |
|
596 | function DefaultErrorElement() {
|
597 | let error = useRouteError();
|
598 | let message = isRouteErrorResponse(error) ? error.status + " " + error.statusText : error instanceof Error ? error.message : JSON.stringify(error);
|
599 | let stack = error instanceof Error ? error.stack : null;
|
600 | let lightgrey = "rgba(200,200,200, 0.5)";
|
601 | let preStyles = {
|
602 | padding: "0.5rem",
|
603 | backgroundColor: lightgrey
|
604 | };
|
605 | let codeStyles = {
|
606 | padding: "2px 4px",
|
607 | backgroundColor: lightgrey
|
608 | };
|
609 | return React.createElement(React.Fragment, null, React.createElement("h2", null, "Unhandled Thrown Error!"), React.createElement("h3", {
|
610 | style: {
|
611 | fontStyle: "italic"
|
612 | }
|
613 | }, message), stack ? React.createElement("pre", {
|
614 | style: preStyles
|
615 | }, stack) : null, React.createElement("p", null, "\uD83D\uDCBF Hey developer \uD83D\uDC4B"), React.createElement("p", null, "You can provide a way better UX than this when your app throws errors by providing your own\xA0", React.createElement("code", {
|
616 | style: codeStyles
|
617 | }, "errorElement"), " props on\xA0", React.createElement("code", {
|
618 | style: codeStyles
|
619 | }, "<Route>")));
|
620 | }
|
621 |
|
622 | class RenderErrorBoundary extends React.Component {
|
623 | constructor(props) {
|
624 | super(props);
|
625 | this.state = {
|
626 | location: props.location,
|
627 | error: props.error
|
628 | };
|
629 | }
|
630 |
|
631 | static getDerivedStateFromError(error) {
|
632 | return {
|
633 | error: error
|
634 | };
|
635 | }
|
636 |
|
637 | static getDerivedStateFromProps(props, state) {
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 | if (state.location !== props.location) {
|
647 | return {
|
648 | error: props.error,
|
649 | location: props.location
|
650 | };
|
651 | }
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 | return {
|
658 | error: props.error || state.error,
|
659 | location: state.location
|
660 | };
|
661 | }
|
662 |
|
663 | componentDidCatch(error, errorInfo) {
|
664 | console.error("React Router caught the following error during render", error, errorInfo);
|
665 | }
|
666 |
|
667 | render() {
|
668 | return this.state.error ? React.createElement(RouteErrorContext.Provider, {
|
669 | value: this.state.error,
|
670 | children: this.props.component
|
671 | }) : this.props.children;
|
672 | }
|
673 |
|
674 | }
|
675 |
|
676 | function RenderedRoute(_ref) {
|
677 | let {
|
678 | routeContext,
|
679 | match,
|
680 | children
|
681 | } = _ref;
|
682 | let dataStaticRouterContext = React.useContext(DataStaticRouterContext);
|
683 |
|
684 |
|
685 | if (dataStaticRouterContext && match.route.errorElement) {
|
686 | dataStaticRouterContext._deepestRenderedBoundaryId = match.route.id;
|
687 | }
|
688 |
|
689 | return React.createElement(RouteContext.Provider, {
|
690 | value: routeContext
|
691 | }, children);
|
692 | }
|
693 |
|
694 | function _renderMatches(matches, parentMatches, dataRouterState) {
|
695 | if (parentMatches === void 0) {
|
696 | parentMatches = [];
|
697 | }
|
698 |
|
699 | if (matches == null) {
|
700 | if (dataRouterState != null && dataRouterState.errors) {
|
701 |
|
702 |
|
703 | matches = dataRouterState.matches;
|
704 | } else {
|
705 | return null;
|
706 | }
|
707 | }
|
708 |
|
709 | let renderedMatches = matches;
|
710 |
|
711 | let errors = dataRouterState == null ? void 0 : dataRouterState.errors;
|
712 |
|
713 | if (errors != null) {
|
714 | let errorIndex = renderedMatches.findIndex(m => m.route.id && (errors == null ? void 0 : errors[m.route.id]));
|
715 | !(errorIndex >= 0) ? process.env.NODE_ENV !== "production" ? invariant(false, "Could not find a matching route for the current errors: " + errors) : invariant(false) : void 0;
|
716 | renderedMatches = renderedMatches.slice(0, Math.min(renderedMatches.length, errorIndex + 1));
|
717 | }
|
718 |
|
719 | return renderedMatches.reduceRight((outlet, match, index) => {
|
720 | let error = match.route.id ? errors == null ? void 0 : errors[match.route.id] : null;
|
721 |
|
722 | let errorElement = dataRouterState ? match.route.errorElement || React.createElement(DefaultErrorElement, null) : null;
|
723 |
|
724 | let getChildren = () => React.createElement(RenderedRoute, {
|
725 | match: match,
|
726 | routeContext: {
|
727 | outlet,
|
728 | matches: parentMatches.concat(renderedMatches.slice(0, index + 1))
|
729 | }
|
730 | }, error ? errorElement : match.route.element !== undefined ? match.route.element : outlet);
|
731 |
|
732 |
|
733 |
|
734 |
|
735 | return dataRouterState && (match.route.errorElement || index === 0) ? React.createElement(RenderErrorBoundary, {
|
736 | location: dataRouterState.location,
|
737 | component: errorElement,
|
738 | error: error,
|
739 | children: getChildren()
|
740 | }) : getChildren();
|
741 | }, null);
|
742 | }
|
743 | var DataRouterHook;
|
744 |
|
745 | (function (DataRouterHook) {
|
746 | DataRouterHook["UseLoaderData"] = "useLoaderData";
|
747 | DataRouterHook["UseActionData"] = "useActionData";
|
748 | DataRouterHook["UseRouteError"] = "useRouteError";
|
749 | DataRouterHook["UseNavigation"] = "useNavigation";
|
750 | DataRouterHook["UseRouteLoaderData"] = "useRouteLoaderData";
|
751 | DataRouterHook["UseMatches"] = "useMatches";
|
752 | DataRouterHook["UseRevalidator"] = "useRevalidator";
|
753 | })(DataRouterHook || (DataRouterHook = {}));
|
754 |
|
755 | function useDataRouterState(hookName) {
|
756 | let state = React.useContext(DataRouterStateContext);
|
757 | !state ? process.env.NODE_ENV !== "production" ? invariant(false, hookName + " must be used within a DataRouterStateContext") : invariant(false) : void 0;
|
758 | return state;
|
759 | }
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 | function useNavigation() {
|
767 | let state = useDataRouterState(DataRouterHook.UseNavigation);
|
768 | return state.navigation;
|
769 | }
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 | function useRevalidator() {
|
776 | let dataRouterContext = React.useContext(DataRouterContext);
|
777 | !dataRouterContext ? process.env.NODE_ENV !== "production" ? invariant(false, "useRevalidator must be used within a DataRouterContext") : invariant(false) : void 0;
|
778 | let state = useDataRouterState(DataRouterHook.UseRevalidator);
|
779 | return {
|
780 | revalidate: dataRouterContext.router.revalidate,
|
781 | state: state.revalidation
|
782 | };
|
783 | }
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 | function useMatches() {
|
790 | let {
|
791 | matches,
|
792 | loaderData
|
793 | } = useDataRouterState(DataRouterHook.UseMatches);
|
794 | return React.useMemo(() => matches.map(match => {
|
795 | let {
|
796 | pathname,
|
797 | params
|
798 | } = match;
|
799 |
|
800 |
|
801 |
|
802 | return {
|
803 | id: match.route.id,
|
804 | pathname,
|
805 | params,
|
806 | data: loaderData[match.route.id],
|
807 | handle: match.route.handle
|
808 | };
|
809 | }), [matches, loaderData]);
|
810 | }
|
811 |
|
812 |
|
813 |
|
814 |
|
815 | function useLoaderData() {
|
816 | let state = useDataRouterState(DataRouterHook.UseLoaderData);
|
817 | let route = React.useContext(RouteContext);
|
818 | !route ? process.env.NODE_ENV !== "production" ? invariant(false, "useLoaderData must be used inside a RouteContext") : invariant(false) : void 0;
|
819 | let thisRoute = route.matches[route.matches.length - 1];
|
820 | !thisRoute.route.id ? process.env.NODE_ENV !== "production" ? invariant(false, "useLoaderData can only be used on routes that contain a unique \"id\"") : invariant(false) : void 0;
|
821 | return state.loaderData[thisRoute.route.id];
|
822 | }
|
823 |
|
824 |
|
825 |
|
826 |
|
827 | function useRouteLoaderData(routeId) {
|
828 | let state = useDataRouterState(DataRouterHook.UseRouteLoaderData);
|
829 | return state.loaderData[routeId];
|
830 | }
|
831 |
|
832 |
|
833 |
|
834 |
|
835 | function useActionData() {
|
836 | let state = useDataRouterState(DataRouterHook.UseActionData);
|
837 | let route = React.useContext(RouteContext);
|
838 | !route ? process.env.NODE_ENV !== "production" ? invariant(false, "useActionData must be used inside a RouteContext") : invariant(false) : void 0;
|
839 | return Object.values((state == null ? void 0 : state.actionData) || {})[0];
|
840 | }
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 | function useRouteError() {
|
848 | var _state$errors;
|
849 |
|
850 | let error = React.useContext(RouteErrorContext);
|
851 | let state = useDataRouterState(DataRouterHook.UseRouteError);
|
852 | let route = React.useContext(RouteContext);
|
853 | let thisRoute = route.matches[route.matches.length - 1];
|
854 |
|
855 |
|
856 | if (error) {
|
857 | return error;
|
858 | }
|
859 |
|
860 | !route ? process.env.NODE_ENV !== "production" ? invariant(false, "useRouteError must be used inside a RouteContext") : invariant(false) : void 0;
|
861 | !thisRoute.route.id ? process.env.NODE_ENV !== "production" ? invariant(false, "useRouteError can only be used on routes that contain a unique \"id\"") : invariant(false) : void 0;
|
862 |
|
863 | return (_state$errors = state.errors) == null ? void 0 : _state$errors[thisRoute.route.id];
|
864 | }
|
865 |
|
866 |
|
867 |
|
868 |
|
869 | function useAsyncValue() {
|
870 | let value = React.useContext(AwaitContext);
|
871 | return value == null ? void 0 : value._data;
|
872 | }
|
873 |
|
874 |
|
875 |
|
876 |
|
877 | function useAsyncError() {
|
878 | let value = React.useContext(AwaitContext);
|
879 | return value == null ? void 0 : value._error;
|
880 | }
|
881 | const alreadyWarned = {};
|
882 |
|
883 | function warningOnce(key, cond, message) {
|
884 | if (!cond && !alreadyWarned[key]) {
|
885 | alreadyWarned[key] = true;
|
886 | process.env.NODE_ENV !== "production" ? warning(false, message) : void 0;
|
887 | }
|
888 | }
|
889 |
|
890 |
|
891 |
|
892 |
|
893 | function RouterProvider(_ref) {
|
894 | let {
|
895 | fallbackElement,
|
896 | router
|
897 | } = _ref;
|
898 |
|
899 | let state = useSyncExternalStore(router.subscribe, () => router.state,
|
900 |
|
901 |
|
902 | () => router.state);
|
903 | let navigator = React.useMemo(() => {
|
904 | return {
|
905 | createHref: router.createHref,
|
906 | go: n => router.navigate(n),
|
907 | push: (to, state, opts) => router.navigate(to, {
|
908 | state,
|
909 | preventScrollReset: opts == null ? void 0 : opts.preventScrollReset
|
910 | }),
|
911 | replace: (to, state, opts) => router.navigate(to, {
|
912 | replace: true,
|
913 | state,
|
914 | preventScrollReset: opts == null ? void 0 : opts.preventScrollReset
|
915 | })
|
916 | };
|
917 | }, [router]);
|
918 | let basename = router.basename || "/";
|
919 | return React.createElement(DataRouterContext.Provider, {
|
920 | value: {
|
921 | router,
|
922 | navigator,
|
923 | static: false,
|
924 |
|
925 | basename
|
926 | }
|
927 | }, React.createElement(DataRouterStateContext.Provider, {
|
928 | value: state
|
929 | }, React.createElement(Router, {
|
930 | basename: router.basename,
|
931 | location: router.state.location,
|
932 | navigationType: router.state.historyAction,
|
933 | navigator: navigator
|
934 | }, router.state.initialized ? React.createElement(Routes, null) : fallbackElement)));
|
935 | }
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 | function MemoryRouter(_ref2) {
|
943 | let {
|
944 | basename,
|
945 | children,
|
946 | initialEntries,
|
947 | initialIndex
|
948 | } = _ref2;
|
949 | let historyRef = React.useRef();
|
950 |
|
951 | if (historyRef.current == null) {
|
952 | historyRef.current = createMemoryHistory({
|
953 | initialEntries,
|
954 | initialIndex,
|
955 | v5Compat: true
|
956 | });
|
957 | }
|
958 |
|
959 | let history = historyRef.current;
|
960 | let [state, setState] = React.useState({
|
961 | action: history.action,
|
962 | location: history.location
|
963 | });
|
964 | React.useLayoutEffect(() => history.listen(setState), [history]);
|
965 | return React.createElement(Router, {
|
966 | basename: basename,
|
967 | children: children,
|
968 | location: state.location,
|
969 | navigationType: state.action,
|
970 | navigator: history
|
971 | });
|
972 | }
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 | function Navigate(_ref3) {
|
984 | let {
|
985 | to,
|
986 | replace,
|
987 | state,
|
988 | relative
|
989 | } = _ref3;
|
990 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
991 |
|
992 | "<Navigate> may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
993 | process.env.NODE_ENV !== "production" ? warning(!React.useContext(NavigationContext).static, "<Navigate> must not be used on the initial render in a <StaticRouter>. " + "This is a no-op, but you should modify your code so the <Navigate> is " + "only ever rendered in response to some user interaction or state change.") : void 0;
|
994 | let dataRouterState = React.useContext(DataRouterStateContext);
|
995 | let navigate = useNavigate();
|
996 | React.useEffect(() => {
|
997 |
|
998 |
|
999 |
|
1000 | if (dataRouterState && dataRouterState.navigation.state !== "idle") {
|
1001 | return;
|
1002 | }
|
1003 |
|
1004 | navigate(to, {
|
1005 | replace,
|
1006 | state,
|
1007 | relative
|
1008 | });
|
1009 | });
|
1010 | return null;
|
1011 | }
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 | function Outlet(props) {
|
1019 | return useOutlet(props.context);
|
1020 | }
|
1021 |
|
1022 |
|
1023 |
|
1024 |
|
1025 |
|
1026 |
|
1027 | function Route(_props) {
|
1028 | process.env.NODE_ENV !== "production" ? invariant(false, "A <Route> is only ever to be used as the child of <Routes> element, " + "never rendered directly. Please wrap your <Route> in a <Routes>.") : invariant(false) ;
|
1029 | }
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 |
|
1035 |
|
1036 |
|
1037 |
|
1038 |
|
1039 |
|
1040 | function Router(_ref4) {
|
1041 | let {
|
1042 | basename: basenameProp = "/",
|
1043 | children = null,
|
1044 | location: locationProp,
|
1045 | navigationType = Action.Pop,
|
1046 | navigator,
|
1047 | static: staticProp = false
|
1048 | } = _ref4;
|
1049 | !!useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, "You cannot render a <Router> inside another <Router>." + " You should never have more than one in your app.") : invariant(false) : void 0;
|
1050 |
|
1051 |
|
1052 | let basename = basenameProp.replace(/^\/*/, "/");
|
1053 | let navigationContext = React.useMemo(() => ({
|
1054 | basename,
|
1055 | navigator,
|
1056 | static: staticProp
|
1057 | }), [basename, navigator, staticProp]);
|
1058 |
|
1059 | if (typeof locationProp === "string") {
|
1060 | locationProp = parsePath(locationProp);
|
1061 | }
|
1062 |
|
1063 | let {
|
1064 | pathname = "/",
|
1065 | search = "",
|
1066 | hash = "",
|
1067 | state = null,
|
1068 | key = "default"
|
1069 | } = locationProp;
|
1070 | let location = React.useMemo(() => {
|
1071 | let trailingPathname = stripBasename(pathname, basename);
|
1072 |
|
1073 | if (trailingPathname == null) {
|
1074 | return null;
|
1075 | }
|
1076 |
|
1077 | return {
|
1078 | pathname: trailingPathname,
|
1079 | search,
|
1080 | hash,
|
1081 | state,
|
1082 | key
|
1083 | };
|
1084 | }, [basename, pathname, search, hash, state, key]);
|
1085 | process.env.NODE_ENV !== "production" ? warning(location != null, "<Router basename=\"" + basename + "\"> is not able to match the URL " + ("\"" + pathname + search + hash + "\" because it does not start with the ") + "basename, so the <Router> won't render anything.") : void 0;
|
1086 |
|
1087 | if (location == null) {
|
1088 | return null;
|
1089 | }
|
1090 |
|
1091 | return React.createElement(NavigationContext.Provider, {
|
1092 | value: navigationContext
|
1093 | }, React.createElement(LocationContext.Provider, {
|
1094 | children: children,
|
1095 | value: {
|
1096 | location,
|
1097 | navigationType
|
1098 | }
|
1099 | }));
|
1100 | }
|
1101 |
|
1102 |
|
1103 |
|
1104 |
|
1105 |
|
1106 |
|
1107 |
|
1108 | function Routes(_ref5) {
|
1109 | let {
|
1110 | children,
|
1111 | location
|
1112 | } = _ref5;
|
1113 | let dataRouterContext = React.useContext(DataRouterContext);
|
1114 |
|
1115 |
|
1116 |
|
1117 | let routes = dataRouterContext && !children ? dataRouterContext.router.routes : createRoutesFromChildren(children);
|
1118 | return useRoutes(routes, location);
|
1119 | }
|
1120 |
|
1121 |
|
1122 |
|
1123 |
|
1124 |
|
1125 | function Await(_ref6) {
|
1126 | let {
|
1127 | children,
|
1128 | errorElement,
|
1129 | resolve
|
1130 | } = _ref6;
|
1131 | return React.createElement(AwaitErrorBoundary, {
|
1132 | resolve: resolve,
|
1133 | errorElement: errorElement
|
1134 | }, React.createElement(ResolveAwait, null, children));
|
1135 | }
|
1136 | var AwaitRenderStatus;
|
1137 |
|
1138 | (function (AwaitRenderStatus) {
|
1139 | AwaitRenderStatus[AwaitRenderStatus["pending"] = 0] = "pending";
|
1140 | AwaitRenderStatus[AwaitRenderStatus["success"] = 1] = "success";
|
1141 | AwaitRenderStatus[AwaitRenderStatus["error"] = 2] = "error";
|
1142 | })(AwaitRenderStatus || (AwaitRenderStatus = {}));
|
1143 |
|
1144 | const neverSettledPromise = new Promise(() => {});
|
1145 |
|
1146 | class AwaitErrorBoundary extends React.Component {
|
1147 | constructor(props) {
|
1148 | super(props);
|
1149 | this.state = {
|
1150 | error: null
|
1151 | };
|
1152 | }
|
1153 |
|
1154 | static getDerivedStateFromError(error) {
|
1155 | return {
|
1156 | error
|
1157 | };
|
1158 | }
|
1159 |
|
1160 | componentDidCatch(error, errorInfo) {
|
1161 | console.error("<Await> caught the following error during render", error, errorInfo);
|
1162 | }
|
1163 |
|
1164 | render() {
|
1165 | let {
|
1166 | children,
|
1167 | errorElement,
|
1168 | resolve
|
1169 | } = this.props;
|
1170 | let promise = null;
|
1171 | let status = AwaitRenderStatus.pending;
|
1172 |
|
1173 | if (!(resolve instanceof Promise)) {
|
1174 |
|
1175 | status = AwaitRenderStatus.success;
|
1176 | promise = Promise.resolve();
|
1177 | Object.defineProperty(promise, "_tracked", {
|
1178 | get: () => true
|
1179 | });
|
1180 | Object.defineProperty(promise, "_data", {
|
1181 | get: () => resolve
|
1182 | });
|
1183 | } else if (this.state.error) {
|
1184 |
|
1185 | status = AwaitRenderStatus.error;
|
1186 | let renderError = this.state.error;
|
1187 | promise = Promise.reject().catch(() => {});
|
1188 |
|
1189 | Object.defineProperty(promise, "_tracked", {
|
1190 | get: () => true
|
1191 | });
|
1192 | Object.defineProperty(promise, "_error", {
|
1193 | get: () => renderError
|
1194 | });
|
1195 | } else if (resolve._tracked) {
|
1196 |
|
1197 | promise = resolve;
|
1198 | status = promise._error !== undefined ? AwaitRenderStatus.error : promise._data !== undefined ? AwaitRenderStatus.success : AwaitRenderStatus.pending;
|
1199 | } else {
|
1200 |
|
1201 | status = AwaitRenderStatus.pending;
|
1202 | Object.defineProperty(resolve, "_tracked", {
|
1203 | get: () => true
|
1204 | });
|
1205 | promise = resolve.then(data => Object.defineProperty(resolve, "_data", {
|
1206 | get: () => data
|
1207 | }), error => Object.defineProperty(resolve, "_error", {
|
1208 | get: () => error
|
1209 | }));
|
1210 | }
|
1211 |
|
1212 | if (status === AwaitRenderStatus.error && promise._error instanceof AbortedDeferredError) {
|
1213 |
|
1214 | throw neverSettledPromise;
|
1215 | }
|
1216 |
|
1217 | if (status === AwaitRenderStatus.error && !errorElement) {
|
1218 |
|
1219 | throw promise._error;
|
1220 | }
|
1221 |
|
1222 | if (status === AwaitRenderStatus.error) {
|
1223 |
|
1224 | return React.createElement(AwaitContext.Provider, {
|
1225 | value: promise,
|
1226 | children: errorElement
|
1227 | });
|
1228 | }
|
1229 |
|
1230 | if (status === AwaitRenderStatus.success) {
|
1231 |
|
1232 | return React.createElement(AwaitContext.Provider, {
|
1233 | value: promise,
|
1234 | children: children
|
1235 | });
|
1236 | }
|
1237 |
|
1238 |
|
1239 | throw promise;
|
1240 | }
|
1241 |
|
1242 | }
|
1243 |
|
1244 |
|
1245 |
|
1246 |
|
1247 |
|
1248 |
|
1249 | function ResolveAwait(_ref7) {
|
1250 | let {
|
1251 | children
|
1252 | } = _ref7;
|
1253 | let data = useAsyncValue();
|
1254 |
|
1255 | if (typeof children === "function") {
|
1256 | return children(data);
|
1257 | }
|
1258 |
|
1259 | return React.createElement(React.Fragment, null, children);
|
1260 | }
|
1261 |
|
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 |
|
1267 |
|
1268 |
|
1269 |
|
1270 |
|
1271 |
|
1272 |
|
1273 | function createRoutesFromChildren(children, parentPath) {
|
1274 | if (parentPath === void 0) {
|
1275 | parentPath = [];
|
1276 | }
|
1277 |
|
1278 | let routes = [];
|
1279 | React.Children.forEach(children, (element, index) => {
|
1280 | if (! React.isValidElement(element)) {
|
1281 |
|
1282 |
|
1283 | return;
|
1284 | }
|
1285 |
|
1286 | if (element.type === React.Fragment) {
|
1287 |
|
1288 | routes.push.apply(routes, createRoutesFromChildren(element.props.children, parentPath));
|
1289 | return;
|
1290 | }
|
1291 |
|
1292 | !(element.type === Route) ? process.env.NODE_ENV !== "production" ? invariant(false, "[" + (typeof element.type === "string" ? element.type : element.type.name) + "] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>") : invariant(false) : void 0;
|
1293 | let treePath = [...parentPath, index];
|
1294 | let route = {
|
1295 | id: element.props.id || treePath.join("-"),
|
1296 | caseSensitive: element.props.caseSensitive,
|
1297 | element: element.props.element,
|
1298 | index: element.props.index,
|
1299 | path: element.props.path,
|
1300 | loader: element.props.loader,
|
1301 | action: element.props.action,
|
1302 | errorElement: element.props.errorElement,
|
1303 | hasErrorBoundary: element.props.errorElement != null,
|
1304 | shouldRevalidate: element.props.shouldRevalidate,
|
1305 | handle: element.props.handle
|
1306 | };
|
1307 |
|
1308 | if (element.props.children) {
|
1309 | route.children = createRoutesFromChildren(element.props.children, treePath);
|
1310 | }
|
1311 |
|
1312 | routes.push(route);
|
1313 | });
|
1314 | return routes;
|
1315 | }
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 | function renderMatches(matches) {
|
1321 | return _renderMatches(matches);
|
1322 | }
|
1323 |
|
1324 |
|
1325 |
|
1326 |
|
1327 |
|
1328 |
|
1329 | function enhanceManualRouteObjects(routes) {
|
1330 | return routes.map(route => {
|
1331 | let routeClone = _extends({}, route);
|
1332 |
|
1333 | if (routeClone.hasErrorBoundary == null) {
|
1334 | routeClone.hasErrorBoundary = routeClone.errorElement != null;
|
1335 | }
|
1336 |
|
1337 | if (routeClone.children) {
|
1338 | routeClone.children = enhanceManualRouteObjects(routeClone.children);
|
1339 | }
|
1340 |
|
1341 | return routeClone;
|
1342 | });
|
1343 | }
|
1344 |
|
1345 | function createMemoryRouter(routes, opts) {
|
1346 | return createRouter({
|
1347 | basename: opts == null ? void 0 : opts.basename,
|
1348 | history: createMemoryHistory({
|
1349 | initialEntries: opts == null ? void 0 : opts.initialEntries,
|
1350 | initialIndex: opts == null ? void 0 : opts.initialIndex
|
1351 | }),
|
1352 | hydrationData: opts == null ? void 0 : opts.hydrationData,
|
1353 | routes: enhanceManualRouteObjects(routes)
|
1354 | }).initialize();
|
1355 | }
|
1356 |
|
1357 | export { Await, MemoryRouter, Navigate, Outlet, Route, Router, RouterProvider, Routes, DataRouterContext as UNSAFE_DataRouterContext, DataRouterStateContext as UNSAFE_DataRouterStateContext, DataStaticRouterContext as UNSAFE_DataStaticRouterContext, LocationContext as UNSAFE_LocationContext, NavigationContext as UNSAFE_NavigationContext, RouteContext as UNSAFE_RouteContext, enhanceManualRouteObjects as UNSAFE_enhanceManualRouteObjects, createMemoryRouter, createRoutesFromChildren, createRoutesFromChildren as createRoutesFromElements, renderMatches, useActionData, useAsyncError, useAsyncValue, useHref, useInRouterContext, useLoaderData, useLocation, useMatch, useMatches, useNavigate, useNavigation, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRevalidator, useRouteError, useRouteLoaderData, useRoutes };
|
1358 |
|