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