2022-01-07 20:23:37 +00:00
|
|
|
import { SessionProvider } from "next-auth/react";
|
2021-10-14 10:57:49 +00:00
|
|
|
import { appWithTranslation } from "next-i18next";
|
2022-05-17 19:31:49 +00:00
|
|
|
import type { AppProps as NextAppProps, AppProps as NextJsAppProps } from "next/app";
|
2022-04-14 02:47:34 +00:00
|
|
|
import { ComponentProps, ReactNode, useMemo } from "react";
|
2021-08-27 12:11:24 +00:00
|
|
|
|
2022-03-18 15:09:13 +00:00
|
|
|
import DynamicHelpscoutProvider from "@ee/lib/helpscout/providerDynamic";
|
2021-09-24 20:02:03 +00:00
|
|
|
import DynamicIntercomProvider from "@ee/lib/intercom/providerDynamic";
|
|
|
|
|
2022-02-16 15:26:48 +00:00
|
|
|
import usePublicPage from "@lib/hooks/usePublicPage";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-10-14 10:57:49 +00:00
|
|
|
import { trpc } from "./trpc";
|
|
|
|
|
2022-05-17 19:31:49 +00:00
|
|
|
const I18nextAdapter = appWithTranslation<NextJsAppProps & { children: React.ReactNode }>(({ children }) => (
|
|
|
|
<>{children}</>
|
|
|
|
));
|
2021-10-14 10:57:49 +00:00
|
|
|
|
2021-10-20 16:00:11 +00:00
|
|
|
// Workaround for https://github.com/vercel/next.js/issues/8592
|
2022-05-26 17:07:14 +00:00
|
|
|
export type AppProps = Omit<NextAppProps, "Component"> & {
|
|
|
|
Component: NextAppProps["Component"] & { requiresLicense?: boolean };
|
2021-10-20 16:00:11 +00:00
|
|
|
/** Will be defined only is there was an error */
|
|
|
|
err?: Error;
|
|
|
|
};
|
|
|
|
|
|
|
|
type AppPropsWithChildren = AppProps & {
|
|
|
|
children: ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
const CustomI18nextProvider = (props: AppPropsWithChildren) => {
|
2021-11-03 14:02:17 +00:00
|
|
|
const { i18n, locale } = trpc.useQuery(["viewer.i18n"]).data ?? {
|
|
|
|
locale: "en",
|
|
|
|
};
|
2021-10-20 16:00:11 +00:00
|
|
|
|
2021-10-14 10:57:49 +00:00
|
|
|
const passedProps = {
|
|
|
|
...props,
|
2021-10-20 16:00:11 +00:00
|
|
|
pageProps: {
|
|
|
|
...props.pageProps,
|
|
|
|
...i18n,
|
|
|
|
},
|
|
|
|
router: locale ? { locale } : props.router,
|
2021-10-14 10:57:49 +00:00
|
|
|
} as unknown as ComponentProps<typeof I18nextAdapter>;
|
|
|
|
return <I18nextAdapter {...passedProps} />;
|
|
|
|
};
|
|
|
|
|
2021-10-20 16:00:11 +00:00
|
|
|
const AppProviders = (props: AppPropsWithChildren) => {
|
2021-10-14 10:57:49 +00:00
|
|
|
const session = trpc.useQuery(["viewer.session"]).data;
|
2022-02-16 15:26:48 +00:00
|
|
|
// No need to have intercom on public pages - Good for Page Performance
|
|
|
|
const isPublicPage = usePublicPage();
|
|
|
|
const RemainingProviders = (
|
|
|
|
<SessionProvider session={session || undefined}>
|
|
|
|
<CustomI18nextProvider {...props}>{props.children}</CustomI18nextProvider>
|
|
|
|
</SessionProvider>
|
|
|
|
);
|
2022-06-02 16:19:01 +00:00
|
|
|
|
|
|
|
if (isPublicPage) {
|
|
|
|
return RemainingProviders;
|
|
|
|
}
|
|
|
|
|
2021-08-27 12:11:24 +00:00
|
|
|
return (
|
2022-06-02 16:19:01 +00:00
|
|
|
<DynamicHelpscoutProvider>
|
|
|
|
<DynamicIntercomProvider>{RemainingProviders}</DynamicIntercomProvider>
|
|
|
|
</DynamicHelpscoutProvider>
|
2021-08-27 12:11:24 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-09-30 12:28:30 +00:00
|
|
|
export default AppProviders;
|