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

126 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-06-26 10:07:12 +00:00
"use client";
import { FC, PropsWithChildren, useState } from "react";
2023-10-16 09:20:40 +02:00
import { usePathname } from "next/navigation";
2023-06-26 10:07:12 +00:00
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;
const client = new GraphQLClient(`${origin}/zammad/graphql`);
2023-08-25 07:11:33 +00:00
const messages: any = { en: locales.en, fr: locales.fr };
const locale = "en";
const fetchAndCheckAuth = async ({
document,
variables,
url,
method,
body,
}: any) => {
2023-06-26 10:07:12 +00:00
const requestHeaders = {
"Content-Type": "application/json",
Accept: "application/json",
2023-06-26 10:07:12 +00:00
"X-CSRF-Token": csrfToken,
};
let responseData = null;
let responseHeaders = new Headers();
let responseStatus = null;
2023-06-26 10:07:12 +00:00
if (document) {
const { data, headers, status } = await client.rawRequest(
document,
variables,
requestHeaders,
);
responseData = data;
responseHeaders = headers;
responseStatus = status;
} else {
const res = await fetch(url, {
method,
headers: requestHeaders,
body,
});
responseData = await res.json();
responseHeaders = res.headers;
responseStatus = res.status;
}
if (responseStatus !== 200) {
2023-10-16 09:20:40 +02:00
const res = await fetch("/zammad/auth/sso", {
method: "GET",
redirect: "manual",
});
console.log({ checkAuth: res });
return null;
}
const token = responseHeaders.get("CSRF-Token");
2023-06-26 10:07:12 +00:00
setCsrfToken(token);
return responseData;
2023-06-26 10:07:12 +00:00
};
const multiFetcher = async ({
document,
variables,
url,
method,
body,
}: any) => {
2023-10-16 09:20:40 +02:00
let checks = 0;
let data = null;
while (!data && checks < 2) {
data = await fetchAndCheckAuth({
document,
variables,
url,
method,
body,
});
2023-10-16 09:20:40 +02:00
checks++;
}
return data;
};
2023-06-26 10:07:12 +00:00
return (
<>
<CssBaseline />
<NextAppDirEmotionCacheProvider options={{ key: "css" }}>
<SWRConfig value={{ fetcher: multiFetcher }}>
2023-06-26 10:07:12 +00:00
<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>
</>
);
};