link-stack/apps/link/app/_components/MultiProvider.tsx

69 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-06-26 10:07:12 +00:00
"use client";
import { FC, PropsWithChildren, useState } from "react";
import { CssBaseline } from "@mui/material";
import { CookiesProvider } from "react-cookie";
import { SessionProvider } from "next-auth/react";
import { NextAppDirEmotionCacheProvider } from "tss-react/next/appDir";
import { SWRConfig } from "swr";
import { GraphQLClient } from "graphql-request";
2023-08-25 07:11:33 +00:00
import { I18n } from "react-polyglot";
2023-06-26 10:07:12 +00:00
import { AdapterDateFns } from "@mui/x-date-pickers-pro/AdapterDateFns";
import { LocalizationProvider } from "@mui/x-date-pickers-pro";
2023-07-17 12:23:12 +00:00
import { LicenseInfo } from "@mui/x-date-pickers-pro";
2023-08-25 07:11:33 +00:00
import { locales } from "leafcutter-common";
2023-07-17 12:23:12 +00:00
LicenseInfo.setLicenseKey(
2023-08-25 07:11:33 +00:00
"7c9bf25d9e240f76e77cbf7d2ba58a23Tz02NjU4OCxFPTE3MTU4NjIzMzQ2ODgsUz1wcm8sTE09c3Vic2NyaXB0aW9uLEtWPTI=",
2023-07-17 12:23:12 +00:00
);
2023-06-26 10:07:12 +00:00
export const MultiProvider: FC<PropsWithChildren> = ({ children }) => {
const [csrfToken, setCsrfToken] = useState("");
const origin =
typeof window !== "undefined" && window.location.origin
? window.location.origin
: null;
2023-08-25 07:11:33 +00:00
const client = new GraphQLClient(`${origin}/zammad/graphql`, {
2023-06-26 10:07:12 +00:00
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
2023-08-25 07:11:33 +00:00
const messages: any = { en: locales.en, fr: locales.fr };
const locale = "en";
2023-06-26 10:07:12 +00:00
const graphQLFetcher = async ({ document, variables }: any) => {
const requestHeaders = {
"X-CSRF-Token": csrfToken,
};
const { data, headers } = await client.rawRequest(
document,
variables,
2023-08-25 07:11:33 +00:00
requestHeaders,
2023-06-26 10:07:12 +00:00
);
const token = headers.get("CSRF-Token");
setCsrfToken(token);
return data;
};
return (
<>
<CssBaseline />
<NextAppDirEmotionCacheProvider options={{ key: "css" }}>
<SWRConfig value={{ fetcher: graphQLFetcher }}>
<SessionProvider>
<CookiesProvider>
<LocalizationProvider dateAdapter={AdapterDateFns}>
2023-08-25 07:11:33 +00:00
<I18n locale={locale} messages={messages[locale]}>
{children}
</I18n>
2023-06-26 10:07:12 +00:00
</LocalizationProvider>
</CookiesProvider>
</SessionProvider>
</SWRConfig>
</NextAppDirEmotionCacheProvider>
</>
);
};