"use client"; import React, { useEffect, useRef, useState } from "react"; import Link from "next/link"; import { AdminMenuItems, ControllerMenuItems } from "@/constants"; import { usePathname, useRouter } from "next/navigation"; import { Squash as Hamburger } from "hamburger-react"; import { AnimatePresence, motion } from "framer-motion"; import { getProfile, revoke } from "@/utils/authenticateApi"; import { useMutation, useQuery } from "@tanstack/react-query"; import { DoorOpen, LogOut } from "lucide-react"; import { deleteCookie, getCookie } from "cookies-next"; export default function NavbarPanel() { const [isOpen, setOpen] = useState(false); const ref = useRef(null); const router = useRouter(); const pathname = usePathname(); const [activeLink, setActiveLink] = useState(""); const [role, setRole] = useState(""); useEffect(() => { const userRole = localStorage.getItem("Sootzani_Role"); setRole(userRole); }, []); useEffect(() => { // Set the active link based on the current route setActiveLink(pathname); }, [pathname]); const { data, isLoading, refetch } = useQuery({ queryKey: ["Profile"], queryFn: () => getProfile(), refetchOnWindowFocus: false, }); const revokeMutation = useMutation({ mutationFn: revoke, onSuccess: (res) => {}, onError: (err: any | string) => {}, }); const menus = { Admin: AdminMenuItems, Inspector: ControllerMenuItems, }; const selectedMenu = menus[role as keyof typeof menus]; const logout = () => { revokeMutation.mutate({ refreshToken: getCookie("Sootzani_Refresh_Token"), }); deleteCookie("Sootzani_Token"); localStorage.removeItem("privateKey"); localStorage.removeItem("privatePassword"); router.push("/"); }; return (

پنل کاربری {|}

{isOpen && (
    {selectedMenu.map((item, idx, array) => { // const { Icon } = item; const lastItem = array.length - 1; return ( setOpen((prev) => !prev)} className={`flex items-center justify-between w-full p-3 rounded-xl text-white ${lastItem === idx ? "bg-blue" : "bg-blue"} `} href={item.href} > {item.name} {/* */} ); })}
)}
{role && ( <> {selectedMenu.map( (item: { id: number; name: string; href: string }) => { const isActive = activeLink === item.href; return (

{item.name}

); } )} )}

{data && ( <> {role === "Admin" ? `${data?.title ? data.title : data.username} (ادمین)` : `${data?.title ? data.title : data.username} (بازرس)`} )}

); }