2023-07-07 13:56:08 +03:00
|
|
|
"use client";
|
|
|
|
|
2023-09-01 16:14:35 +03:00
|
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
2023-12-30 01:45:36 +03:00
|
|
|
import { posthog } from "posthog-js";
|
|
|
|
import { PostHogProvider } from "posthog-js/react";
|
2023-07-07 13:56:08 +03:00
|
|
|
import { PropsWithChildren, useEffect } from "react";
|
|
|
|
|
2023-12-14 12:15:38 +03:00
|
|
|
import { Menu } from "@/lib/components/Menu/Menu";
|
2023-12-14 18:22:09 +03:00
|
|
|
import { useOutsideClickListener } from "@/lib/components/Menu/hooks/useOutsideClickListener";
|
2023-12-14 12:15:38 +03:00
|
|
|
import { NotificationBanner } from "@/lib/components/NotificationBanner";
|
2023-11-20 20:04:26 +03:00
|
|
|
import { BrainProvider } from "@/lib/context";
|
2023-07-07 13:56:08 +03:00
|
|
|
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
|
2023-12-13 22:56:07 +03:00
|
|
|
import { SideBarProvider } from "@/lib/context/SidebarProvider/sidebar-provider";
|
2023-07-17 16:45:18 +03:00
|
|
|
import { useSupabase } from "@/lib/context/SupabaseProvider";
|
2023-08-07 15:13:41 +03:00
|
|
|
import { UpdateMetadata } from "@/lib/helpers/updateMetadata";
|
2023-09-18 16:12:50 +03:00
|
|
|
import { usePageTracking } from "@/services/analytics/june/usePageTracking";
|
2023-12-14 12:15:38 +03:00
|
|
|
|
2023-08-29 11:50:36 +03:00
|
|
|
import "../lib/config/LocaleConfig/i18n";
|
2023-07-07 13:56:08 +03:00
|
|
|
|
2023-12-30 01:45:36 +03:00
|
|
|
if (
|
|
|
|
process.env.NEXT_PUBLIC_POSTHOG_KEY != null &&
|
|
|
|
process.env.NEXT_PUBLIC_POSTHOG_HOST != null
|
|
|
|
) {
|
|
|
|
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
|
2024-01-02 12:03:49 +03:00
|
|
|
ui_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
|
|
api_host: `${process.env.NEXT_PUBLIC_FRONTEND_URL}/ingest`,
|
2023-12-30 01:45:36 +03:00
|
|
|
opt_in_site_apps: true,
|
2023-12-30 02:53:47 +03:00
|
|
|
disable_session_recording: true,
|
2023-12-30 01:45:36 +03:00
|
|
|
});
|
2023-12-28 13:03:05 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 13:56:08 +03:00
|
|
|
// This wrapper is used to make effect calls at a high level in app rendering.
|
2023-11-20 20:04:26 +03:00
|
|
|
const App = ({ children }: PropsWithChildren): JSX.Element => {
|
2023-09-18 22:28:07 +03:00
|
|
|
const { fetchAllBrains, fetchDefaultBrain, fetchPublicPrompts } =
|
2023-08-29 11:50:36 +03:00
|
|
|
useBrainContext();
|
2023-12-14 18:22:09 +03:00
|
|
|
const { onClickOutside } = useOutsideClickListener();
|
2023-07-17 16:45:18 +03:00
|
|
|
const { session } = useSupabase();
|
2023-07-07 13:56:08 +03:00
|
|
|
|
2023-09-01 16:14:35 +03:00
|
|
|
usePageTracking();
|
|
|
|
|
2023-07-07 13:56:08 +03:00
|
|
|
useEffect(() => {
|
2023-10-02 10:27:17 +03:00
|
|
|
if (session?.user) {
|
|
|
|
void fetchAllBrains();
|
|
|
|
void fetchDefaultBrain();
|
|
|
|
void fetchPublicPrompts();
|
2023-12-30 01:45:36 +03:00
|
|
|
posthog.identify(session.user.id, { email: session.user.email });
|
2023-12-30 02:53:47 +03:00
|
|
|
posthog.startSessionRecording();
|
2023-10-02 10:27:17 +03:00
|
|
|
}
|
|
|
|
}, [session]);
|
2023-07-07 13:56:08 +03:00
|
|
|
|
|
|
|
return (
|
2023-12-28 13:03:05 +03:00
|
|
|
<PostHogProvider client={posthog}>
|
|
|
|
<div className="flex flex-1 flex-col overflow-auto">
|
|
|
|
<NotificationBanner />
|
|
|
|
<div className="relative h-full w-full flex justify-stretch items-stretch overflow-auto">
|
|
|
|
<Menu />
|
|
|
|
<div onClick={onClickOutside} className="flex-1">
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
<UpdateMetadata />
|
2023-12-14 18:22:09 +03:00
|
|
|
</div>
|
2023-12-14 12:15:38 +03:00
|
|
|
</div>
|
2023-12-28 13:03:05 +03:00
|
|
|
</PostHogProvider>
|
2023-11-20 20:04:26 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const queryClient = new QueryClient();
|
|
|
|
|
|
|
|
const AppWithQueryClient = ({ children }: PropsWithChildren): JSX.Element => {
|
|
|
|
return (
|
|
|
|
<QueryClientProvider client={queryClient}>
|
|
|
|
<BrainProvider>
|
2023-12-13 22:56:07 +03:00
|
|
|
<SideBarProvider>
|
|
|
|
<App>{children}</App>
|
|
|
|
</SideBarProvider>
|
2023-11-20 20:04:26 +03:00
|
|
|
</BrainProvider>
|
2023-09-01 16:14:35 +03:00
|
|
|
</QueryClientProvider>
|
2023-07-07 13:56:08 +03:00
|
|
|
);
|
|
|
|
};
|
2023-11-20 20:04:26 +03:00
|
|
|
|
|
|
|
export { AppWithQueryClient as App };
|