206 lines
7.9 KiB
TypeScript
206 lines
7.9 KiB
TypeScript
"use client";
|
||
|
||
import React, { useEffect, useState } from "react";
|
||
import { Input } from "./ui/input";
|
||
import Image from "next/image";
|
||
import { authorizedMenuItems, menuItems } from "@/constants";
|
||
import Link from "next/link";
|
||
import Login from "./Login";
|
||
import { Button } from "./ui/button";
|
||
import { usePathname } from "next/navigation";
|
||
import TopNav from "./TopNav";
|
||
import { cn, isAuthorized } from "@/lib/utils";
|
||
import useToken from "@/store/useToken";
|
||
import { DropdownMenu } from "@radix-ui/react-dropdown-menu";
|
||
import ProfileDropDown from "./ProfileDropDown";
|
||
import { getCookie } from "cookies-next";
|
||
import Hamburger from "hamburger-react";
|
||
import { AnimatePresence, motion } from "framer-motion";
|
||
export default function Navbar() {
|
||
const pathname = usePathname();
|
||
const isAuth = useToken((state) => state.token);
|
||
const [auth, setAuth] = useState<boolean>();
|
||
const [isOpen, setOpen] = useState<boolean>(false);
|
||
const token = getCookie("Sootzani_Token");
|
||
useEffect(() => {
|
||
const token = getCookie("Sootzani_Token");
|
||
if (token) {
|
||
setAuth(true);
|
||
} else {
|
||
setAuth(false);
|
||
}
|
||
}, [isAuth, token]);
|
||
console.log(pathname);
|
||
return (
|
||
<div className={` left-0 right-0 top-0 z-20 fixed`}>
|
||
{/* <TopNav /> */}
|
||
<div className=" bg-white h-16 shadow-md flex items-center justify-between py-2 px-6">
|
||
<div className=" flex ">
|
||
<div className=" md:hidden -mr-4">
|
||
<Hamburger
|
||
toggled={isOpen}
|
||
size={20}
|
||
toggle={setOpen}
|
||
direction="right"
|
||
/>
|
||
<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-[4rem] p-5 bg-white border-b-2 shadow-md"
|
||
>
|
||
<ul className="grid gap-2 ">
|
||
{auth ? (
|
||
<>
|
||
{authorizedMenuItems.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-yellow" : "bg-blue"} `}
|
||
href={item.href}
|
||
>
|
||
<span className="flex gap-1 text-sm">
|
||
{item.name}
|
||
</span>
|
||
{/* <Icon className="text-xl" /> */}
|
||
</Link>
|
||
</motion.li>
|
||
);
|
||
})}
|
||
</>
|
||
) : (
|
||
<>
|
||
{menuItems.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-yellow" : "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>
|
||
<div className=" flex items-center max-md:mr-1 gap-5">
|
||
<Link href="/" className=" max-sm:hidden">
|
||
<Image
|
||
src="/img/logo.png"
|
||
quality={100}
|
||
alt="log"
|
||
width={32}
|
||
height={32}
|
||
/>
|
||
</Link>
|
||
|
||
<div className=" text-brown flex flex-col text-center">
|
||
<Link href="/">
|
||
<h1 className="text-[23px] w-full">عیــون</h1>
|
||
<h1 className="text-[9px]">سامانه افشای فساد شهرداری</h1>
|
||
</Link>
|
||
</div>
|
||
</div>
|
||
<div className=" flex gap-5 items-center mr-8 max-md:hidden">
|
||
{auth ? (
|
||
<>
|
||
{authorizedMenuItems.map((item, index, array) => {
|
||
return (
|
||
<Link
|
||
className={cn(
|
||
" p-2 rounded-md",
|
||
pathname == item.href && "bg-gray-200"
|
||
)}
|
||
key={item.id}
|
||
href={item.href}
|
||
>
|
||
<div className=" text-sm font-bold">{item.name}</div>
|
||
</Link>
|
||
);
|
||
})}
|
||
</>
|
||
) : (
|
||
<>
|
||
{menuItems.map((item, index, array) => {
|
||
if (array.length - 1 !== index) {
|
||
return (
|
||
<Link
|
||
className={cn(
|
||
" p-1 rounded-md",
|
||
pathname == item.href && "bg-gray-200"
|
||
)}
|
||
key={item.id}
|
||
href={item.href}
|
||
>
|
||
<div className=" text-sm font-bold p-2">
|
||
{item.name}
|
||
</div>
|
||
</Link>
|
||
);
|
||
}
|
||
})}
|
||
</>
|
||
)}
|
||
</div>
|
||
</div>
|
||
{auth ? (
|
||
<ProfileDropDown />
|
||
) : (
|
||
<Link href="/Login">
|
||
<Button
|
||
onClick={() =>
|
||
localStorage.setItem("Sootzani_Login_State", "true")
|
||
}
|
||
className=" bg-blue hover:bg-blue/90 rounded-lg max-sm:text-xs"
|
||
>
|
||
ورود به پنل کاربری
|
||
</Button>
|
||
</Link>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|