Skip to content

Commit

Permalink
feat: group context providers into one
Browse files Browse the repository at this point in the history
  • Loading branch information
Loxeris committed Jul 15, 2024
1 parent b6641dc commit 5f678b9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 39 deletions.
46 changes: 46 additions & 0 deletions packages/diracx-web-components/src/contexts/DiracXWebProviders.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { CssBaseline, ThemeProvider as MUIThemeProvider } from "@mui/material";
import { useMUITheme } from "../hooks";
import {
OIDCConfigurationProvider,
ThemeProvider,
NavigationProvider,
ApplicationsProvider,
} from "./index";

export function DiracXWebProviders({
children,
getPath,
setPath,
getSearchParams,
}: {
children: React.ReactNode;
getPath: () => string;
setPath: (path: string) => void;
getSearchParams: () => URLSearchParams;
}) {
return (
<OIDCConfigurationProvider>
<ThemeProvider>
<NavigationProvider
getPath={getPath}
setPath={setPath}
getSearchParams={getSearchParams}
>
<ApplicationsProvider>
<MUIProviders>{children}</MUIProviders>
</ApplicationsProvider>
</NavigationProvider>
</ThemeProvider>
</OIDCConfigurationProvider>
);
}

function MUIProviders({ children }: { children: React.ReactNode }) {
const theme = useMUITheme();
return (
<MUIThemeProvider theme={theme}>
<CssBaseline />
{children}
</MUIThemeProvider>
);
}
1 change: 1 addition & 0 deletions packages/diracx-web-components/src/contexts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export {
type ThemeProviderProps,
} from "./ThemeProvider";
export * from "./NavigationProvider";
export * from "./DiracXWebProviders";
2 changes: 1 addition & 1 deletion packages/diracx-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build && mv out ../..",
"build": "next build && rm -rf ../../out && mv out ../..",
"start": "next start",
"lint": "next lint",
"ts-lint": "tsc -noEmit -incremental",
Expand Down
42 changes: 15 additions & 27 deletions packages/diracx-web/src/app/(dashboard)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,43 @@
"use client";
import React from "react";
import CssBaseline from "@mui/material/CssBaseline";
import { Box } from "@mui/material";
import { ThemeProvider as MUIThemeProvider } from "@mui/material/styles";
import { DiracXWebProviders } from "@diracgrid/diracx-web-components/contexts";
import {
OIDCSecure,
Dashboard,
} from "@diracgrid/diracx-web-components/components";
import {
ApplicationsProvider,
NavigationProvider,
} from "@diracgrid/diracx-web-components/contexts";
import { useMUITheme } from "@diracgrid/diracx-web-components/hooks";
import { usePathname, useRouter, useSearchParams } from "next/navigation";

export default function DashboardLayout({
children,
}: {
children: React.ReactNode;
}) {
const theme = useMUITheme();
const pathname = usePathname();
const router = useRouter();
const searchParams = useSearchParams();
return (
<section>
<NavigationProvider
<DiracXWebProviders
getPath={() => pathname}
setPath={(path: string) => {
router.push(path);
}}
getSearchParams={() => searchParams}
>
<ApplicationsProvider>
<OIDCSecure>
<Dashboard>
<MUIThemeProvider theme={theme}>
<CssBaseline />
<Box
sx={{
ml: "5%",
mr: "5%",
}}
>
{children}
</Box>
</MUIThemeProvider>
</Dashboard>
</OIDCSecure>
</ApplicationsProvider>
</NavigationProvider>
<OIDCSecure>
<Dashboard>
<Box
sx={{
ml: "5%",
mr: "5%",
}}
>
{children}
</Box>
</Dashboard>
</OIDCSecure>
</DiracXWebProviders>
</section>
);
}
10 changes: 6 additions & 4 deletions packages/diracx-web/src/app/auth/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";
import React from "react";
import { DiracXWebProviders } from "@diracgrid/diracx-web-components/contexts";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { NavigationProvider } from "@diracgrid/diracx-web-components/contexts";

export default function AuthLayout({
children,
Expand All @@ -12,12 +12,14 @@ export default function AuthLayout({
const router = useRouter();
const searchParams = useSearchParams();
return (
<NavigationProvider
<DiracXWebProviders
getPath={() => pathname}
setPath={router.push}
setPath={(path: string) => {
router.push(path);
}}
getSearchParams={() => searchParams}
>
{children}
</NavigationProvider>
</DiracXWebProviders>
);
}
9 changes: 2 additions & 7 deletions packages/diracx-web/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Inter } from "next/font/google";
import {
OIDCConfigurationProvider,
ThemeProvider,
} from "@diracgrid/diracx-web-components/contexts";
import { Suspense } from "react";

const inter = Inter({ subsets: ["latin"] });

Expand All @@ -23,9 +20,7 @@ export default function RootLayout({
return (
<html lang="en">
<body className={inter.className}>
<OIDCConfigurationProvider>
<ThemeProvider>{children}</ThemeProvider>
</OIDCConfigurationProvider>
<Suspense>{children}</Suspense>
</body>
</html>
);
Expand Down

0 comments on commit 5f678b9

Please sign in to comment.