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