diff --git a/src/components/BaseHeader.tsx b/src/components/BaseHeader.tsx index 4011c671..93d0a874 100644 --- a/src/components/BaseHeader.tsx +++ b/src/components/BaseHeader.tsx @@ -4,12 +4,14 @@ import { getServerAuthSession } from '@src/server/auth'; import { ClubSearchBar, EventSearchBar } from './SearchBar'; import SignInButton from './signInButton'; import MobileNav from './MobileNav'; +import { api } from '@src/trpc/server'; export const BaseHeader = async ({ children }: { children: ReactNode }) => { const session = await getServerAuthSession(); + const userCapabilities = await api.userMetadata.getUserSidebarCapabilities(); return (
- + {children}
{session !== null ? ( diff --git a/src/components/MobileNav.tsx b/src/components/MobileNav.tsx index 49aa557a..da2d12c0 100644 --- a/src/components/MobileNav.tsx +++ b/src/components/MobileNav.tsx @@ -1,9 +1,15 @@ 'use client'; +import { type Dispatch, type SetStateAction } from 'react'; import { useState } from 'react'; -import MobileNavMenu from './MobileNavMenu'; -const MobileNav = () => { +import NavMenu from './NavMenu'; +import { type personalCats } from '@src/constants/categories'; + +type NavMenuProps = { + userCapabilites: Array<(typeof personalCats)[number]>; +}; +const MobileNav = ({ userCapabilites }: NavMenuProps) => { const [isOpen, setIsOpen] = useState(false); if (!isOpen) @@ -26,7 +32,30 @@ const MobileNav = () => {
); - return ; + return ( + + ); }; +const MobileNavMenu = ({ + setIsOpen, + userCapabilites, +}: { + setIsOpen: Dispatch>; + userCapabilites: Array<(typeof personalCats)[number]>; +}) => { + return ( + <> +
{ + setIsOpen(false); + }} + className="fixed left-0 top-0 z-50 h-screen w-full bg-black bg-opacity-50" + >
+ + + ); +}; export default MobileNav; diff --git a/src/components/MobileNavMenu.tsx b/src/components/MobileNavMenu.tsx deleted file mode 100644 index 12def32e..00000000 --- a/src/components/MobileNavMenu.tsx +++ /dev/null @@ -1,27 +0,0 @@ -'use client'; - -import { type Dispatch, type SetStateAction } from 'react'; - -import NavMenu from './NavMenu'; - -const MobileNavMenu = ({ - setIsOpen, -}: { - setIsOpen: Dispatch>; -}) => { - return ( - <> -
{ - setIsOpen(false); - }} - className="fixed left-0 top-0 z-50 h-screen w-full bg-black bg-opacity-50" - >
- - - ); -}; - -export default MobileNavMenu; diff --git a/src/components/NavMenu.tsx b/src/components/NavMenu.tsx index aa8e52b5..16faf041 100644 --- a/src/components/NavMenu.tsx +++ b/src/components/NavMenu.tsx @@ -1,11 +1,15 @@ 'use client'; import SidebarItems from './SidebarItems'; import Image from 'next/image'; -import { mainCats, moreCats } from '@src/constants/categories'; -import { api } from '@src/trpc/react'; -const NavMenu = () => { - const personalCapabilites = - api.userMetadata.getUserSidebarCapabilities.useQuery(); +import { + mainCats, + moreCats, + type personalCats, +} from '@src/constants/categories'; +type NavMenuProps = { + userCapabilites: Array<(typeof personalCats)[number]>; +}; +const NavMenu = ({ userCapabilites }: NavMenuProps) => { return ( <>
@@ -33,10 +37,9 @@ const NavMenu = () => { More
- {personalCapabilites.isSuccess && - personalCapabilites.data.map((cat) => ( - - ))} + {userCapabilites.map((cat) => ( + + ))} {moreCats.map((cat) => ( ))} diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index 18faf0e3..dc5fb81e 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -1,10 +1,13 @@ +import { api } from '@src/trpc/server'; import NavMenu from './NavMenu'; // Keep in mind that in all routes we need pl-72 for the sidebar -const Sidebar = () => { +const Sidebar = async () => { + const userSidebarCapabilities = + await api.userMetadata.getUserSidebarCapabilities(); return (
- +
); }; diff --git a/src/utils/sidebarCapabilities.ts b/src/utils/sidebarCapabilities.ts new file mode 100644 index 00000000..91f82df4 --- /dev/null +++ b/src/utils/sidebarCapabilities.ts @@ -0,0 +1,35 @@ +import { type personalCats } from '@src/constants/categories'; +import { getServerAuthSession } from '@src/server/auth'; +import { db } from '@src/server/db'; +import { admin } from '@src/server/db/schema/admin'; +import { userMetadataToClubs } from '@src/server/db/schema/users'; +import { and, eq, or } from 'drizzle-orm'; +import { cache } from 'react'; + +export const getUserSidebarCapabilities = cache(async () => { + const session = await getServerAuthSession(); + const capabilites: (typeof personalCats)[number][] = []; + if (!session) return capabilites; + if ( + await db.query.userMetadataToClubs.findFirst({ + where: and( + eq(userMetadataToClubs.userId, session.user.id), + or( + eq(userMetadataToClubs.memberType, 'Officer'), + eq(userMetadataToClubs.memberType, 'President'), + ), + ), + }) + ) { + capabilites.push('Manage Clubs'); + } + if ( + ( + await db.query.admin.findMany({ + where: eq(admin.userId, session.user.id), + }) + ).length === 1 + ) + capabilites.push('Admin'); + return capabilites; +});