Skip to content

Commit

Permalink
updates main nav according to authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgnlez committed Dec 17, 2024
1 parent 1568494 commit ae2e351
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions client/src/containers/nav/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"use client";

import { useMemo } from "react";

import Link from "next/link";
import { usePathname } from "next/navigation";

import { ROLES } from "@shared/entities/users/roles.enum";
import {
LayoutDashboardIcon,
ClipboardEditIcon,
ClipboardListIcon,
LayoutDashboardIcon,
ServerCogIcon,
UserIcon,
// FileQuestionIcon,
} from "lucide-react";
import { useSession } from "next-auth/react";

Expand Down Expand Up @@ -47,12 +49,14 @@ const navItems = {
url: "/my-projects",
icon: ClipboardListIcon,
match: (pathname: string) => pathname === "/my-projects",
isAuth: true,
},
{
title: "Admin",
url: "/admin",
icon: ServerCogIcon,
match: (pathname: string) => pathname.startsWith("/admin"),
isAdmin: true,
},
],
footer: [
Expand All @@ -67,8 +71,23 @@ const navItems = {

export default function MainNav() {
const { open } = useSidebar();
const { status } = useSession();
const { status, data } = useSession();
const pathname = usePathname();
const isAdmin = data?.user.role === ROLES.ADMIN;

const mainNavItems = useMemo(
() =>
navItems.main.filter((item) => {
if (item.isAdmin) {
return isAdmin;
}
if (item.isAuth) {
return status === "authenticated";
}
return true;
}),
[isAdmin, status],
);

return (
<Sidebar collapsible="icon" className="py-6">
Expand All @@ -79,7 +98,7 @@ export default function MainNav() {
<SidebarGroup>
<SidebarGroupContent>
<SidebarMenu>
{navItems.main.map((item) => {
{mainNavItems.map((item) => {
const isActive = item.match(pathname);
return (
<SidebarMenuItem key={item.title}>
Expand Down

0 comments on commit ae2e351

Please sign in to comment.