1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | import { invariant, joinPaths, matchPath, UNSAFE_getPathContributingMatches, warning, resolveTo, 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 | function useNavigate() {
|
366 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
367 |
|
368 | "useNavigate() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
369 | let {
|
370 | basename,
|
371 | navigator
|
372 | } = React.useContext(NavigationContext);
|
373 | let {
|
374 | matches
|
375 | } = React.useContext(RouteContext);
|
376 | let {
|
377 | pathname: locationPathname
|
378 | } = useLocation();
|
379 | let routePathnamesJson = JSON.stringify(UNSAFE_getPathContributingMatches(matches).map(match => match.pathnameBase));
|
380 | let activeRef = React.useRef(false);
|
381 | React.useEffect(() => {
|
382 | activeRef.current = true;
|
383 | });
|
384 | let navigate = React.useCallback(function (to, options) {
|
385 | if (options === void 0) {
|
386 | options = {};
|
387 | }
|
388 |
|
389 | 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;
|
390 | if (!activeRef.current) return;
|
391 |
|
392 | if (typeof to === "number") {
|
393 | navigator.go(to);
|
394 | return;
|
395 | }
|
396 |
|
397 | let path = resolveTo(to, JSON.parse(routePathnamesJson), locationPathname, options.relative === "path");
|
398 |
|
399 |
|
400 |
|
401 |
|
402 | if (basename !== "/") {
|
403 | path.pathname = path.pathname === "/" ? basename : joinPaths([basename, path.pathname]);
|
404 | }
|
405 |
|
406 | (!!options.replace ? navigator.replace : navigator.push)(path, options.state, options);
|
407 | }, [basename, navigator, routePathnamesJson, locationPathname]);
|
408 | return navigate;
|
409 | }
|
410 | const OutletContext = React.createContext(null);
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 | function useOutletContext() {
|
418 | return React.useContext(OutletContext);
|
419 | }
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 | function useOutlet(context) {
|
428 | let outlet = React.useContext(RouteContext).outlet;
|
429 |
|
430 | if (outlet) {
|
431 | return React.createElement(OutletContext.Provider, {
|
432 | value: context
|
433 | }, outlet);
|
434 | }
|
435 |
|
436 | return outlet;
|
437 | }
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 | function useParams() {
|
446 | let {
|
447 | matches
|
448 | } = React.useContext(RouteContext);
|
449 | let routeMatch = matches[matches.length - 1];
|
450 | return routeMatch ? routeMatch.params : {};
|
451 | }
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 | function useResolvedPath(to, _temp2) {
|
459 | let {
|
460 | relative
|
461 | } = _temp2 === void 0 ? {} : _temp2;
|
462 | let {
|
463 | matches
|
464 | } = React.useContext(RouteContext);
|
465 | let {
|
466 | pathname: locationPathname
|
467 | } = useLocation();
|
468 | let routePathnamesJson = JSON.stringify(UNSAFE_getPathContributingMatches(matches).map(match => match.pathnameBase));
|
469 | return React.useMemo(() => resolveTo(to, JSON.parse(routePathnamesJson), locationPathname, relative === "path"), [to, routePathnamesJson, locationPathname, relative]);
|
470 | }
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 | function useRoutes(routes, locationArg) {
|
481 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
482 |
|
483 | "useRoutes() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
484 | let {
|
485 | navigator
|
486 | } = React.useContext(NavigationContext);
|
487 | let dataRouterStateContext = React.useContext(DataRouterStateContext);
|
488 | let {
|
489 | matches: parentMatches
|
490 | } = React.useContext(RouteContext);
|
491 | let routeMatch = parentMatches[parentMatches.length - 1];
|
492 | let parentParams = routeMatch ? routeMatch.params : {};
|
493 | let parentPathname = routeMatch ? routeMatch.pathname : "/";
|
494 | let parentPathnameBase = routeMatch ? routeMatch.pathnameBase : "/";
|
495 | let parentRoute = routeMatch && routeMatch.route;
|
496 |
|
497 | if (process.env.NODE_ENV !== "production") {
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 | let parentPath = parentRoute && parentRoute.path || "";
|
519 | 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 + "/*") + "\">."));
|
520 | }
|
521 |
|
522 | let locationFromContext = useLocation();
|
523 | let location;
|
524 |
|
525 | if (locationArg) {
|
526 | var _parsedLocationArg$pa;
|
527 |
|
528 | let parsedLocationArg = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
|
529 | !(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;
|
530 | location = parsedLocationArg;
|
531 | } else {
|
532 | location = locationFromContext;
|
533 | }
|
534 |
|
535 | let pathname = location.pathname || "/";
|
536 | let remainingPathname = parentPathnameBase === "/" ? pathname : pathname.slice(parentPathnameBase.length) || "/";
|
537 | let matches = matchRoutes(routes, {
|
538 | pathname: remainingPathname
|
539 | });
|
540 |
|
541 | if (process.env.NODE_ENV !== "production") {
|
542 | process.env.NODE_ENV !== "production" ? warning(parentRoute || matches != null, "No routes matched location \"" + location.pathname + location.search + location.hash + "\" ") : void 0;
|
543 | 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;
|
544 | }
|
545 |
|
546 | let renderedMatches = _renderMatches(matches && matches.map(match => Object.assign({}, match, {
|
547 | params: Object.assign({}, parentParams, match.params),
|
548 | pathname: joinPaths([parentPathnameBase,
|
549 | navigator.encodeLocation ? navigator.encodeLocation(match.pathname).pathname : match.pathname]),
|
550 | pathnameBase: match.pathnameBase === "/" ? parentPathnameBase : joinPaths([parentPathnameBase,
|
551 | navigator.encodeLocation ? navigator.encodeLocation(match.pathnameBase).pathname : match.pathnameBase])
|
552 | })), parentMatches, dataRouterStateContext || undefined);
|
553 |
|
554 |
|
555 |
|
556 |
|
557 | if (locationArg && renderedMatches) {
|
558 | return React.createElement(LocationContext.Provider, {
|
559 | value: {
|
560 | location: _extends({
|
561 | pathname: "/",
|
562 | search: "",
|
563 | hash: "",
|
564 | state: null,
|
565 | key: "default"
|
566 | }, location),
|
567 | navigationType: Action.Pop
|
568 | }
|
569 | }, renderedMatches);
|
570 | }
|
571 |
|
572 | return renderedMatches;
|
573 | }
|
574 |
|
575 | function DefaultErrorElement() {
|
576 | let error = useRouteError();
|
577 | let message = isRouteErrorResponse(error) ? error.status + " " + error.statusText : error instanceof Error ? error.message : JSON.stringify(error);
|
578 | let stack = error instanceof Error ? error.stack : null;
|
579 | let lightgrey = "rgba(200,200,200, 0.5)";
|
580 | let preStyles = {
|
581 | padding: "0.5rem",
|
582 | backgroundColor: lightgrey
|
583 | };
|
584 | let codeStyles = {
|
585 | padding: "2px 4px",
|
586 | backgroundColor: lightgrey
|
587 | };
|
588 | return React.createElement(React.Fragment, null, React.createElement("h2", null, "Unhandled Thrown Error!"), React.createElement("h3", {
|
589 | style: {
|
590 | fontStyle: "italic"
|
591 | }
|
592 | }, message), stack ? React.createElement("pre", {
|
593 | style: preStyles
|
594 | }, 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", {
|
595 | style: codeStyles
|
596 | }, "errorElement"), " props on\xA0", React.createElement("code", {
|
597 | style: codeStyles
|
598 | }, "<Route>")));
|
599 | }
|
600 |
|
601 | class RenderErrorBoundary extends React.Component {
|
602 | constructor(props) {
|
603 | super(props);
|
604 | this.state = {
|
605 | location: props.location,
|
606 | error: props.error
|
607 | };
|
608 | }
|
609 |
|
610 | static getDerivedStateFromError(error) {
|
611 | return {
|
612 | error: error
|
613 | };
|
614 | }
|
615 |
|
616 | static getDerivedStateFromProps(props, state) {
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 | if (state.location !== props.location) {
|
626 | return {
|
627 | error: props.error,
|
628 | location: props.location
|
629 | };
|
630 | }
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 | return {
|
637 | error: props.error || state.error,
|
638 | location: state.location
|
639 | };
|
640 | }
|
641 |
|
642 | componentDidCatch(error, errorInfo) {
|
643 | console.error("React Router caught the following error during render", error, errorInfo);
|
644 | }
|
645 |
|
646 | render() {
|
647 | return this.state.error ? React.createElement(RouteErrorContext.Provider, {
|
648 | value: this.state.error,
|
649 | children: this.props.component
|
650 | }) : this.props.children;
|
651 | }
|
652 |
|
653 | }
|
654 |
|
655 | function RenderedRoute(_ref) {
|
656 | let {
|
657 | routeContext,
|
658 | match,
|
659 | children
|
660 | } = _ref;
|
661 | let dataStaticRouterContext = React.useContext(DataStaticRouterContext);
|
662 |
|
663 |
|
664 | if (dataStaticRouterContext && match.route.errorElement) {
|
665 | dataStaticRouterContext._deepestRenderedBoundaryId = match.route.id;
|
666 | }
|
667 |
|
668 | return React.createElement(RouteContext.Provider, {
|
669 | value: routeContext
|
670 | }, children);
|
671 | }
|
672 |
|
673 | function _renderMatches(matches, parentMatches, dataRouterState) {
|
674 | if (parentMatches === void 0) {
|
675 | parentMatches = [];
|
676 | }
|
677 |
|
678 | if (matches == null) {
|
679 | if (dataRouterState != null && dataRouterState.errors) {
|
680 |
|
681 |
|
682 | matches = dataRouterState.matches;
|
683 | } else {
|
684 | return null;
|
685 | }
|
686 | }
|
687 |
|
688 | let renderedMatches = matches;
|
689 |
|
690 | let errors = dataRouterState == null ? void 0 : dataRouterState.errors;
|
691 |
|
692 | if (errors != null) {
|
693 | let errorIndex = renderedMatches.findIndex(m => m.route.id && (errors == null ? void 0 : errors[m.route.id]));
|
694 | !(errorIndex >= 0) ? process.env.NODE_ENV !== "production" ? invariant(false, "Could not find a matching route for the current errors: " + errors) : invariant(false) : void 0;
|
695 | renderedMatches = renderedMatches.slice(0, Math.min(renderedMatches.length, errorIndex + 1));
|
696 | }
|
697 |
|
698 | return renderedMatches.reduceRight((outlet, match, index) => {
|
699 | let error = match.route.id ? errors == null ? void 0 : errors[match.route.id] : null;
|
700 |
|
701 | let errorElement = dataRouterState ? match.route.errorElement || React.createElement(DefaultErrorElement, null) : null;
|
702 |
|
703 | let getChildren = () => React.createElement(RenderedRoute, {
|
704 | match: match,
|
705 | routeContext: {
|
706 | outlet,
|
707 | matches: parentMatches.concat(renderedMatches.slice(0, index + 1))
|
708 | }
|
709 | }, error ? errorElement : match.route.element !== undefined ? match.route.element : outlet);
|
710 |
|
711 |
|
712 |
|
713 |
|
714 | return dataRouterState && (match.route.errorElement || index === 0) ? React.createElement(RenderErrorBoundary, {
|
715 | location: dataRouterState.location,
|
716 | component: errorElement,
|
717 | error: error,
|
718 | children: getChildren()
|
719 | }) : getChildren();
|
720 | }, null);
|
721 | }
|
722 | var DataRouterHook;
|
723 |
|
724 | (function (DataRouterHook) {
|
725 | DataRouterHook["UseRevalidator"] = "useRevalidator";
|
726 | })(DataRouterHook || (DataRouterHook = {}));
|
727 |
|
728 | var DataRouterStateHook;
|
729 |
|
730 | (function (DataRouterStateHook) {
|
731 | DataRouterStateHook["UseLoaderData"] = "useLoaderData";
|
732 | DataRouterStateHook["UseActionData"] = "useActionData";
|
733 | DataRouterStateHook["UseRouteError"] = "useRouteError";
|
734 | DataRouterStateHook["UseNavigation"] = "useNavigation";
|
735 | DataRouterStateHook["UseRouteLoaderData"] = "useRouteLoaderData";
|
736 | DataRouterStateHook["UseMatches"] = "useMatches";
|
737 | DataRouterStateHook["UseRevalidator"] = "useRevalidator";
|
738 | })(DataRouterStateHook || (DataRouterStateHook = {}));
|
739 |
|
740 | function getDataRouterConsoleError(hookName) {
|
741 | return hookName + " must be used within a data router. See https://reactrouter.com/en/main/routers/picking-a-router.";
|
742 | }
|
743 |
|
744 | function useDataRouterContext(hookName) {
|
745 | let ctx = React.useContext(DataRouterContext);
|
746 | !ctx ? process.env.NODE_ENV !== "production" ? invariant(false, getDataRouterConsoleError(hookName)) : invariant(false) : void 0;
|
747 | return ctx;
|
748 | }
|
749 |
|
750 | function useDataRouterState(hookName) {
|
751 | let state = React.useContext(DataRouterStateContext);
|
752 | !state ? process.env.NODE_ENV !== "production" ? invariant(false, getDataRouterConsoleError(hookName)) : invariant(false) : void 0;
|
753 | return state;
|
754 | }
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 |
|
761 | function useNavigation() {
|
762 | let state = useDataRouterState(DataRouterStateHook.UseNavigation);
|
763 | return state.navigation;
|
764 | }
|
765 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 | function useRevalidator() {
|
771 | let dataRouterContext = useDataRouterContext(DataRouterHook.UseRevalidator);
|
772 | let state = useDataRouterState(DataRouterStateHook.UseRevalidator);
|
773 | return {
|
774 | revalidate: dataRouterContext.router.revalidate,
|
775 | state: state.revalidation
|
776 | };
|
777 | }
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 | function useMatches() {
|
784 | let {
|
785 | matches,
|
786 | loaderData
|
787 | } = useDataRouterState(DataRouterStateHook.UseMatches);
|
788 | return React.useMemo(() => matches.map(match => {
|
789 | let {
|
790 | pathname,
|
791 | params
|
792 | } = match;
|
793 |
|
794 |
|
795 |
|
796 | return {
|
797 | id: match.route.id,
|
798 | pathname,
|
799 | params,
|
800 | data: loaderData[match.route.id],
|
801 | handle: match.route.handle
|
802 | };
|
803 | }), [matches, loaderData]);
|
804 | }
|
805 |
|
806 |
|
807 |
|
808 |
|
809 | function useLoaderData() {
|
810 | let state = useDataRouterState(DataRouterStateHook.UseLoaderData);
|
811 | let route = React.useContext(RouteContext);
|
812 | !route ? process.env.NODE_ENV !== "production" ? invariant(false, "useLoaderData must be used inside a RouteContext") : invariant(false) : void 0;
|
813 | let thisRoute = route.matches[route.matches.length - 1];
|
814 | !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;
|
815 | return state.loaderData[thisRoute.route.id];
|
816 | }
|
817 |
|
818 |
|
819 |
|
820 |
|
821 | function useRouteLoaderData(routeId) {
|
822 | let state = useDataRouterState(DataRouterStateHook.UseRouteLoaderData);
|
823 | return state.loaderData[routeId];
|
824 | }
|
825 |
|
826 |
|
827 |
|
828 |
|
829 | function useActionData() {
|
830 | let state = useDataRouterState(DataRouterStateHook.UseActionData);
|
831 | let route = React.useContext(RouteContext);
|
832 | !route ? process.env.NODE_ENV !== "production" ? invariant(false, "useActionData must be used inside a RouteContext") : invariant(false) : void 0;
|
833 | return Object.values((state == null ? void 0 : state.actionData) || {})[0];
|
834 | }
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 | function useRouteError() {
|
842 | var _state$errors;
|
843 |
|
844 | let error = React.useContext(RouteErrorContext);
|
845 | let state = useDataRouterState(DataRouterStateHook.UseRouteError);
|
846 | let route = React.useContext(RouteContext);
|
847 | let thisRoute = route.matches[route.matches.length - 1];
|
848 |
|
849 |
|
850 | if (error) {
|
851 | return error;
|
852 | }
|
853 |
|
854 | !route ? process.env.NODE_ENV !== "production" ? invariant(false, "useRouteError must be used inside a RouteContext") : invariant(false) : void 0;
|
855 | !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;
|
856 |
|
857 | return (_state$errors = state.errors) == null ? void 0 : _state$errors[thisRoute.route.id];
|
858 | }
|
859 |
|
860 |
|
861 |
|
862 |
|
863 | function useAsyncValue() {
|
864 | let value = React.useContext(AwaitContext);
|
865 | return value == null ? void 0 : value._data;
|
866 | }
|
867 |
|
868 |
|
869 |
|
870 |
|
871 | function useAsyncError() {
|
872 | let value = React.useContext(AwaitContext);
|
873 | return value == null ? void 0 : value._error;
|
874 | }
|
875 | const alreadyWarned = {};
|
876 |
|
877 | function warningOnce(key, cond, message) {
|
878 | if (!cond && !alreadyWarned[key]) {
|
879 | alreadyWarned[key] = true;
|
880 | process.env.NODE_ENV !== "production" ? warning(false, message) : void 0;
|
881 | }
|
882 | }
|
883 |
|
884 |
|
885 |
|
886 |
|
887 | function RouterProvider(_ref) {
|
888 | let {
|
889 | fallbackElement,
|
890 | router
|
891 | } = _ref;
|
892 |
|
893 | let state = useSyncExternalStore(router.subscribe, () => router.state,
|
894 |
|
895 |
|
896 | () => router.state);
|
897 | let navigator = React.useMemo(() => {
|
898 | return {
|
899 | createHref: router.createHref,
|
900 | encodeLocation: router.encodeLocation,
|
901 | go: n => router.navigate(n),
|
902 | push: (to, state, opts) => router.navigate(to, {
|
903 | state,
|
904 | preventScrollReset: opts == null ? void 0 : opts.preventScrollReset
|
905 | }),
|
906 | replace: (to, state, opts) => router.navigate(to, {
|
907 | replace: true,
|
908 | state,
|
909 | preventScrollReset: opts == null ? void 0 : opts.preventScrollReset
|
910 | })
|
911 | };
|
912 | }, [router]);
|
913 | let basename = router.basename || "/";
|
914 | return React.createElement(DataRouterContext.Provider, {
|
915 | value: {
|
916 | router,
|
917 | navigator,
|
918 | static: false,
|
919 |
|
920 | basename
|
921 | }
|
922 | }, React.createElement(DataRouterStateContext.Provider, {
|
923 | value: state
|
924 | }, React.createElement(Router, {
|
925 | basename: router.basename,
|
926 | location: router.state.location,
|
927 | navigationType: router.state.historyAction,
|
928 | navigator: navigator
|
929 | }, router.state.initialized ? React.createElement(Routes, null) : fallbackElement)));
|
930 | }
|
931 |
|
932 |
|
933 |
|
934 |
|
935 |
|
936 |
|
937 | function MemoryRouter(_ref2) {
|
938 | let {
|
939 | basename,
|
940 | children,
|
941 | initialEntries,
|
942 | initialIndex
|
943 | } = _ref2;
|
944 | let historyRef = React.useRef();
|
945 |
|
946 | if (historyRef.current == null) {
|
947 | historyRef.current = createMemoryHistory({
|
948 | initialEntries,
|
949 | initialIndex,
|
950 | v5Compat: true
|
951 | });
|
952 | }
|
953 |
|
954 | let history = historyRef.current;
|
955 | let [state, setState] = React.useState({
|
956 | action: history.action,
|
957 | location: history.location
|
958 | });
|
959 | React.useLayoutEffect(() => history.listen(setState), [history]);
|
960 | return React.createElement(Router, {
|
961 | basename: basename,
|
962 | children: children,
|
963 | location: state.location,
|
964 | navigationType: state.action,
|
965 | navigator: history
|
966 | });
|
967 | }
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 | function Navigate(_ref3) {
|
979 | let {
|
980 | to,
|
981 | replace,
|
982 | state,
|
983 | relative
|
984 | } = _ref3;
|
985 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false,
|
986 |
|
987 | "<Navigate> may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
988 | 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;
|
989 | let dataRouterState = React.useContext(DataRouterStateContext);
|
990 | let navigate = useNavigate();
|
991 | React.useEffect(() => {
|
992 |
|
993 |
|
994 |
|
995 | if (dataRouterState && dataRouterState.navigation.state !== "idle") {
|
996 | return;
|
997 | }
|
998 |
|
999 | navigate(to, {
|
1000 | replace,
|
1001 | state,
|
1002 | relative
|
1003 | });
|
1004 | });
|
1005 | return null;
|
1006 | }
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 | function Outlet(props) {
|
1014 | return useOutlet(props.context);
|
1015 | }
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 |
|
1022 | function Route(_props) {
|
1023 | 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) ;
|
1024 | }
|
1025 |
|
1026 |
|
1027 |
|
1028 |
|
1029 |
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 |
|
1035 | function Router(_ref4) {
|
1036 | let {
|
1037 | basename: basenameProp = "/",
|
1038 | children = null,
|
1039 | location: locationProp,
|
1040 | navigationType = Action.Pop,
|
1041 | navigator,
|
1042 | static: staticProp = false
|
1043 | } = _ref4;
|
1044 | !!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;
|
1045 |
|
1046 |
|
1047 | let basename = basenameProp.replace(/^\/*/, "/");
|
1048 | let navigationContext = React.useMemo(() => ({
|
1049 | basename,
|
1050 | navigator,
|
1051 | static: staticProp
|
1052 | }), [basename, navigator, staticProp]);
|
1053 |
|
1054 | if (typeof locationProp === "string") {
|
1055 | locationProp = parsePath(locationProp);
|
1056 | }
|
1057 |
|
1058 | let {
|
1059 | pathname = "/",
|
1060 | search = "",
|
1061 | hash = "",
|
1062 | state = null,
|
1063 | key = "default"
|
1064 | } = locationProp;
|
1065 | let location = React.useMemo(() => {
|
1066 | let trailingPathname = stripBasename(pathname, basename);
|
1067 |
|
1068 | if (trailingPathname == null) {
|
1069 | return null;
|
1070 | }
|
1071 |
|
1072 | return {
|
1073 | pathname: trailingPathname,
|
1074 | search,
|
1075 | hash,
|
1076 | state,
|
1077 | key
|
1078 | };
|
1079 | }, [basename, pathname, search, hash, state, key]);
|
1080 | 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;
|
1081 |
|
1082 | if (location == null) {
|
1083 | return null;
|
1084 | }
|
1085 |
|
1086 | return React.createElement(NavigationContext.Provider, {
|
1087 | value: navigationContext
|
1088 | }, React.createElement(LocationContext.Provider, {
|
1089 | children: children,
|
1090 | value: {
|
1091 | location,
|
1092 | navigationType
|
1093 | }
|
1094 | }));
|
1095 | }
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 |
|
1102 |
|
1103 | function Routes(_ref5) {
|
1104 | let {
|
1105 | children,
|
1106 | location
|
1107 | } = _ref5;
|
1108 | let dataRouterContext = React.useContext(DataRouterContext);
|
1109 |
|
1110 |
|
1111 |
|
1112 | let routes = dataRouterContext && !children ? dataRouterContext.router.routes : createRoutesFromChildren(children);
|
1113 | return useRoutes(routes, location);
|
1114 | }
|
1115 |
|
1116 |
|
1117 |
|
1118 |
|
1119 |
|
1120 | function Await(_ref6) {
|
1121 | let {
|
1122 | children,
|
1123 | errorElement,
|
1124 | resolve
|
1125 | } = _ref6;
|
1126 | return React.createElement(AwaitErrorBoundary, {
|
1127 | resolve: resolve,
|
1128 | errorElement: errorElement
|
1129 | }, React.createElement(ResolveAwait, null, children));
|
1130 | }
|
1131 | var AwaitRenderStatus;
|
1132 |
|
1133 | (function (AwaitRenderStatus) {
|
1134 | AwaitRenderStatus[AwaitRenderStatus["pending"] = 0] = "pending";
|
1135 | AwaitRenderStatus[AwaitRenderStatus["success"] = 1] = "success";
|
1136 | AwaitRenderStatus[AwaitRenderStatus["error"] = 2] = "error";
|
1137 | })(AwaitRenderStatus || (AwaitRenderStatus = {}));
|
1138 |
|
1139 | const neverSettledPromise = new Promise(() => {});
|
1140 |
|
1141 | class AwaitErrorBoundary extends React.Component {
|
1142 | constructor(props) {
|
1143 | super(props);
|
1144 | this.state = {
|
1145 | error: null
|
1146 | };
|
1147 | }
|
1148 |
|
1149 | static getDerivedStateFromError(error) {
|
1150 | return {
|
1151 | error
|
1152 | };
|
1153 | }
|
1154 |
|
1155 | componentDidCatch(error, errorInfo) {
|
1156 | console.error("<Await> caught the following error during render", error, errorInfo);
|
1157 | }
|
1158 |
|
1159 | render() {
|
1160 | let {
|
1161 | children,
|
1162 | errorElement,
|
1163 | resolve
|
1164 | } = this.props;
|
1165 | let promise = null;
|
1166 | let status = AwaitRenderStatus.pending;
|
1167 |
|
1168 | if (!(resolve instanceof Promise)) {
|
1169 |
|
1170 | status = AwaitRenderStatus.success;
|
1171 | promise = Promise.resolve();
|
1172 | Object.defineProperty(promise, "_tracked", {
|
1173 | get: () => true
|
1174 | });
|
1175 | Object.defineProperty(promise, "_data", {
|
1176 | get: () => resolve
|
1177 | });
|
1178 | } else if (this.state.error) {
|
1179 |
|
1180 | status = AwaitRenderStatus.error;
|
1181 | let renderError = this.state.error;
|
1182 | promise = Promise.reject().catch(() => {});
|
1183 |
|
1184 | Object.defineProperty(promise, "_tracked", {
|
1185 | get: () => true
|
1186 | });
|
1187 | Object.defineProperty(promise, "_error", {
|
1188 | get: () => renderError
|
1189 | });
|
1190 | } else if (resolve._tracked) {
|
1191 |
|
1192 | promise = resolve;
|
1193 | status = promise._error !== undefined ? AwaitRenderStatus.error : promise._data !== undefined ? AwaitRenderStatus.success : AwaitRenderStatus.pending;
|
1194 | } else {
|
1195 |
|
1196 | status = AwaitRenderStatus.pending;
|
1197 | Object.defineProperty(resolve, "_tracked", {
|
1198 | get: () => true
|
1199 | });
|
1200 | promise = resolve.then(data => Object.defineProperty(resolve, "_data", {
|
1201 | get: () => data
|
1202 | }), error => Object.defineProperty(resolve, "_error", {
|
1203 | get: () => error
|
1204 | }));
|
1205 | }
|
1206 |
|
1207 | if (status === AwaitRenderStatus.error && promise._error instanceof AbortedDeferredError) {
|
1208 |
|
1209 | throw neverSettledPromise;
|
1210 | }
|
1211 |
|
1212 | if (status === AwaitRenderStatus.error && !errorElement) {
|
1213 |
|
1214 | throw promise._error;
|
1215 | }
|
1216 |
|
1217 | if (status === AwaitRenderStatus.error) {
|
1218 |
|
1219 | return React.createElement(AwaitContext.Provider, {
|
1220 | value: promise,
|
1221 | children: errorElement
|
1222 | });
|
1223 | }
|
1224 |
|
1225 | if (status === AwaitRenderStatus.success) {
|
1226 |
|
1227 | return React.createElement(AwaitContext.Provider, {
|
1228 | value: promise,
|
1229 | children: children
|
1230 | });
|
1231 | }
|
1232 |
|
1233 |
|
1234 | throw promise;
|
1235 | }
|
1236 |
|
1237 | }
|
1238 |
|
1239 |
|
1240 |
|
1241 |
|
1242 |
|
1243 |
|
1244 | function ResolveAwait(_ref7) {
|
1245 | let {
|
1246 | children
|
1247 | } = _ref7;
|
1248 | let data = useAsyncValue();
|
1249 |
|
1250 | if (typeof children === "function") {
|
1251 | return children(data);
|
1252 | }
|
1253 |
|
1254 | return React.createElement(React.Fragment, null, children);
|
1255 | }
|
1256 |
|
1257 |
|
1258 |
|
1259 |
|
1260 |
|
1261 |
|
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 |
|
1267 |
|
1268 | function createRoutesFromChildren(children, parentPath) {
|
1269 | if (parentPath === void 0) {
|
1270 | parentPath = [];
|
1271 | }
|
1272 |
|
1273 | let routes = [];
|
1274 | React.Children.forEach(children, (element, index) => {
|
1275 | if (! React.isValidElement(element)) {
|
1276 |
|
1277 |
|
1278 | return;
|
1279 | }
|
1280 |
|
1281 | if (element.type === React.Fragment) {
|
1282 |
|
1283 | routes.push.apply(routes, createRoutesFromChildren(element.props.children, parentPath));
|
1284 | return;
|
1285 | }
|
1286 |
|
1287 | !(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;
|
1288 | !(!element.props.index || !element.props.children) ? process.env.NODE_ENV !== "production" ? invariant(false, "An index route cannot have child routes.") : invariant(false) : void 0;
|
1289 | let treePath = [...parentPath, index];
|
1290 | let route = {
|
1291 | id: element.props.id || treePath.join("-"),
|
1292 | caseSensitive: element.props.caseSensitive,
|
1293 | element: element.props.element,
|
1294 | index: element.props.index,
|
1295 | path: element.props.path,
|
1296 | loader: element.props.loader,
|
1297 | action: element.props.action,
|
1298 | errorElement: element.props.errorElement,
|
1299 | hasErrorBoundary: element.props.errorElement != null,
|
1300 | shouldRevalidate: element.props.shouldRevalidate,
|
1301 | handle: element.props.handle
|
1302 | };
|
1303 |
|
1304 | if (element.props.children) {
|
1305 | route.children = createRoutesFromChildren(element.props.children, treePath);
|
1306 | }
|
1307 |
|
1308 | routes.push(route);
|
1309 | });
|
1310 | return routes;
|
1311 | }
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 | function renderMatches(matches) {
|
1317 | return _renderMatches(matches);
|
1318 | }
|
1319 |
|
1320 |
|
1321 |
|
1322 |
|
1323 |
|
1324 |
|
1325 | function enhanceManualRouteObjects(routes) {
|
1326 | return routes.map(route => {
|
1327 | let routeClone = _extends({}, route);
|
1328 |
|
1329 | if (routeClone.hasErrorBoundary == null) {
|
1330 | routeClone.hasErrorBoundary = routeClone.errorElement != null;
|
1331 | }
|
1332 |
|
1333 | if (routeClone.children) {
|
1334 | routeClone.children = enhanceManualRouteObjects(routeClone.children);
|
1335 | }
|
1336 |
|
1337 | return routeClone;
|
1338 | });
|
1339 | }
|
1340 |
|
1341 | function createMemoryRouter(routes, opts) {
|
1342 | return createRouter({
|
1343 | basename: opts == null ? void 0 : opts.basename,
|
1344 | history: createMemoryHistory({
|
1345 | initialEntries: opts == null ? void 0 : opts.initialEntries,
|
1346 | initialIndex: opts == null ? void 0 : opts.initialIndex
|
1347 | }),
|
1348 | hydrationData: opts == null ? void 0 : opts.hydrationData,
|
1349 | routes: enhanceManualRouteObjects(routes)
|
1350 | }).initialize();
|
1351 | }
|
1352 |
|
1353 | 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 };
|
1354 |
|