| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | import { UNSAFE_invariant, joinPaths, matchPath, UNSAFE_getPathContributingMatches, UNSAFE_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 | if (process.env.NODE_ENV !== "production") {
|
| 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 | if (process.env.NODE_ENV !== "production") {
|
| 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 DataRouterContext = React.createContext(null);
|
| 194 |
|
| 195 | if (process.env.NODE_ENV !== "production") {
|
| 196 | DataRouterContext.displayName = "DataRouter";
|
| 197 | }
|
| 198 |
|
| 199 | const DataRouterStateContext = React.createContext(null);
|
| 200 |
|
| 201 | if (process.env.NODE_ENV !== "production") {
|
| 202 | DataRouterStateContext.displayName = "DataRouterState";
|
| 203 | }
|
| 204 |
|
| 205 | const AwaitContext = React.createContext(null);
|
| 206 |
|
| 207 | if (process.env.NODE_ENV !== "production") {
|
| 208 | AwaitContext.displayName = "Await";
|
| 209 | }
|
| 210 |
|
| 211 | const NavigationContext = React.createContext(null);
|
| 212 |
|
| 213 | if (process.env.NODE_ENV !== "production") {
|
| 214 | NavigationContext.displayName = "Navigation";
|
| 215 | }
|
| 216 |
|
| 217 | const LocationContext = React.createContext(null);
|
| 218 |
|
| 219 | if (process.env.NODE_ENV !== "production") {
|
| 220 | LocationContext.displayName = "Location";
|
| 221 | }
|
| 222 |
|
| 223 | const RouteContext = React.createContext({
|
| 224 | outlet: null,
|
| 225 | matches: []
|
| 226 | });
|
| 227 |
|
| 228 | if (process.env.NODE_ENV !== "production") {
|
| 229 | RouteContext.displayName = "Route";
|
| 230 | }
|
| 231 |
|
| 232 | const RouteErrorContext = React.createContext(null);
|
| 233 |
|
| 234 | if (process.env.NODE_ENV !== "production") {
|
| 235 | RouteErrorContext.displayName = "RouteError";
|
| 236 | }
|
| 237 |
|
| 238 | function _extends() {
|
| 239 | _extends = Object.assign ? Object.assign.bind() : function (target) {
|
| 240 | for (var i = 1; i < arguments.length; i++) {
|
| 241 | var source = arguments[i];
|
| 242 |
|
| 243 | for (var key in source) {
|
| 244 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
| 245 | target[key] = source[key];
|
| 246 | }
|
| 247 | }
|
| 248 | }
|
| 249 |
|
| 250 | return target;
|
| 251 | };
|
| 252 | return _extends.apply(this, arguments);
|
| 253 | }
|
| 254 |
|
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 |
|
| 261 |
|
| 262 | function useHref(to, _temp) {
|
| 263 | let {
|
| 264 | relative
|
| 265 | } = _temp === void 0 ? {} : _temp;
|
| 266 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false,
|
| 267 |
|
| 268 | "useHref() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
| 269 | let {
|
| 270 | basename,
|
| 271 | navigator
|
| 272 | } = React.useContext(NavigationContext);
|
| 273 | let {
|
| 274 | hash,
|
| 275 | pathname,
|
| 276 | search
|
| 277 | } = useResolvedPath(to, {
|
| 278 | relative
|
| 279 | });
|
| 280 | let joinedPathname = pathname;
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 | if (basename !== "/") {
|
| 286 | joinedPathname = pathname === "/" ? basename : joinPaths([basename, pathname]);
|
| 287 | }
|
| 288 |
|
| 289 | return navigator.createHref({
|
| 290 | pathname: joinedPathname,
|
| 291 | search,
|
| 292 | hash
|
| 293 | });
|
| 294 | }
|
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 |
|
| 300 |
|
| 301 | function useInRouterContext() {
|
| 302 | return React.useContext(LocationContext) != null;
|
| 303 | }
|
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 |
|
| 314 |
|
| 315 | function useLocation() {
|
| 316 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false,
|
| 317 |
|
| 318 | "useLocation() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
| 319 | return React.useContext(LocationContext).location;
|
| 320 | }
|
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 |
|
| 327 |
|
| 328 | function useNavigationType() {
|
| 329 | return React.useContext(LocationContext).navigationType;
|
| 330 | }
|
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 |
|
| 338 |
|
| 339 | function useMatch(pattern) {
|
| 340 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false,
|
| 341 |
|
| 342 | "useMatch() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
| 343 | let {
|
| 344 | pathname
|
| 345 | } = useLocation();
|
| 346 | return React.useMemo(() => matchPath(pattern, pathname), [pathname, pattern]);
|
| 347 | }
|
| 348 | |
| 349 | |
| 350 |
|
| 351 |
|
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 |
|
| 358 | function useNavigate() {
|
| 359 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false,
|
| 360 |
|
| 361 | "useNavigate() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
| 362 | let {
|
| 363 | basename,
|
| 364 | navigator
|
| 365 | } = React.useContext(NavigationContext);
|
| 366 | let {
|
| 367 | matches
|
| 368 | } = React.useContext(RouteContext);
|
| 369 | let {
|
| 370 | pathname: locationPathname
|
| 371 | } = useLocation();
|
| 372 | let routePathnamesJson = JSON.stringify(UNSAFE_getPathContributingMatches(matches).map(match => match.pathnameBase));
|
| 373 | let activeRef = React.useRef(false);
|
| 374 | React.useEffect(() => {
|
| 375 | activeRef.current = true;
|
| 376 | });
|
| 377 | let navigate = React.useCallback(function (to, options) {
|
| 378 | if (options === void 0) {
|
| 379 | options = {};
|
| 380 | }
|
| 381 |
|
| 382 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(activeRef.current, "You should call navigate() in a React.useEffect(), not when " + "your component is first rendered.") : void 0;
|
| 383 | if (!activeRef.current) return;
|
| 384 |
|
| 385 | if (typeof to === "number") {
|
| 386 | navigator.go(to);
|
| 387 | return;
|
| 388 | }
|
| 389 |
|
| 390 | let path = resolveTo(to, JSON.parse(routePathnamesJson), locationPathname, options.relative === "path");
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 | if (basename !== "/") {
|
| 396 | path.pathname = path.pathname === "/" ? basename : joinPaths([basename, path.pathname]);
|
| 397 | }
|
| 398 |
|
| 399 | (!!options.replace ? navigator.replace : navigator.push)(path, options.state, options);
|
| 400 | }, [basename, navigator, routePathnamesJson, locationPathname]);
|
| 401 | return navigate;
|
| 402 | }
|
| 403 | const OutletContext = React.createContext(null);
|
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 |
|
| 409 |
|
| 410 | function useOutletContext() {
|
| 411 | return React.useContext(OutletContext);
|
| 412 | }
|
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 |
|
| 419 |
|
| 420 | function useOutlet(context) {
|
| 421 | let outlet = React.useContext(RouteContext).outlet;
|
| 422 |
|
| 423 | if (outlet) {
|
| 424 | return React.createElement(OutletContext.Provider, {
|
| 425 | value: context
|
| 426 | }, outlet);
|
| 427 | }
|
| 428 |
|
| 429 | return outlet;
|
| 430 | }
|
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 |
|
| 437 |
|
| 438 | function useParams() {
|
| 439 | let {
|
| 440 | matches
|
| 441 | } = React.useContext(RouteContext);
|
| 442 | let routeMatch = matches[matches.length - 1];
|
| 443 | return routeMatch ? routeMatch.params : {};
|
| 444 | }
|
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 |
|
| 450 |
|
| 451 | function useResolvedPath(to, _temp2) {
|
| 452 | let {
|
| 453 | relative
|
| 454 | } = _temp2 === void 0 ? {} : _temp2;
|
| 455 | let {
|
| 456 | matches
|
| 457 | } = React.useContext(RouteContext);
|
| 458 | let {
|
| 459 | pathname: locationPathname
|
| 460 | } = useLocation();
|
| 461 | let routePathnamesJson = JSON.stringify(UNSAFE_getPathContributingMatches(matches).map(match => match.pathnameBase));
|
| 462 | return React.useMemo(() => resolveTo(to, JSON.parse(routePathnamesJson), locationPathname, relative === "path"), [to, routePathnamesJson, locationPathname, relative]);
|
| 463 | }
|
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 |
|
| 472 |
|
| 473 | function useRoutes(routes, locationArg) {
|
| 474 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false,
|
| 475 |
|
| 476 | "useRoutes() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
| 477 | let {
|
| 478 | navigator
|
| 479 | } = React.useContext(NavigationContext);
|
| 480 | let dataRouterStateContext = React.useContext(DataRouterStateContext);
|
| 481 | let {
|
| 482 | matches: parentMatches
|
| 483 | } = React.useContext(RouteContext);
|
| 484 | let routeMatch = parentMatches[parentMatches.length - 1];
|
| 485 | let parentParams = routeMatch ? routeMatch.params : {};
|
| 486 | let parentPathname = routeMatch ? routeMatch.pathname : "/";
|
| 487 | let parentPathnameBase = routeMatch ? routeMatch.pathnameBase : "/";
|
| 488 | let parentRoute = routeMatch && routeMatch.route;
|
| 489 |
|
| 490 | if (process.env.NODE_ENV !== "production") {
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 | let parentPath = parentRoute && parentRoute.path || "";
|
| 512 | 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 + "/*") + "\">."));
|
| 513 | }
|
| 514 |
|
| 515 | let locationFromContext = useLocation();
|
| 516 | let location;
|
| 517 |
|
| 518 | if (locationArg) {
|
| 519 | var _parsedLocationArg$pa;
|
| 520 |
|
| 521 | let parsedLocationArg = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
|
| 522 | !(parentPathnameBase === "/" || ((_parsedLocationArg$pa = parsedLocationArg.pathname) == null ? void 0 : _parsedLocationArg$pa.startsWith(parentPathnameBase))) ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "When overriding the location using `<Routes location>` or `useRoutes(routes, location)`, " + "the location pathname must begin with the portion of the URL pathname that was " + ("matched by all parent routes. The current pathname base is \"" + parentPathnameBase + "\" ") + ("but pathname \"" + parsedLocationArg.pathname + "\" was given in the `location` prop.")) : UNSAFE_invariant(false) : void 0;
|
| 523 | location = parsedLocationArg;
|
| 524 | } else {
|
| 525 | location = locationFromContext;
|
| 526 | }
|
| 527 |
|
| 528 | let pathname = location.pathname || "/";
|
| 529 | let remainingPathname = parentPathnameBase === "/" ? pathname : pathname.slice(parentPathnameBase.length) || "/";
|
| 530 | let matches = matchRoutes(routes, {
|
| 531 | pathname: remainingPathname
|
| 532 | });
|
| 533 |
|
| 534 | if (process.env.NODE_ENV !== "production") {
|
| 535 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(parentRoute || matches != null, "No routes matched location \"" + location.pathname + location.search + location.hash + "\" ") : void 0;
|
| 536 | process.env.NODE_ENV !== "production" ? 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.") : void 0;
|
| 537 | }
|
| 538 |
|
| 539 | let renderedMatches = _renderMatches(matches && matches.map(match => Object.assign({}, match, {
|
| 540 | params: Object.assign({}, parentParams, match.params),
|
| 541 | pathname: joinPaths([parentPathnameBase,
|
| 542 | navigator.encodeLocation ? navigator.encodeLocation(match.pathname).pathname : match.pathname]),
|
| 543 | pathnameBase: match.pathnameBase === "/" ? parentPathnameBase : joinPaths([parentPathnameBase,
|
| 544 | navigator.encodeLocation ? navigator.encodeLocation(match.pathnameBase).pathname : match.pathnameBase])
|
| 545 | })), parentMatches, dataRouterStateContext || undefined);
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 | if (locationArg && renderedMatches) {
|
| 551 | return React.createElement(LocationContext.Provider, {
|
| 552 | value: {
|
| 553 | location: _extends({
|
| 554 | pathname: "/",
|
| 555 | search: "",
|
| 556 | hash: "",
|
| 557 | state: null,
|
| 558 | key: "default"
|
| 559 | }, location),
|
| 560 | navigationType: Action.Pop
|
| 561 | }
|
| 562 | }, renderedMatches);
|
| 563 | }
|
| 564 |
|
| 565 | return renderedMatches;
|
| 566 | }
|
| 567 |
|
| 568 | function DefaultErrorComponent() {
|
| 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 | let devInfo = null;
|
| 582 |
|
| 583 | if (process.env.NODE_ENV !== "production") {
|
| 584 | 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\xA0", React.createElement("code", {
|
| 585 | style: codeStyles
|
| 586 | }, "ErrorBoundary"), " prop on\xA0", React.createElement("code", {
|
| 587 | style: codeStyles
|
| 588 | }, "<Route>")));
|
| 589 | }
|
| 590 |
|
| 591 | return React.createElement(React.Fragment, null, React.createElement("h2", null, "Unexpected Application Error!"), React.createElement("h3", {
|
| 592 | style: {
|
| 593 | fontStyle: "italic"
|
| 594 | }
|
| 595 | }, message), stack ? React.createElement("pre", {
|
| 596 | style: preStyles
|
| 597 | }, stack) : null, devInfo);
|
| 598 | }
|
| 599 |
|
| 600 | class RenderErrorBoundary extends React.Component {
|
| 601 | constructor(props) {
|
| 602 | super(props);
|
| 603 | this.state = {
|
| 604 | location: props.location,
|
| 605 | error: props.error
|
| 606 | };
|
| 607 | }
|
| 608 |
|
| 609 | static getDerivedStateFromError(error) {
|
| 610 | return {
|
| 611 | error: error
|
| 612 | };
|
| 613 | }
|
| 614 |
|
| 615 | static getDerivedStateFromProps(props, state) {
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 | if (state.location !== props.location) {
|
| 625 | return {
|
| 626 | error: props.error,
|
| 627 | location: props.location
|
| 628 | };
|
| 629 | }
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 | return {
|
| 636 | error: props.error || state.error,
|
| 637 | location: state.location
|
| 638 | };
|
| 639 | }
|
| 640 |
|
| 641 | componentDidCatch(error, errorInfo) {
|
| 642 | console.error("React Router caught the following error during render", error, errorInfo);
|
| 643 | }
|
| 644 |
|
| 645 | render() {
|
| 646 | return this.state.error ? React.createElement(RouteContext.Provider, {
|
| 647 | value: this.props.routeContext
|
| 648 | }, React.createElement(RouteErrorContext.Provider, {
|
| 649 | value: this.state.error,
|
| 650 | children: this.props.component
|
| 651 | })) : this.props.children;
|
| 652 | }
|
| 653 |
|
| 654 | }
|
| 655 |
|
| 656 | function RenderedRoute(_ref) {
|
| 657 | let {
|
| 658 | routeContext,
|
| 659 | match,
|
| 660 | children
|
| 661 | } = _ref;
|
| 662 | let dataRouterContext = React.useContext(DataRouterContext);
|
| 663 |
|
| 664 |
|
| 665 | if (dataRouterContext && dataRouterContext.static && dataRouterContext.staticContext && (match.route.errorElement || match.route.ErrorBoundary)) {
|
| 666 | dataRouterContext.staticContext._deepestRenderedBoundaryId = match.route.id;
|
| 667 | }
|
| 668 |
|
| 669 | return React.createElement(RouteContext.Provider, {
|
| 670 | value: routeContext
|
| 671 | }, children);
|
| 672 | }
|
| 673 |
|
| 674 | function _renderMatches(matches, parentMatches, dataRouterState) {
|
| 675 | if (parentMatches === void 0) {
|
| 676 | parentMatches = [];
|
| 677 | }
|
| 678 |
|
| 679 | if (matches == null) {
|
| 680 | if (dataRouterState != null && dataRouterState.errors) {
|
| 681 |
|
| 682 |
|
| 683 | matches = dataRouterState.matches;
|
| 684 | } else {
|
| 685 | return null;
|
| 686 | }
|
| 687 | }
|
| 688 |
|
| 689 | let renderedMatches = matches;
|
| 690 |
|
| 691 | let errors = dataRouterState == null ? void 0 : dataRouterState.errors;
|
| 692 |
|
| 693 | if (errors != null) {
|
| 694 | let errorIndex = renderedMatches.findIndex(m => m.route.id && (errors == null ? void 0 : errors[m.route.id]));
|
| 695 | !(errorIndex >= 0) ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "Could not find a matching route for the current errors: " + errors) : UNSAFE_invariant(false) : void 0;
|
| 696 | renderedMatches = renderedMatches.slice(0, Math.min(renderedMatches.length, errorIndex + 1));
|
| 697 | }
|
| 698 |
|
| 699 | return renderedMatches.reduceRight((outlet, match, index) => {
|
| 700 | let error = match.route.id ? errors == null ? void 0 : errors[match.route.id] : null;
|
| 701 |
|
| 702 | let errorElement = null;
|
| 703 |
|
| 704 | if (dataRouterState) {
|
| 705 | if (match.route.ErrorBoundary) {
|
| 706 | errorElement = React.createElement(match.route.ErrorBoundary, null);
|
| 707 | } else if (match.route.errorElement) {
|
| 708 | errorElement = match.route.errorElement;
|
| 709 | } else {
|
| 710 | errorElement = React.createElement(DefaultErrorComponent, null);
|
| 711 | }
|
| 712 | }
|
| 713 |
|
| 714 | let matches = parentMatches.concat(renderedMatches.slice(0, index + 1));
|
| 715 |
|
| 716 | let getChildren = () => {
|
| 717 | let children = outlet;
|
| 718 |
|
| 719 | if (error) {
|
| 720 | children = errorElement;
|
| 721 | } else if (match.route.Component) {
|
| 722 | children = React.createElement(match.route.Component, null);
|
| 723 | } else if (match.route.element) {
|
| 724 | children = match.route.element;
|
| 725 | }
|
| 726 |
|
| 727 | return React.createElement(RenderedRoute, {
|
| 728 | match: match,
|
| 729 | routeContext: {
|
| 730 | outlet,
|
| 731 | matches
|
| 732 | },
|
| 733 | children: children
|
| 734 | });
|
| 735 | };
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 | return dataRouterState && (match.route.ErrorBoundary || match.route.errorElement || index === 0) ? React.createElement(RenderErrorBoundary, {
|
| 741 | location: dataRouterState.location,
|
| 742 | component: errorElement,
|
| 743 | error: error,
|
| 744 | children: getChildren(),
|
| 745 | routeContext: {
|
| 746 | outlet: null,
|
| 747 | matches
|
| 748 | }
|
| 749 | }) : getChildren();
|
| 750 | }, null);
|
| 751 | }
|
| 752 | var DataRouterHook;
|
| 753 |
|
| 754 | (function (DataRouterHook) {
|
| 755 | DataRouterHook["UseBlocker"] = "useBlocker";
|
| 756 | DataRouterHook["UseRevalidator"] = "useRevalidator";
|
| 757 | })(DataRouterHook || (DataRouterHook = {}));
|
| 758 |
|
| 759 | var DataRouterStateHook;
|
| 760 |
|
| 761 | (function (DataRouterStateHook) {
|
| 762 | DataRouterStateHook["UseBlocker"] = "useBlocker";
|
| 763 | DataRouterStateHook["UseLoaderData"] = "useLoaderData";
|
| 764 | DataRouterStateHook["UseActionData"] = "useActionData";
|
| 765 | DataRouterStateHook["UseRouteError"] = "useRouteError";
|
| 766 | DataRouterStateHook["UseNavigation"] = "useNavigation";
|
| 767 | DataRouterStateHook["UseRouteLoaderData"] = "useRouteLoaderData";
|
| 768 | DataRouterStateHook["UseMatches"] = "useMatches";
|
| 769 | DataRouterStateHook["UseRevalidator"] = "useRevalidator";
|
| 770 | })(DataRouterStateHook || (DataRouterStateHook = {}));
|
| 771 |
|
| 772 | function getDataRouterConsoleError(hookName) {
|
| 773 | return hookName + " must be used within a data router. See https://reactrouter.com/routers/picking-a-router.";
|
| 774 | }
|
| 775 |
|
| 776 | function useDataRouterContext(hookName) {
|
| 777 | let ctx = React.useContext(DataRouterContext);
|
| 778 | !ctx ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, getDataRouterConsoleError(hookName)) : UNSAFE_invariant(false) : void 0;
|
| 779 | return ctx;
|
| 780 | }
|
| 781 |
|
| 782 | function useDataRouterState(hookName) {
|
| 783 | let state = React.useContext(DataRouterStateContext);
|
| 784 | !state ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, getDataRouterConsoleError(hookName)) : UNSAFE_invariant(false) : void 0;
|
| 785 | return state;
|
| 786 | }
|
| 787 |
|
| 788 | function useRouteContext(hookName) {
|
| 789 | let route = React.useContext(RouteContext);
|
| 790 | !route ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, getDataRouterConsoleError(hookName)) : UNSAFE_invariant(false) : void 0;
|
| 791 | return route;
|
| 792 | }
|
| 793 |
|
| 794 | function useCurrentRouteId(hookName) {
|
| 795 | let route = useRouteContext(hookName);
|
| 796 | let thisRoute = route.matches[route.matches.length - 1];
|
| 797 | !thisRoute.route.id ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, hookName + " can only be used on routes that contain a unique \"id\"") : UNSAFE_invariant(false) : void 0;
|
| 798 | return thisRoute.route.id;
|
| 799 | }
|
| 800 | |
| 801 | |
| 802 | |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 | function useNavigation() {
|
| 807 | let state = useDataRouterState(DataRouterStateHook.UseNavigation);
|
| 808 | return state.navigation;
|
| 809 | }
|
| 810 | |
| 811 | |
| 812 | |
| 813 |
|
| 814 |
|
| 815 | function useRevalidator() {
|
| 816 | let dataRouterContext = useDataRouterContext(DataRouterHook.UseRevalidator);
|
| 817 | let state = useDataRouterState(DataRouterStateHook.UseRevalidator);
|
| 818 | return {
|
| 819 | revalidate: dataRouterContext.router.revalidate,
|
| 820 | state: state.revalidation
|
| 821 | };
|
| 822 | }
|
| 823 | |
| 824 | |
| 825 | |
| 826 |
|
| 827 |
|
| 828 | function useMatches() {
|
| 829 | let {
|
| 830 | matches,
|
| 831 | loaderData
|
| 832 | } = useDataRouterState(DataRouterStateHook.UseMatches);
|
| 833 | return React.useMemo(() => matches.map(match => {
|
| 834 | let {
|
| 835 | pathname,
|
| 836 | params
|
| 837 | } = match;
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 | return {
|
| 842 | id: match.route.id,
|
| 843 | pathname,
|
| 844 | params,
|
| 845 | data: loaderData[match.route.id],
|
| 846 | handle: match.route.handle
|
| 847 | };
|
| 848 | }), [matches, loaderData]);
|
| 849 | }
|
| 850 | |
| 851 | |
| 852 |
|
| 853 |
|
| 854 | function useLoaderData() {
|
| 855 | let state = useDataRouterState(DataRouterStateHook.UseLoaderData);
|
| 856 | let routeId = useCurrentRouteId(DataRouterStateHook.UseLoaderData);
|
| 857 |
|
| 858 | if (state.errors && state.errors[routeId] != null) {
|
| 859 | console.error("You cannot `useLoaderData` in an errorElement (routeId: " + routeId + ")");
|
| 860 | return undefined;
|
| 861 | }
|
| 862 |
|
| 863 | return state.loaderData[routeId];
|
| 864 | }
|
| 865 | |
| 866 | |
| 867 |
|
| 868 |
|
| 869 | function useRouteLoaderData(routeId) {
|
| 870 | let state = useDataRouterState(DataRouterStateHook.UseRouteLoaderData);
|
| 871 | return state.loaderData[routeId];
|
| 872 | }
|
| 873 | |
| 874 | |
| 875 |
|
| 876 |
|
| 877 | function useActionData() {
|
| 878 | let state = useDataRouterState(DataRouterStateHook.UseActionData);
|
| 879 | let route = React.useContext(RouteContext);
|
| 880 | !route ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "useActionData must be used inside a RouteContext") : UNSAFE_invariant(false) : void 0;
|
| 881 | return Object.values((state == null ? void 0 : state.actionData) || {})[0];
|
| 882 | }
|
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 |
|
| 888 |
|
| 889 | function useRouteError() {
|
| 890 | var _state$errors;
|
| 891 |
|
| 892 | let error = React.useContext(RouteErrorContext);
|
| 893 | let state = useDataRouterState(DataRouterStateHook.UseRouteError);
|
| 894 | let routeId = useCurrentRouteId(DataRouterStateHook.UseRouteError);
|
| 895 |
|
| 896 |
|
| 897 | if (error) {
|
| 898 | return error;
|
| 899 | }
|
| 900 |
|
| 901 |
|
| 902 | return (_state$errors = state.errors) == null ? void 0 : _state$errors[routeId];
|
| 903 | }
|
| 904 | |
| 905 | |
| 906 |
|
| 907 |
|
| 908 | function useAsyncValue() {
|
| 909 | let value = React.useContext(AwaitContext);
|
| 910 | return value == null ? void 0 : value._data;
|
| 911 | }
|
| 912 | |
| 913 | |
| 914 |
|
| 915 |
|
| 916 | function useAsyncError() {
|
| 917 | let value = React.useContext(AwaitContext);
|
| 918 | return value == null ? void 0 : value._error;
|
| 919 | }
|
| 920 | let blockerId = 0;
|
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 |
|
| 927 |
|
| 928 | function useBlocker(shouldBlock) {
|
| 929 | let {
|
| 930 | router
|
| 931 | } = useDataRouterContext(DataRouterHook.UseBlocker);
|
| 932 | let state = useDataRouterState(DataRouterStateHook.UseBlocker);
|
| 933 | let [blockerKey] = React.useState(() => String(++blockerId));
|
| 934 | let blockerFunction = React.useCallback(args => {
|
| 935 | return typeof shouldBlock === "function" ? !!shouldBlock(args) : !!shouldBlock;
|
| 936 | }, [shouldBlock]);
|
| 937 | let blocker = router.getBlocker(blockerKey, blockerFunction);
|
| 938 |
|
| 939 | React.useEffect(() => () => router.deleteBlocker(blockerKey), [router, blockerKey]);
|
| 940 |
|
| 941 |
|
| 942 | return state.blockers.get(blockerKey) || blocker;
|
| 943 | }
|
| 944 | const alreadyWarned = {};
|
| 945 |
|
| 946 | function warningOnce(key, cond, message) {
|
| 947 | if (!cond && !alreadyWarned[key]) {
|
| 948 | alreadyWarned[key] = true;
|
| 949 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(false, message) : void 0;
|
| 950 | }
|
| 951 | }
|
| 952 |
|
| 953 | |
| 954 | |
| 955 |
|
| 956 | function RouterProvider(_ref) {
|
| 957 | let {
|
| 958 | fallbackElement,
|
| 959 | router
|
| 960 | } = _ref;
|
| 961 | let getState = React.useCallback(() => router.state, [router]);
|
| 962 |
|
| 963 | let state = useSyncExternalStore(router.subscribe, getState,
|
| 964 |
|
| 965 |
|
| 966 | getState);
|
| 967 | let navigator = React.useMemo(() => {
|
| 968 | return {
|
| 969 | createHref: router.createHref,
|
| 970 | encodeLocation: router.encodeLocation,
|
| 971 | go: n => router.navigate(n),
|
| 972 | push: (to, state, opts) => router.navigate(to, {
|
| 973 | state,
|
| 974 | preventScrollReset: opts == null ? void 0 : opts.preventScrollReset
|
| 975 | }),
|
| 976 | replace: (to, state, opts) => router.navigate(to, {
|
| 977 | replace: true,
|
| 978 | state,
|
| 979 | preventScrollReset: opts == null ? void 0 : opts.preventScrollReset
|
| 980 | })
|
| 981 | };
|
| 982 | }, [router]);
|
| 983 | let basename = router.basename || "/";
|
| 984 | let dataRouterContext = React.useMemo(() => ({
|
| 985 | router,
|
| 986 | navigator,
|
| 987 | static: false,
|
| 988 | basename
|
| 989 | }), [router, navigator, basename]);
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 | return React.createElement(React.Fragment, null, React.createElement(DataRouterContext.Provider, {
|
| 997 | value: dataRouterContext
|
| 998 | }, React.createElement(DataRouterStateContext.Provider, {
|
| 999 | value: state
|
| 1000 | }, React.createElement(Router, {
|
| 1001 | basename: router.basename,
|
| 1002 | location: router.state.location,
|
| 1003 | navigationType: router.state.historyAction,
|
| 1004 | navigator: navigator
|
| 1005 | }, router.state.initialized ? React.createElement(Routes, null) : fallbackElement))), null);
|
| 1006 | }
|
| 1007 |
|
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 |
|
| 1013 | function MemoryRouter(_ref2) {
|
| 1014 | let {
|
| 1015 | basename,
|
| 1016 | children,
|
| 1017 | initialEntries,
|
| 1018 | initialIndex
|
| 1019 | } = _ref2;
|
| 1020 | let historyRef = React.useRef();
|
| 1021 |
|
| 1022 | if (historyRef.current == null) {
|
| 1023 | historyRef.current = createMemoryHistory({
|
| 1024 | initialEntries,
|
| 1025 | initialIndex,
|
| 1026 | v5Compat: true
|
| 1027 | });
|
| 1028 | }
|
| 1029 |
|
| 1030 | let history = historyRef.current;
|
| 1031 | let [state, setState] = React.useState({
|
| 1032 | action: history.action,
|
| 1033 | location: history.location
|
| 1034 | });
|
| 1035 | React.useLayoutEffect(() => history.listen(setState), [history]);
|
| 1036 | return React.createElement(Router, {
|
| 1037 | basename: basename,
|
| 1038 | children: children,
|
| 1039 | location: state.location,
|
| 1040 | navigationType: state.action,
|
| 1041 | navigator: history
|
| 1042 | });
|
| 1043 | }
|
| 1044 |
|
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 |
|
| 1054 | function Navigate(_ref3) {
|
| 1055 | let {
|
| 1056 | to,
|
| 1057 | replace,
|
| 1058 | state,
|
| 1059 | relative
|
| 1060 | } = _ref3;
|
| 1061 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false,
|
| 1062 |
|
| 1063 | "<Navigate> may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
| 1064 | process.env.NODE_ENV !== "production" ? 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.") : void 0;
|
| 1065 | let dataRouterState = React.useContext(DataRouterStateContext);
|
| 1066 | let navigate = useNavigate();
|
| 1067 | React.useEffect(() => {
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 | if (dataRouterState && dataRouterState.navigation.state !== "idle") {
|
| 1072 | return;
|
| 1073 | }
|
| 1074 |
|
| 1075 | navigate(to, {
|
| 1076 | replace,
|
| 1077 | state,
|
| 1078 | relative
|
| 1079 | });
|
| 1080 | });
|
| 1081 | return null;
|
| 1082 | }
|
| 1083 |
|
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 |
|
| 1089 | function Outlet(props) {
|
| 1090 | return useOutlet(props.context);
|
| 1091 | }
|
| 1092 |
|
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 |
|
| 1098 | function Route(_props) {
|
| 1099 | process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "A <Route> is only ever to be used as the child of <Routes> element, " + "never rendered directly. Please wrap your <Route> in a <Routes>.") : UNSAFE_invariant(false) ;
|
| 1100 | }
|
| 1101 |
|
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 |
|
| 1111 | function Router(_ref4) {
|
| 1112 | let {
|
| 1113 | basename: basenameProp = "/",
|
| 1114 | children = null,
|
| 1115 | location: locationProp,
|
| 1116 | navigationType = Action.Pop,
|
| 1117 | navigator,
|
| 1118 | static: staticProp = false
|
| 1119 | } = _ref4;
|
| 1120 | !!useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "You cannot render a <Router> inside another <Router>." + " You should never have more than one in your app.") : UNSAFE_invariant(false) : void 0;
|
| 1121 |
|
| 1122 |
|
| 1123 | let basename = basenameProp.replace(/^\/*/, "/");
|
| 1124 | let navigationContext = React.useMemo(() => ({
|
| 1125 | basename,
|
| 1126 | navigator,
|
| 1127 | static: staticProp
|
| 1128 | }), [basename, navigator, staticProp]);
|
| 1129 |
|
| 1130 | if (typeof locationProp === "string") {
|
| 1131 | locationProp = parsePath(locationProp);
|
| 1132 | }
|
| 1133 |
|
| 1134 | let {
|
| 1135 | pathname = "/",
|
| 1136 | search = "",
|
| 1137 | hash = "",
|
| 1138 | state = null,
|
| 1139 | key = "default"
|
| 1140 | } = locationProp;
|
| 1141 | let locationContext = React.useMemo(() => {
|
| 1142 | let trailingPathname = stripBasename(pathname, basename);
|
| 1143 |
|
| 1144 | if (trailingPathname == null) {
|
| 1145 | return null;
|
| 1146 | }
|
| 1147 |
|
| 1148 | return {
|
| 1149 | location: {
|
| 1150 | pathname: trailingPathname,
|
| 1151 | search,
|
| 1152 | hash,
|
| 1153 | state,
|
| 1154 | key
|
| 1155 | },
|
| 1156 | navigationType
|
| 1157 | };
|
| 1158 | }, [basename, pathname, search, hash, state, key, navigationType]);
|
| 1159 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(locationContext != null, "<Router basename=\"" + basename + "\"> is not able to match the URL " + ("\"" + pathname + search + hash + "\" because it does not start with the ") + "basename, so the <Router> won't render anything.") : void 0;
|
| 1160 |
|
| 1161 | if (locationContext == null) {
|
| 1162 | return null;
|
| 1163 | }
|
| 1164 |
|
| 1165 | return React.createElement(NavigationContext.Provider, {
|
| 1166 | value: navigationContext
|
| 1167 | }, React.createElement(LocationContext.Provider, {
|
| 1168 | children: children,
|
| 1169 | value: locationContext
|
| 1170 | }));
|
| 1171 | }
|
| 1172 |
|
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 |
|
| 1179 | function Routes(_ref5) {
|
| 1180 | let {
|
| 1181 | children,
|
| 1182 | location
|
| 1183 | } = _ref5;
|
| 1184 | let dataRouterContext = React.useContext(DataRouterContext);
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 | let routes = dataRouterContext && !children ? dataRouterContext.router.routes : createRoutesFromChildren(children);
|
| 1189 | return useRoutes(routes, location);
|
| 1190 | }
|
| 1191 |
|
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 |
|
| 1196 | function Await(_ref6) {
|
| 1197 | let {
|
| 1198 | children,
|
| 1199 | errorElement,
|
| 1200 | resolve
|
| 1201 | } = _ref6;
|
| 1202 | return React.createElement(AwaitErrorBoundary, {
|
| 1203 | resolve: resolve,
|
| 1204 | errorElement: errorElement
|
| 1205 | }, React.createElement(ResolveAwait, null, children));
|
| 1206 | }
|
| 1207 | var AwaitRenderStatus;
|
| 1208 |
|
| 1209 | (function (AwaitRenderStatus) {
|
| 1210 | AwaitRenderStatus[AwaitRenderStatus["pending"] = 0] = "pending";
|
| 1211 | AwaitRenderStatus[AwaitRenderStatus["success"] = 1] = "success";
|
| 1212 | AwaitRenderStatus[AwaitRenderStatus["error"] = 2] = "error";
|
| 1213 | })(AwaitRenderStatus || (AwaitRenderStatus = {}));
|
| 1214 |
|
| 1215 | const neverSettledPromise = new Promise(() => {});
|
| 1216 |
|
| 1217 | class AwaitErrorBoundary extends React.Component {
|
| 1218 | constructor(props) {
|
| 1219 | super(props);
|
| 1220 | this.state = {
|
| 1221 | error: null
|
| 1222 | };
|
| 1223 | }
|
| 1224 |
|
| 1225 | static getDerivedStateFromError(error) {
|
| 1226 | return {
|
| 1227 | error
|
| 1228 | };
|
| 1229 | }
|
| 1230 |
|
| 1231 | componentDidCatch(error, errorInfo) {
|
| 1232 | console.error("<Await> caught the following error during render", error, errorInfo);
|
| 1233 | }
|
| 1234 |
|
| 1235 | render() {
|
| 1236 | let {
|
| 1237 | children,
|
| 1238 | errorElement,
|
| 1239 | resolve
|
| 1240 | } = this.props;
|
| 1241 | let promise = null;
|
| 1242 | let status = AwaitRenderStatus.pending;
|
| 1243 |
|
| 1244 | if (!(resolve instanceof Promise)) {
|
| 1245 |
|
| 1246 | status = AwaitRenderStatus.success;
|
| 1247 | promise = Promise.resolve();
|
| 1248 | Object.defineProperty(promise, "_tracked", {
|
| 1249 | get: () => true
|
| 1250 | });
|
| 1251 | Object.defineProperty(promise, "_data", {
|
| 1252 | get: () => resolve
|
| 1253 | });
|
| 1254 | } else if (this.state.error) {
|
| 1255 |
|
| 1256 | status = AwaitRenderStatus.error;
|
| 1257 | let renderError = this.state.error;
|
| 1258 | promise = Promise.reject().catch(() => {});
|
| 1259 |
|
| 1260 | Object.defineProperty(promise, "_tracked", {
|
| 1261 | get: () => true
|
| 1262 | });
|
| 1263 | Object.defineProperty(promise, "_error", {
|
| 1264 | get: () => renderError
|
| 1265 | });
|
| 1266 | } else if (resolve._tracked) {
|
| 1267 |
|
| 1268 | promise = resolve;
|
| 1269 | status = promise._error !== undefined ? AwaitRenderStatus.error : promise._data !== undefined ? AwaitRenderStatus.success : AwaitRenderStatus.pending;
|
| 1270 | } else {
|
| 1271 |
|
| 1272 | status = AwaitRenderStatus.pending;
|
| 1273 | Object.defineProperty(resolve, "_tracked", {
|
| 1274 | get: () => true
|
| 1275 | });
|
| 1276 | promise = resolve.then(data => Object.defineProperty(resolve, "_data", {
|
| 1277 | get: () => data
|
| 1278 | }), error => Object.defineProperty(resolve, "_error", {
|
| 1279 | get: () => error
|
| 1280 | }));
|
| 1281 | }
|
| 1282 |
|
| 1283 | if (status === AwaitRenderStatus.error && promise._error instanceof AbortedDeferredError) {
|
| 1284 |
|
| 1285 | throw neverSettledPromise;
|
| 1286 | }
|
| 1287 |
|
| 1288 | if (status === AwaitRenderStatus.error && !errorElement) {
|
| 1289 |
|
| 1290 | throw promise._error;
|
| 1291 | }
|
| 1292 |
|
| 1293 | if (status === AwaitRenderStatus.error) {
|
| 1294 |
|
| 1295 | return React.createElement(AwaitContext.Provider, {
|
| 1296 | value: promise,
|
| 1297 | children: errorElement
|
| 1298 | });
|
| 1299 | }
|
| 1300 |
|
| 1301 | if (status === AwaitRenderStatus.success) {
|
| 1302 |
|
| 1303 | return React.createElement(AwaitContext.Provider, {
|
| 1304 | value: promise,
|
| 1305 | children: children
|
| 1306 | });
|
| 1307 | }
|
| 1308 |
|
| 1309 |
|
| 1310 | throw promise;
|
| 1311 | }
|
| 1312 |
|
| 1313 | }
|
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 | function ResolveAwait(_ref7) {
|
| 1321 | let {
|
| 1322 | children
|
| 1323 | } = _ref7;
|
| 1324 | let data = useAsyncValue();
|
| 1325 | let toRender = typeof children === "function" ? children(data) : children;
|
| 1326 | return React.createElement(React.Fragment, null, toRender);
|
| 1327 | }
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 | |
| 1332 | |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 | function createRoutesFromChildren(children, parentPath) {
|
| 1341 | if (parentPath === void 0) {
|
| 1342 | parentPath = [];
|
| 1343 | }
|
| 1344 |
|
| 1345 | let routes = [];
|
| 1346 | React.Children.forEach(children, (element, index) => {
|
| 1347 | if (! React.isValidElement(element)) {
|
| 1348 |
|
| 1349 |
|
| 1350 | return;
|
| 1351 | }
|
| 1352 |
|
| 1353 | let treePath = [...parentPath, index];
|
| 1354 |
|
| 1355 | if (element.type === React.Fragment) {
|
| 1356 |
|
| 1357 | routes.push.apply(routes, createRoutesFromChildren(element.props.children, treePath));
|
| 1358 | return;
|
| 1359 | }
|
| 1360 |
|
| 1361 | !(element.type === Route) ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "[" + (typeof element.type === "string" ? element.type : element.type.name) + "] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>") : UNSAFE_invariant(false) : void 0;
|
| 1362 | !(!element.props.index || !element.props.children) ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "An index route cannot have child routes.") : UNSAFE_invariant(false) : void 0;
|
| 1363 | let route = {
|
| 1364 | id: element.props.id || treePath.join("-"),
|
| 1365 | caseSensitive: element.props.caseSensitive,
|
| 1366 | element: element.props.element,
|
| 1367 | Component: element.props.Component,
|
| 1368 | index: element.props.index,
|
| 1369 | path: element.props.path,
|
| 1370 | loader: element.props.loader,
|
| 1371 | action: element.props.action,
|
| 1372 | errorElement: element.props.errorElement,
|
| 1373 | ErrorBoundary: element.props.ErrorBoundary,
|
| 1374 | hasErrorBoundary: element.props.ErrorBoundary != null || element.props.errorElement != null,
|
| 1375 | shouldRevalidate: element.props.shouldRevalidate,
|
| 1376 | handle: element.props.handle,
|
| 1377 | lazy: element.props.lazy
|
| 1378 | };
|
| 1379 |
|
| 1380 | if (element.props.children) {
|
| 1381 | route.children = createRoutesFromChildren(element.props.children, treePath);
|
| 1382 | }
|
| 1383 |
|
| 1384 | routes.push(route);
|
| 1385 | });
|
| 1386 | return routes;
|
| 1387 | }
|
| 1388 | |
| 1389 | |
| 1390 |
|
| 1391 |
|
| 1392 | function renderMatches(matches) {
|
| 1393 | return _renderMatches(matches);
|
| 1394 | }
|
| 1395 |
|
| 1396 | function detectErrorBoundary(route) {
|
| 1397 | if (process.env.NODE_ENV !== "production") {
|
| 1398 | if (route.Component && route.element) {
|
| 1399 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(false, "You should not include both `Component` and `element` on your route - " + "`element` will be ignored.") : void 0;
|
| 1400 | }
|
| 1401 |
|
| 1402 | if (route.ErrorBoundary && route.errorElement) {
|
| 1403 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(false, "You should not include both `ErrorBoundary` and `errorElement` on your route - " + "`errorElement` will be ignored.") : void 0;
|
| 1404 | }
|
| 1405 | }
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 | return Boolean(route.ErrorBoundary) || Boolean(route.errorElement);
|
| 1410 | }
|
| 1411 |
|
| 1412 | function createMemoryRouter(routes, opts) {
|
| 1413 | return createRouter({
|
| 1414 | basename: opts == null ? void 0 : opts.basename,
|
| 1415 | future: opts == null ? void 0 : opts.future,
|
| 1416 | history: createMemoryHistory({
|
| 1417 | initialEntries: opts == null ? void 0 : opts.initialEntries,
|
| 1418 | initialIndex: opts == null ? void 0 : opts.initialIndex
|
| 1419 | }),
|
| 1420 | hydrationData: opts == null ? void 0 : opts.hydrationData,
|
| 1421 | routes,
|
| 1422 | detectErrorBoundary
|
| 1423 | }).initialize();
|
| 1424 | }
|
| 1425 |
|
| 1426 | 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, detectErrorBoundary as UNSAFE_detectErrorBoundary, createMemoryRouter, createRoutesFromChildren, createRoutesFromChildren as createRoutesFromElements, renderMatches, useBlocker as unstable_useBlocker, useActionData, useAsyncError, useAsyncValue, useHref, useInRouterContext, useLoaderData, useLocation, useMatch, useMatches, useNavigate, useNavigation, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRevalidator, useRouteError, useRouteLoaderData, useRoutes };
|
| 1427 |
|