quivr/frontend/app/supabase-provider.tsx
Zineb El Bachiri 1d7bc8a5bc
Devx/add linter rules (#331)
* remove duplicate import

* 🚧 add new linter configuration

* 🧑‍💻  add and run prettier

* 🐛 add babel parser for linter

* 🧑‍💻 add lint-fix command

* 🚨 use lint-fix

* 🚨 remove 'FC' as a type. Use const and JSX.Element

* 🚨 enforce arrow function rule from linter

* 🔥 delete unused file

* 🚨 adding /* eslint-disable */ in failing files

* 💩 add ts-expect-error to Victory components
2023-06-15 11:52:46 +02:00

60 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;
type SupabaseContext = {
supabase: SupabaseClient;
session: MaybeSession;
};
const Context = createContext<SupabaseContext | undefined>(undefined);
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;
};
export default SupabaseProvider;