quivr/frontend/lib/context/SupabaseProvider/supabase-provider.tsx
Mamadou DICKO 9bb7ccf651
Frontend/test/chat 1 (#508)
* feat: add providers mocks

* test(<ChatPage/>: add render test using providers
2023-07-05 09:30:22 +02:00

44 lines
958 B
TypeScript

"use client";
import {
createBrowserSupabaseClient,
Session,
} from "@supabase/auth-helpers-nextjs";
import { useRouter } from "next/navigation";
import { createContext, useEffect, useState } from "react";
import { SupabaseContextType } from "./types";
export const SupabaseContext = createContext<SupabaseContextType | undefined>(
undefined
);
export const SupabaseProvider = ({
children,
session,
}: {
children: React.ReactNode;
session: Session | null;
}): JSX.Element => {
const [supabase] = useState(() => createBrowserSupabaseClient());
const router = useRouter();
useEffect(() => {
const {
data: { subscription },
} = supabase.auth.onAuthStateChange(() => {
router.refresh();
});
return () => {
subscription.unsubscribe();
};
}, [router, supabase]);
return (
<SupabaseContext.Provider value={{ supabase, session }}>
<>{children}</>
</SupabaseContext.Provider>
);
};