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