-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use data loaders, add global progress, fix favorite toggle loading state
- Loading branch information
1 parent
679c552
commit 5700968
Showing
20 changed files
with
443 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { loaderBuilder } from "./loader"; | ||
export { DashboardRoot } from "./page"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { type LoaderFunctionArgs } from "react-router-dom"; | ||
|
||
import type { TrpcUtils } from "~utils/trpc"; | ||
|
||
export function loaderBuilder(trpcUtils: TrpcUtils) { | ||
return async (_args: LoaderFunctionArgs) => { | ||
await Promise.all([ | ||
trpcUtils.table.list.prefetch(), | ||
trpcUtils.table.favorites.prefetch(), | ||
]); | ||
|
||
return null; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,43 @@ | ||
import { createBrowserRouter, Navigate } from "react-router-dom"; | ||
import { GlobalProgress } from "@manifold/ui/components/global-progress"; | ||
import { useDeferredValue, useEffect, useState } from "react"; | ||
import { RouterProvider } from "react-router-dom"; | ||
|
||
import { AuthGuard } from "~features/auth/components/auth-guard"; | ||
import { GuestGuard } from "~features/auth/components/guest-guard"; | ||
import { RootError } from "~features/routing/root/error"; | ||
import { RootLayout } from "~features/routing/root/layout"; | ||
import { rootLoader } from "~features/routing/root/loader"; | ||
import { routerBuilder } from "~features/routing/router"; | ||
import { trpc } from "~utils/trpc"; | ||
|
||
export const router = createBrowserRouter( | ||
[ | ||
{ | ||
path: "/", | ||
element: <RootLayout />, | ||
loader: rootLoader, | ||
errorElement: <RootError />, | ||
children: [ | ||
{ | ||
element: <GuestGuard />, | ||
children: [ | ||
{ | ||
index: true, | ||
lazy: () => import("~features/landing/pages/root/page"), | ||
}, | ||
{ | ||
path: "/login", | ||
lazy: () => import("~features/auth/pages/login/page"), | ||
}, | ||
], | ||
}, | ||
{ | ||
element: <AuthGuard />, | ||
children: [ | ||
{ | ||
path: "dashboard", | ||
lazy: () => import("~features/dashboard/pages/root/page"), | ||
}, | ||
{ | ||
path: "table", | ||
children: [ | ||
{ | ||
path: "new", | ||
lazy: () => import("~features/table/pages/new/page"), | ||
}, | ||
{ | ||
path: ":id", | ||
children: [ | ||
{ | ||
path: "edit", | ||
lazy: () => import("~features/table/pages/edit/page"), | ||
}, | ||
{ | ||
index: true, | ||
element: <Navigate to="edit" replace />, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
path: "*", | ||
lazy: () => import("~features/routing/root/not-found"), | ||
}, | ||
], | ||
}, | ||
], | ||
{ | ||
future: { | ||
v7_skipActionErrorRevalidation: true, | ||
v7_relativeSplatPath: true, | ||
v7_fetcherPersist: true, | ||
v7_normalizeFormMethod: true, | ||
}, | ||
}, | ||
); | ||
export function Router() { | ||
const trpcUtils = trpc.useUtils(); | ||
const [routerInstance] = useState(() => routerBuilder(trpcUtils)); | ||
|
||
return ( | ||
<> | ||
<NavigationProgress router={routerInstance} /> | ||
<RouterProvider router={routerInstance} /> | ||
</> | ||
); | ||
} | ||
|
||
function NavigationProgress({ | ||
router, | ||
}: { | ||
router: ReturnType<typeof routerBuilder>; | ||
}) { | ||
const [isRouteChangePending, setIsRouteChangePending] = useState(false); | ||
|
||
/** | ||
* Deferring this allows us to avoid showing the progress bar for very short | ||
* route state changes. | ||
*/ | ||
const isPending = useDeferredValue(isRouteChangePending); | ||
|
||
useEffect(() => { | ||
return router.subscribe((state) => { | ||
setIsRouteChangePending( | ||
state.navigation.state === "loading" || | ||
state.navigation.state === "submitting", | ||
); | ||
}); | ||
}, [router]); | ||
|
||
return <GlobalProgress isAnimating={isPending} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.