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