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