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";
|
2021-10-20 16:00:11 +00:00
|
|
|
import type { AppProps as NextAppProps } 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
|
|
|
import { createTelemetryClient, TelemetryProvider } from "@lib/telemetry";
|
|
|
|
|
2021-10-14 10:57:49 +00:00
|
|
|
import { trpc } from "./trpc";
|
|
|
|
|
|
|
|
const I18nextAdapter = appWithTranslation(({ children }: { children?: ReactNode }) => <>{children}</>);
|
|
|
|
|
2021-10-20 16:00:11 +00:00
|
|
|
// Workaround for https://github.com/vercel/next.js/issues/8592
|
|
|
|
export type AppProps = NextAppProps & {
|
|
|
|
/** 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-04-14 02:47:34 +00:00
|
|
|
const telemetryClient = useMemo(createTelemetryClient, []);
|
2021-08-27 12:11:24 +00:00
|
|
|
return (
|
2022-04-14 02:47:34 +00:00
|
|
|
<TelemetryProvider value={telemetryClient}>
|
2022-04-12 18:41:42 +00:00
|
|
|
{isPublicPage ? (
|
|
|
|
RemainingProviders
|
|
|
|
) : (
|
|
|
|
<DynamicHelpscoutProvider>
|
|
|
|
<DynamicIntercomProvider>{RemainingProviders}</DynamicIntercomProvider>
|
|
|
|
</DynamicHelpscoutProvider>
|
|
|
|
)}
|
2021-08-27 12:11:24 +00:00
|
|
|
</TelemetryProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-09-30 12:28:30 +00:00
|
|
|
export default AppProviders;
|