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