| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | import { createMemoryHistory, createPath, invariant, parsePath, warning } from "./router/history.js";
|
| 12 | import { defaultMapRouteProperties, getResolveToMatches, getRoutePattern, resolveTo, stripBasename } from "./router/utils.js";
|
| 13 | import { getNavigatorCurrentUrl, validateNavigationTarget } from "./router/navigation.js";
|
| 14 | import { createRouter } from "./router/router.js";
|
| 15 | import { AwaitContext, DataRouterContext, DataRouterStateContext, FetchersContext, LocationContext, NavigationContext, RouteContext, ViewTransitionContext, useIsRSCRouterContext } from "./context.js";
|
| 16 | import { _renderMatches, useActionData, useAsyncValue, useInRouterContext, useLoaderData, useLocation, useMatches, useNavigate, useOutlet, useParams, useRouteError, useRoutes, useRoutesImpl } from "./hooks.js";
|
| 17 | import { warnOnce } from "./server-runtime/warnings.js";
|
| 18 | import * as React$1 from "react";
|
| 19 | import { useOptimistic } from "react";
|
| 20 |
|
| 21 | const hydrationRouteProperties = ["HydrateFallback", "hydrateFallbackElement"];
|
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 |
|
| 47 | function createMemoryRouter(routes, opts) {
|
| 48 | return createRouter({
|
| 49 | basename: opts?.basename,
|
| 50 | getContext: opts?.getContext,
|
| 51 | future: opts?.future,
|
| 52 | history: createMemoryHistory({
|
| 53 | initialEntries: opts?.initialEntries,
|
| 54 | initialIndex: opts?.initialIndex
|
| 55 | }),
|
| 56 | hydrationData: opts?.hydrationData,
|
| 57 | routes,
|
| 58 | mapRouteProperties: defaultMapRouteProperties,
|
| 59 | hydrationRouteProperties,
|
| 60 | dataStrategy: opts?.dataStrategy,
|
| 61 | patchRoutesOnNavigation: opts?.patchRoutesOnNavigation,
|
| 62 | instrumentations: opts?.instrumentations
|
| 63 | }).initialize();
|
| 64 | }
|
| 65 | var Deferred = class {
|
| 66 | status = "pending";
|
| 67 | promise;
|
| 68 | resolve;
|
| 69 | reject;
|
| 70 | constructor() {
|
| 71 | this.promise = new Promise((resolve, reject) => {
|
| 72 | this.resolve = (value) => {
|
| 73 | if (this.status === "pending") {
|
| 74 | this.status = "resolved";
|
| 75 | resolve(value);
|
| 76 | }
|
| 77 | };
|
| 78 | this.reject = (reason) => {
|
| 79 | if (this.status === "pending") {
|
| 80 | this.status = "rejected";
|
| 81 | reject(reason);
|
| 82 | }
|
| 83 | };
|
| 84 | });
|
| 85 | }
|
| 86 | };
|
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 |
|
| 121 | function RouterProvider({ router, flushSync: reactDomFlushSyncImpl, onError, useTransitions }) {
|
| 122 | useTransitions = useIsRSCRouterContext() || useTransitions;
|
| 123 | let [_state, setStateImpl] = React$1.useState(router.state);
|
| 124 | let [state, setOptimisticState] = useOptimistic(_state);
|
| 125 | let [pendingState, setPendingState] = React$1.useState();
|
| 126 | let [vtContext, setVtContext] = React$1.useState({ isTransitioning: false });
|
| 127 | let [renderDfd, setRenderDfd] = React$1.useState();
|
| 128 | let [transition, setTransition] = React$1.useState();
|
| 129 | let [interruption, setInterruption] = React$1.useState();
|
| 130 | let fetcherData = React$1.useRef( new Map());
|
| 131 | let setState = React$1.useCallback((newState, { deletedFetchers, newErrors, flushSync, viewTransitionOpts }) => {
|
| 132 | if (newErrors && onError) Object.values(newErrors).forEach((error) => onError(error, {
|
| 133 | location: newState.location,
|
| 134 | params: newState.matches[0]?.params ?? {},
|
| 135 | pattern: getRoutePattern(newState.matches)
|
| 136 | }));
|
| 137 | newState.fetchers.forEach((fetcher, key) => {
|
| 138 | if (fetcher.data !== void 0) fetcherData.current.set(key, fetcher.data);
|
| 139 | });
|
| 140 | deletedFetchers.forEach((key) => fetcherData.current.delete(key));
|
| 141 | warnOnce(flushSync === false || reactDomFlushSyncImpl != null, "You provided the `flushSync` option to a router update, but you are not using the `<RouterProvider>` from `react-router/dom` so `ReactDOM.flushSync()` is unavailable. Please update your app to `import { RouterProvider } from \"react-router/dom\"` and ensure you have `react-dom` installed as a dependency to use the `flushSync` option.");
|
| 142 | let isViewTransitionAvailable = router.window != null && router.window.document != null && typeof router.window.document.startViewTransition === "function";
|
| 143 | warnOnce(viewTransitionOpts == null || isViewTransitionAvailable, "You provided the `viewTransition` option to a router update, but you do not appear to be running in a DOM environment as `window.startViewTransition` is not available.");
|
| 144 | if (!viewTransitionOpts || !isViewTransitionAvailable) {
|
| 145 | if (reactDomFlushSyncImpl && flushSync) reactDomFlushSyncImpl(() => setStateImpl(newState));
|
| 146 | else if (useTransitions === false) setStateImpl(newState);
|
| 147 | else React$1.startTransition(() => {
|
| 148 | if (useTransitions === true) setOptimisticState((s) => getOptimisticRouterState(s, newState));
|
| 149 | setStateImpl(newState);
|
| 150 | });
|
| 151 | return;
|
| 152 | }
|
| 153 | if (reactDomFlushSyncImpl && flushSync) {
|
| 154 | reactDomFlushSyncImpl(() => {
|
| 155 | if (transition) {
|
| 156 | renderDfd?.resolve();
|
| 157 | transition.skipTransition();
|
| 158 | }
|
| 159 | setVtContext({
|
| 160 | isTransitioning: true,
|
| 161 | flushSync: true,
|
| 162 | currentLocation: viewTransitionOpts.currentLocation,
|
| 163 | nextLocation: viewTransitionOpts.nextLocation
|
| 164 | });
|
| 165 | });
|
| 166 | let t = router.window.document.startViewTransition(() => {
|
| 167 | reactDomFlushSyncImpl(() => setStateImpl(newState));
|
| 168 | });
|
| 169 | t.finished.finally(() => {
|
| 170 | reactDomFlushSyncImpl(() => {
|
| 171 | setRenderDfd(void 0);
|
| 172 | setTransition(void 0);
|
| 173 | setPendingState(void 0);
|
| 174 | setVtContext({ isTransitioning: false });
|
| 175 | });
|
| 176 | });
|
| 177 | reactDomFlushSyncImpl(() => setTransition(t));
|
| 178 | return;
|
| 179 | }
|
| 180 | if (transition) {
|
| 181 | renderDfd?.resolve();
|
| 182 | transition.skipTransition();
|
| 183 | setInterruption({
|
| 184 | state: newState,
|
| 185 | currentLocation: viewTransitionOpts.currentLocation,
|
| 186 | nextLocation: viewTransitionOpts.nextLocation
|
| 187 | });
|
| 188 | } else {
|
| 189 | setPendingState(newState);
|
| 190 | setVtContext({
|
| 191 | isTransitioning: true,
|
| 192 | flushSync: false,
|
| 193 | currentLocation: viewTransitionOpts.currentLocation,
|
| 194 | nextLocation: viewTransitionOpts.nextLocation
|
| 195 | });
|
| 196 | }
|
| 197 | }, [
|
| 198 | router.window,
|
| 199 | reactDomFlushSyncImpl,
|
| 200 | transition,
|
| 201 | renderDfd,
|
| 202 | useTransitions,
|
| 203 | setOptimisticState,
|
| 204 | onError
|
| 205 | ]);
|
| 206 | React$1.useLayoutEffect(() => router.subscribe(setState), [router, setState]);
|
| 207 | React$1.useEffect(() => {
|
| 208 | if (vtContext.isTransitioning && !vtContext.flushSync) setRenderDfd(new Deferred());
|
| 209 | }, [vtContext]);
|
| 210 | React$1.useEffect(() => {
|
| 211 | if (renderDfd && pendingState && router.window) {
|
| 212 | let newState = pendingState;
|
| 213 | let renderPromise = renderDfd.promise;
|
| 214 | let transition = router.window.document.startViewTransition(async () => {
|
| 215 | if (useTransitions === false) setStateImpl(newState);
|
| 216 | else React$1.startTransition(() => {
|
| 217 | if (useTransitions === true) setOptimisticState((s) => getOptimisticRouterState(s, newState));
|
| 218 | setStateImpl(newState);
|
| 219 | });
|
| 220 | await renderPromise;
|
| 221 | });
|
| 222 | transition.finished.finally(() => {
|
| 223 | setRenderDfd(void 0);
|
| 224 | setTransition(void 0);
|
| 225 | setPendingState(void 0);
|
| 226 | setVtContext({ isTransitioning: false });
|
| 227 | });
|
| 228 | setTransition(transition);
|
| 229 | }
|
| 230 | }, [
|
| 231 | pendingState,
|
| 232 | renderDfd,
|
| 233 | router.window,
|
| 234 | useTransitions,
|
| 235 | setOptimisticState
|
| 236 | ]);
|
| 237 | React$1.useEffect(() => {
|
| 238 | if (renderDfd && pendingState && state.location.key === pendingState.location.key) renderDfd.resolve();
|
| 239 | }, [
|
| 240 | renderDfd,
|
| 241 | transition,
|
| 242 | state.location,
|
| 243 | pendingState
|
| 244 | ]);
|
| 245 | React$1.useEffect(() => {
|
| 246 | if (!vtContext.isTransitioning && interruption) {
|
| 247 | setPendingState(interruption.state);
|
| 248 | setVtContext({
|
| 249 | isTransitioning: true,
|
| 250 | flushSync: false,
|
| 251 | currentLocation: interruption.currentLocation,
|
| 252 | nextLocation: interruption.nextLocation
|
| 253 | });
|
| 254 | setInterruption(void 0);
|
| 255 | }
|
| 256 | }, [vtContext.isTransitioning, interruption]);
|
| 257 | let navigator = React$1.useMemo(() => {
|
| 258 | return {
|
| 259 | createHref: router.createHref,
|
| 260 | createURL: router.createURL,
|
| 261 | encodeLocation: router.encodeLocation,
|
| 262 | go: (n) => router.navigate(n),
|
| 263 | push: (to, state, opts) => router.navigate(to, {
|
| 264 | state,
|
| 265 | preventScrollReset: opts?.preventScrollReset
|
| 266 | }),
|
| 267 | replace: (to, state, opts) => router.navigate(to, {
|
| 268 | replace: true,
|
| 269 | state,
|
| 270 | preventScrollReset: opts?.preventScrollReset
|
| 271 | })
|
| 272 | };
|
| 273 | }, [router]);
|
| 274 | let basename = router.basename || "/";
|
| 275 | let dataRouterContext = React$1.useMemo(() => ({
|
| 276 | router,
|
| 277 | navigator,
|
| 278 | static: false,
|
| 279 | basename,
|
| 280 | onError
|
| 281 | }), [
|
| 282 | router,
|
| 283 | navigator,
|
| 284 | basename,
|
| 285 | onError
|
| 286 | ]);
|
| 287 | return React$1.createElement(React$1.Fragment, null, React$1.createElement(DataRouterContext.Provider, { value: dataRouterContext }, React$1.createElement(DataRouterStateContext.Provider, { value: state }, React$1.createElement(FetchersContext.Provider, { value: fetcherData.current }, React$1.createElement(ViewTransitionContext.Provider, { value: vtContext }, React$1.createElement(Router, {
|
| 288 | basename,
|
| 289 | location: state.location,
|
| 290 | navigationType: state.historyAction,
|
| 291 | navigator,
|
| 292 | useTransitions
|
| 293 | }, React$1.createElement(MemoizedDataRoutes, {
|
| 294 | routes: router.routes,
|
| 295 | manifest: router.manifest,
|
| 296 | future: router.future,
|
| 297 | state,
|
| 298 | isStatic: false,
|
| 299 | onError
|
| 300 | })))))), null);
|
| 301 | }
|
| 302 | function getOptimisticRouterState(currentState, newState) {
|
| 303 | return {
|
| 304 | ...currentState,
|
| 305 | navigation: newState.navigation.state !== "idle" ? newState.navigation : currentState.navigation,
|
| 306 | revalidation: newState.revalidation !== "idle" ? newState.revalidation : currentState.revalidation,
|
| 307 | actionData: newState.navigation.state !== "submitting" ? newState.actionData : currentState.actionData,
|
| 308 | fetchers: newState.fetchers
|
| 309 | };
|
| 310 | }
|
| 311 | const MemoizedDataRoutes = React$1.memo(DataRoutes);
|
| 312 | function DataRoutes({ routes, manifest, future, state, isStatic, onError }) {
|
| 313 | return useRoutesImpl(routes, void 0, {
|
| 314 | manifest,
|
| 315 | state,
|
| 316 | isStatic,
|
| 317 | onError,
|
| 318 | future
|
| 319 | });
|
| 320 | }
|
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 |
|
| 336 | function MemoryRouter({ basename, children, initialEntries, initialIndex, useTransitions }) {
|
| 337 | let historyRef = React$1.useRef(null);
|
| 338 | if (historyRef.current == null) historyRef.current = createMemoryHistory({
|
| 339 | initialEntries,
|
| 340 | initialIndex,
|
| 341 | v5Compat: true
|
| 342 | });
|
| 343 | let history = historyRef.current;
|
| 344 | let [state, setStateImpl] = React$1.useState({
|
| 345 | action: history.action,
|
| 346 | location: history.location
|
| 347 | });
|
| 348 | let setState = React$1.useCallback((newState) => {
|
| 349 | if (useTransitions === false) setStateImpl(newState);
|
| 350 | else React$1.startTransition(() => setStateImpl(newState));
|
| 351 | }, [useTransitions]);
|
| 352 | React$1.useLayoutEffect(() => history.listen(setState), [history, setState]);
|
| 353 | return React$1.createElement(Router, {
|
| 354 | basename,
|
| 355 | children,
|
| 356 | location: state.location,
|
| 357 | navigationType: state.action,
|
| 358 | navigator: history,
|
| 359 | useTransitions
|
| 360 | });
|
| 361 | }
|
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 |
|
| 382 | function Navigate({ to, replace, state, relative }) {
|
| 383 | invariant(useInRouterContext(), `<Navigate> may be used only in the context of a <Router> component.`);
|
| 384 | let { static: isStatic, navigator } = React$1.useContext(NavigationContext);
|
| 385 | 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.");
|
| 386 | let { matches } = React$1.useContext(RouteContext);
|
| 387 | let { pathname: locationPathname } = useLocation();
|
| 388 | let navigate = useNavigate();
|
| 389 | let path = resolveTo(to, getResolveToMatches(matches), locationPathname, relative === "path");
|
| 390 | validateNavigationTarget(typeof to === "string" ? to : createPath(to), navigator.createHref(path), getNavigatorCurrentUrl(navigator), "reject");
|
| 391 | let jsonPath = JSON.stringify(path);
|
| 392 | React$1.useEffect(() => {
|
| 393 | navigate(JSON.parse(jsonPath), {
|
| 394 | replace,
|
| 395 | state,
|
| 396 | relative
|
| 397 | });
|
| 398 | }, [
|
| 399 | navigate,
|
| 400 | jsonPath,
|
| 401 | relative,
|
| 402 | replace,
|
| 403 | state
|
| 404 | ]);
|
| 405 | return null;
|
| 406 | }
|
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 |
|
| 429 | function Outlet(props) {
|
| 430 | return useOutlet(props.context);
|
| 431 | }
|
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 |
|
| 488 | function Route(props) {
|
| 489 | 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>.");
|
| 490 | }
|
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 |
|
| 512 | function Router({ basename: basenameProp = "/", children = null, location: locationProp, navigationType = "POP", navigator, static: staticProp = false, useTransitions }) {
|
| 513 | invariant(!useInRouterContext(), "You cannot render a <Router> inside another <Router>. You should never have more than one in your app.");
|
| 514 | let basename = basenameProp.replace(/^\/*/, "/");
|
| 515 | let navigationContext = React$1.useMemo(() => ({
|
| 516 | basename,
|
| 517 | navigator,
|
| 518 | static: staticProp,
|
| 519 | useTransitions,
|
| 520 | future: {}
|
| 521 | }), [
|
| 522 | basename,
|
| 523 | navigator,
|
| 524 | staticProp,
|
| 525 | useTransitions
|
| 526 | ]);
|
| 527 | if (typeof locationProp === "string") locationProp = parsePath(locationProp);
|
| 528 | let { pathname = "/", search = "", hash = "", state = null, key = "default", mask } = locationProp;
|
| 529 | let locationContext = React$1.useMemo(() => {
|
| 530 | let trailingPathname = stripBasename(pathname, basename);
|
| 531 | if (trailingPathname == null) return null;
|
| 532 | return {
|
| 533 | location: {
|
| 534 | pathname: trailingPathname,
|
| 535 | search,
|
| 536 | hash,
|
| 537 | state,
|
| 538 | key,
|
| 539 | mask
|
| 540 | },
|
| 541 | navigationType
|
| 542 | };
|
| 543 | }, [
|
| 544 | basename,
|
| 545 | pathname,
|
| 546 | search,
|
| 547 | hash,
|
| 548 | state,
|
| 549 | key,
|
| 550 | navigationType,
|
| 551 | mask
|
| 552 | ]);
|
| 553 | 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.`);
|
| 554 | if (locationContext == null) return null;
|
| 555 | return React$1.createElement(NavigationContext.Provider, { value: navigationContext }, React$1.createElement(LocationContext.Provider, {
|
| 556 | children,
|
| 557 | value: locationContext
|
| 558 | }));
|
| 559 | }
|
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 |
|
| 582 | function Routes({ children, location }) {
|
| 583 | return useRoutes(createRoutesFromChildren(children), location);
|
| 584 | }
|
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 |
|
| 632 | function Await({ children, errorElement, resolve }) {
|
| 633 | let dataRouterContext = React$1.useContext(DataRouterContext);
|
| 634 | let dataRouterStateContext = React$1.useContext(DataRouterStateContext);
|
| 635 | let onError = React$1.useCallback((error, errorInfo) => {
|
| 636 | if (dataRouterContext && dataRouterContext.onError && dataRouterStateContext) dataRouterContext.onError(error, {
|
| 637 | location: dataRouterStateContext.location,
|
| 638 | params: dataRouterStateContext.matches[0]?.params || {},
|
| 639 | pattern: getRoutePattern(dataRouterStateContext.matches),
|
| 640 | errorInfo
|
| 641 | });
|
| 642 | }, [dataRouterContext, dataRouterStateContext]);
|
| 643 | return React$1.createElement(AwaitErrorBoundary, {
|
| 644 | resolve,
|
| 645 | errorElement,
|
| 646 | onError
|
| 647 | }, React$1.createElement(ResolveAwait, null, children));
|
| 648 | }
|
| 649 | var AwaitErrorBoundary = class extends React$1.Component {
|
| 650 | constructor(props) {
|
| 651 | super(props);
|
| 652 | this.state = { error: null };
|
| 653 | }
|
| 654 | static getDerivedStateFromError(error) {
|
| 655 | return { error };
|
| 656 | }
|
| 657 | componentDidCatch(error, errorInfo) {
|
| 658 | if (this.props.onError) this.props.onError(error, errorInfo);
|
| 659 | else console.error("<Await> caught the following error during render", error, errorInfo);
|
| 660 | }
|
| 661 | render() {
|
| 662 | let { children, errorElement, resolve } = this.props;
|
| 663 | let promise = null;
|
| 664 | let status = 0;
|
| 665 | if (!(resolve instanceof Promise)) {
|
| 666 | status = 1;
|
| 667 | promise = Promise.resolve();
|
| 668 | Object.defineProperty(promise, "_tracked", { get: () => true });
|
| 669 | Object.defineProperty(promise, "_data", { get: () => resolve });
|
| 670 | } else if (this.state.error) {
|
| 671 | status = 2;
|
| 672 | let renderError = this.state.error;
|
| 673 | promise = Promise.reject().catch(() => {});
|
| 674 | Object.defineProperty(promise, "_tracked", { get: () => true });
|
| 675 | Object.defineProperty(promise, "_error", { get: () => renderError });
|
| 676 | } else if (resolve._tracked) {
|
| 677 | promise = resolve;
|
| 678 | status = "_error" in promise ? 2 : "_data" in promise ? 1 : 0;
|
| 679 | } else {
|
| 680 | status = 0;
|
| 681 | Object.defineProperty(resolve, "_tracked", { get: () => true });
|
| 682 | promise = resolve.then((data) => Object.defineProperty(resolve, "_data", { get: () => data }), (error) => {
|
| 683 | this.props.onError?.(error);
|
| 684 | Object.defineProperty(resolve, "_error", { get: () => error });
|
| 685 | });
|
| 686 | }
|
| 687 | if (status === 2 && !errorElement) throw promise._error;
|
| 688 | if (status === 2) return React$1.createElement(AwaitContext.Provider, {
|
| 689 | value: promise,
|
| 690 | children: errorElement
|
| 691 | });
|
| 692 | if (status === 1) return React$1.createElement(AwaitContext.Provider, {
|
| 693 | value: promise,
|
| 694 | children
|
| 695 | });
|
| 696 | throw promise;
|
| 697 | }
|
| 698 | };
|
| 699 | function ResolveAwait({ children }) {
|
| 700 | let data = useAsyncValue();
|
| 701 | let toRender = typeof children === "function" ? children(data) : children;
|
| 702 | return React$1.createElement(React$1.Fragment, null, toRender);
|
| 703 | }
|
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 |
|
| 715 | function createRoutesFromChildren(children, parentPath = []) {
|
| 716 | let routes = [];
|
| 717 | React$1.Children.forEach(children, (element, index) => {
|
| 718 | if (!React$1.isValidElement(element)) return;
|
| 719 | let treePath = [...parentPath, index];
|
| 720 | if (element.type === React$1.Fragment) {
|
| 721 | routes.push.apply(routes, createRoutesFromChildren(element.props.children, treePath));
|
| 722 | return;
|
| 723 | }
|
| 724 | invariant(element.type === Route, `[${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>`);
|
| 725 | let props = element.props;
|
| 726 | invariant(!props.index || !props.children, "An index route cannot have child routes.");
|
| 727 | let route = {
|
| 728 | id: props.id || treePath.join("-"),
|
| 729 | caseSensitive: props.caseSensitive,
|
| 730 | element: props.element,
|
| 731 | Component: props.Component,
|
| 732 | index: props.index,
|
| 733 | path: props.path,
|
| 734 | middleware: props.middleware,
|
| 735 | loader: props.loader,
|
| 736 | action: props.action,
|
| 737 | hydrateFallbackElement: props.hydrateFallbackElement,
|
| 738 | HydrateFallback: props.HydrateFallback,
|
| 739 | errorElement: props.errorElement,
|
| 740 | ErrorBoundary: props.ErrorBoundary,
|
| 741 | shouldRevalidate: props.shouldRevalidate,
|
| 742 | handle: props.handle,
|
| 743 | lazy: props.lazy
|
| 744 | };
|
| 745 | if (props.children) route.children = createRoutesFromChildren(props.children, treePath);
|
| 746 | routes.push(route);
|
| 747 | });
|
| 748 | return routes;
|
| 749 | }
|
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 |
|
| 778 | const createRoutesFromElements = createRoutesFromChildren;
|
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 |
|
| 787 | function renderMatches(matches) {
|
| 788 | return _renderMatches(matches);
|
| 789 | }
|
| 790 | function useRouteComponentProps() {
|
| 791 | return {
|
| 792 | params: useParams(),
|
| 793 | loaderData: useLoaderData(),
|
| 794 | actionData: useActionData(),
|
| 795 | matches: useMatches()
|
| 796 | };
|
| 797 | }
|
| 798 | function WithComponentProps({ children }) {
|
| 799 | const props = useRouteComponentProps();
|
| 800 | return React$1.cloneElement(children, props);
|
| 801 | }
|
| 802 | function withComponentProps(Component) {
|
| 803 | return function WithComponentProps() {
|
| 804 | const props = useRouteComponentProps();
|
| 805 | return React$1.createElement(Component, props);
|
| 806 | };
|
| 807 | }
|
| 808 | function useHydrateFallbackProps() {
|
| 809 | return {
|
| 810 | params: useParams(),
|
| 811 | loaderData: useLoaderData(),
|
| 812 | actionData: useActionData()
|
| 813 | };
|
| 814 | }
|
| 815 | function WithHydrateFallbackProps({ children }) {
|
| 816 | const props = useHydrateFallbackProps();
|
| 817 | return React$1.cloneElement(children, props);
|
| 818 | }
|
| 819 | function withHydrateFallbackProps(HydrateFallback) {
|
| 820 | return function WithHydrateFallbackProps() {
|
| 821 | const props = useHydrateFallbackProps();
|
| 822 | return React$1.createElement(HydrateFallback, props);
|
| 823 | };
|
| 824 | }
|
| 825 | function useErrorBoundaryProps() {
|
| 826 | return {
|
| 827 | params: useParams(),
|
| 828 | loaderData: useLoaderData(),
|
| 829 | actionData: useActionData(),
|
| 830 | error: useRouteError()
|
| 831 | };
|
| 832 | }
|
| 833 | function WithErrorBoundaryProps({ children }) {
|
| 834 | const props = useErrorBoundaryProps();
|
| 835 | return React$1.cloneElement(children, props);
|
| 836 | }
|
| 837 | function withErrorBoundaryProps(ErrorBoundary) {
|
| 838 | return function WithErrorBoundaryProps() {
|
| 839 | const props = useErrorBoundaryProps();
|
| 840 | return React$1.createElement(ErrorBoundary, props);
|
| 841 | };
|
| 842 | }
|
| 843 |
|
| 844 | export { Await, DataRoutes, MemoryRouter, Navigate, Outlet, Route, Router, RouterProvider, Routes, WithComponentProps, WithErrorBoundaryProps, WithHydrateFallbackProps, createMemoryRouter, createRoutesFromChildren, createRoutesFromElements, hydrationRouteProperties, renderMatches, withComponentProps, withErrorBoundaryProps, withHydrateFallbackProps };
|