From dcc15a0e70450b871a92651da82a19af83ba5957 Mon Sep 17 00:00:00 2001 From: sirin Date: Wed, 30 Oct 2024 01:21:07 +0700 Subject: [PATCH] feat: render admin button for admin role --- src/components/navigationBar/nav.tsx | 6 ++- src/components/navigationBar/profileBar.tsx | 8 ++++ src/hooks/useUserRole.ts | 41 +++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/hooks/useUserRole.ts diff --git a/src/components/navigationBar/nav.tsx b/src/components/navigationBar/nav.tsx index ed4ac8b..58c4fbd 100644 --- a/src/components/navigationBar/nav.tsx +++ b/src/components/navigationBar/nav.tsx @@ -27,7 +27,8 @@ const ListItem = React.forwardRef, React.ComponentPropsWit "block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground", className )} - {...props}> + {...props} + >
{title}

{children}

@@ -71,7 +72,8 @@ export function NavigationBar() { + aria-label="Brand" + > logo B2DVentures diff --git a/src/components/navigationBar/profileBar.tsx b/src/components/navigationBar/profileBar.tsx index 7b3a860..d464a0d 100644 --- a/src/components/navigationBar/profileBar.tsx +++ b/src/components/navigationBar/profileBar.tsx @@ -15,6 +15,7 @@ import { Skeleton } from "@/components/ui/skeleton"; import { Bell, Heart, Wallet } from "lucide-react"; import { LogoutButton } from "@/components/auth/logoutButton"; import useSession from "@/lib/supabase/useSession"; +import { useUserRole } from "@/hooks/useUserRole"; const UnAuthenticatedComponents = () => { return ( @@ -34,6 +35,8 @@ const UnAuthenticatedComponents = () => { const AuthenticatedComponents = ({ uid }: { uid: string }) => { let notifications = 100; const displayValue = notifications >= 100 ? "..." : notifications; + const { data } = useUserRole(); + return (
@@ -62,6 +65,11 @@ const AuthenticatedComponents = ({ uid }: { uid: string }) => { Settings Support + {data != null && data != undefined && data.role === "admin" && ( + + Admin + + )} diff --git a/src/hooks/useUserRole.ts b/src/hooks/useUserRole.ts new file mode 100644 index 0000000..07a9ffe --- /dev/null +++ b/src/hooks/useUserRole.ts @@ -0,0 +1,41 @@ +"use client"; + +import { createSupabaseClient } from "@/lib/supabase/clientComponentClient"; +import { getUserRole } from "@/lib/data/userQuery"; +import { useQuery } from "@supabase-cache-helpers/postgrest-react-query"; +import { useState, useEffect } from "react"; +import { Session } from "@supabase/supabase-js"; + +export function useUserRole() { + const client = createSupabaseClient(); + const [session, setSession] = useState(null); + const [sessionError, setSessionError] = useState(null); + + useEffect(() => { + async function fetchSession() { + try { + const { + data: { session }, + error, + } = await client.auth.getSession(); + if (error) throw error; + setSession(session); + } catch (error) { + setSessionError(error as Error); + console.error("Error fetching session:", error); + } + } + + fetchSession(); + }, [client.auth]); + + const { data, isLoading, error: userRoleError } = useQuery(getUserRole(client, session?.user?.id!)); + + return { + data, + isLoading: isLoading || !session, + error: userRoleError || sessionError, + session, + userId: session?.user?.id, + }; +}