quivr/frontend/lib/context/SupabaseProvider/supabase-provider.tsx
Zineb El Bachiri 9c8e0aa0e4
Feat/multiple brains frontend (#344)
* 🗑️ remove date input from fetch_user_id_from_credentials

* ♻️ refactor backend utils by splitting it into files

* 💡 comments for next actions to update /upload

* 🚚 move SupabaseProvider tp libs

* 🚚 move useAxios to hooks

* ♻️ refacto brain routes

* 🚨 update lintermfor paths

*  new brain context provider

*  new brain component in navbar

* 🚨 fix linter and async

* 🇸🇪 add feature flag for multiple-brains
2023-06-20 16:17:13 +02:00

58 lines
1.3 KiB
TypeScript

"use client";
import type { SupabaseClient } from "@supabase/auth-helpers-nextjs";
import {
createBrowserSupabaseClient,
Session,
} from "@supabase/auth-helpers-nextjs";
import { useRouter } from "next/navigation";
import { createContext, useContext, useEffect, useState } from "react";
type MaybeSession = Session | null;
export type SupabaseContext = {
supabase: SupabaseClient;
session: MaybeSession;
};
const Context = createContext<SupabaseContext | undefined>(undefined);
export const SupabaseProvider = ({
children,
session,
}: {
children: React.ReactNode;
session: MaybeSession;
}): JSX.Element => {
const [supabase] = useState(() => createBrowserSupabaseClient());
const router = useRouter();
useEffect(() => {
const {
data: { subscription },
} = supabase.auth.onAuthStateChange(() => {
router.refresh();
});
return () => {
subscription.unsubscribe();
};
}, [router, supabase]);
return (
<Context.Provider value={{ supabase, session }}>
<>{children}</>
</Context.Provider>
);
};
export const useSupabase = (): SupabaseContext => {
const context = useContext(Context);
if (context === undefined) {
throw new Error("useSupabase must be used inside SupabaseProvider");
}
return context;
};