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