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