-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppRouter.tsx
63 lines (52 loc) · 2.02 KB
/
AppRouter.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { useLogger } from "@squide/core";
import { useRoutes, type Route } from "@squide/react-router";
import { useEffect, useMemo, type ReactElement } from "react";
import type { RouterProviderProps } from "react-router-dom";
import { AppRouterDispatcherContext, AppRouterStateContext } from "./AppRouterContext.ts";
import { useAppRouterReducer } from "./AppRouterReducer.ts";
import { RootRoute } from "./RootRoute.tsx";
import { useStrictRegistrationMode } from "./useStrictRegistrationMode.ts";
export interface AppRouterRenderFunctionArgs {
routes: Route[];
}
export interface RenderRouterProviderFunctionArgs {
rootRoute: ReactElement;
registeredRoutes: Route[];
routerProviderProps: Omit<RouterProviderProps, "router">;
}
export type RenderRouterProviderFunction = (args: RenderRouterProviderFunctionArgs) => ReactElement;
export interface AppRouterProps {
waitForMsw: boolean;
waitForPublicData?: boolean;
waitForProtectedData?: boolean;
children: RenderRouterProviderFunction;
}
export function AppRouter(props: AppRouterProps) {
const {
waitForMsw,
waitForPublicData = false,
waitForProtectedData = false,
children: renderRouterProvider
} = props;
const [state, dispatch] = useAppRouterReducer(waitForMsw, waitForPublicData, waitForProtectedData);
const logger = useLogger();
const routes = useRoutes();
useStrictRegistrationMode();
useEffect(() => {
logger.debug("[squide] AppRouter state updated:", state);
}, [state, logger]);
const routerProvider = useMemo(() => {
return renderRouterProvider({
rootRoute: <RootRoute />,
registeredRoutes: routes,
routerProviderProps: {}
});
}, [routes, renderRouterProvider]);
return (
<AppRouterDispatcherContext.Provider value={dispatch}>
<AppRouterStateContext.Provider value={state}>
{routerProvider}
</AppRouterStateContext.Provider>
</AppRouterDispatcherContext.Provider>
);
}