Sootzani-Inspector/components/NavbarPanel.tsx

155 lines
5.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"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 } from "@/utils/authenticateApi";
import { useQuery } from "@tanstack/react-query";
import { DoorOpen, LogOut } from "lucide-react";
import { deleteCookie } 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<string | null>("");
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 menus = {
Admin: AdminMenuItems,
Inspector: ControllerMenuItems,
};
const selectedMenu = menus[role as keyof typeof menus];
return (
<div
className="fixed flex justify-between items-center gap-3
flex-row sm:items-center sm:gap-0
w-full px-8 py-4 z-10 top-0 bg-blue max-md:px-4 shadow-md
"
>
<div className=" flex items-center gap-8 max-md:gap-4">
<div className=" md:hidden">
<Hamburger
toggled={isOpen}
size={20}
toggle={setOpen}
direction="right"
color="#fff"
/>
</div>
<p className="text-white inline-block bg-clip-text text-sm font-bold max-md:text-sm max-md:font-medium">
پنل کاربری {<span className=" max-md:hidden">|</span>}
</p>
<AnimatePresence>
{isOpen && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
className="fixed z-[100] left-0 shadow-4xl right-0 top-[3rem] p-5 bg-gradient-to-b from-blue to-white border-b border-b-white/20"
>
<ul className="grid gap-2 ">
{selectedMenu.map((item, idx, array) => {
// const { Icon } = item;
const lastItem = array.length - 1;
return (
<motion.li
initial={{ scale: 0, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
transition={{
type: "spring",
stiffness: 260,
damping: 20,
delay: 0.1 + idx / 10,
}}
key={item.name}
className=" w-full p-[0.08rem] rounded-xl bg-gradient-to-tr from-neutral-800 via-white to-neutral-700"
>
<Link
onClick={() => 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}
>
<span className="flex gap-1 text-sm">{item.name}</span>
{/* <Icon className="text-xl" /> */}
</Link>
</motion.li>
);
})}
</ul>
</motion.div>
)}
</AnimatePresence>
<div className="flex flex-row gap-7 max-md:gap-3 max-md:hidden">
{role && (
<>
{selectedMenu.map(
(item: { id: number; name: string; href: string }) => {
const isActive = activeLink === item.href;
return (
<Link key={item.id} href={item.href}>
<p
className={`text-white font-normal text-sm max-md:text-xs
hover:bg-white/50 rounded-sm p-1
animate-out duration-300 ${isActive ? 'bg-white/50 ' : '' }`}
>
{item.name}
</p>
</Link>
);
}
)}
</>
)}
</div>
</div>
<div className=" flex gap-4 items-center">
<h1 className="text-white inline-block bg-clip-text before:max-md:hidden text-sm font-bold max-md:text-xs max-md:font-medium">
{data && (
<>
{role === "Admin"
? `${data?.title ? data.title : data.username} (ادمین)`
: `${data?.title ? data.title : data.username} (بازرس)`}
</>
)}
</h1>
<div className="hover:bg-white/50 p-1 rounded-md">
<LogOut
onClick={() => {
deleteCookie("Sootzani_Token");
localStorage.removeItem("privateKey");
localStorage.removeItem("privatePassword");
router.push("/");
}}
className=" text-white cursor-pointer"
/>
</div>
</div>
</div>
);
}