446 lines
14 KiB
TypeScript
446 lines
14 KiB
TypeScript
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
import { Link, useLocation } from "react-router-dom";
|
|
|
|
// Assume these icons are imported from an icon library
|
|
import {
|
|
BoxCubeIcon,
|
|
ChevronDownIcon,
|
|
GridIcon,
|
|
GroupIcon,
|
|
HorizontaLDots,
|
|
ListIcon,
|
|
PieChartIcon,
|
|
TableIcon,
|
|
UserCircleIcon,
|
|
PlugInIcon,
|
|
UserIcon,
|
|
} from "../icons";
|
|
import { useSidebar } from "../context/SidebarContext";
|
|
// import SidebarWidget from "./SidebarWidget";
|
|
import { apiGet } from "../lib/api";
|
|
|
|
type NavItem = {
|
|
name: string;
|
|
icon: React.ReactNode;
|
|
path?: string;
|
|
subItems?: { name: string; path: string; pro?: boolean; new?: boolean }[];
|
|
};
|
|
|
|
const navItems: NavItem[] = [
|
|
{
|
|
icon: <GridIcon />,
|
|
name: "Dashboard",
|
|
path: "/",
|
|
},
|
|
{
|
|
icon: <BoxCubeIcon />,
|
|
name: "Produk",
|
|
path: "/produk",
|
|
},
|
|
{
|
|
name: "Stok",
|
|
icon: <TableIcon />,
|
|
subItems: [
|
|
{ name: "Stok Gudang", path: "/stok-gudang" },
|
|
{ name: "Stok Toko", path: "/stok-toko" },
|
|
{ name: "Penerimaan Barang", path: "/penerimaan-barang" },
|
|
{ name: "Transfer Gudang → Toko", path: "/transfer-stok" },
|
|
],
|
|
},
|
|
{
|
|
name: "Transaksi",
|
|
icon: <ListIcon />,
|
|
path: "/transaksi",
|
|
},
|
|
{
|
|
name: "Piutang",
|
|
icon: <ListIcon />,
|
|
path: "/piutang",
|
|
},
|
|
{
|
|
name: "Voucher",
|
|
icon: <TableIcon />,
|
|
path: "/voucher",
|
|
},
|
|
{
|
|
name: "Laporan",
|
|
icon: <PieChartIcon />,
|
|
subItems: [
|
|
{ name: "Laporan Harian", path: "/laporan-harian" },
|
|
{ name: "Laporan Bulanan", path: "/laporan-bulanan" },
|
|
],
|
|
},
|
|
{
|
|
icon: <GroupIcon />,
|
|
name: "Pegawai",
|
|
path: "/pegawai",
|
|
},
|
|
{
|
|
icon: <UserCircleIcon />,
|
|
name: "Distributor",
|
|
path: "/distributor",
|
|
},
|
|
{
|
|
icon: <UserIcon />,
|
|
name: "User",
|
|
path: "/users",
|
|
},
|
|
{
|
|
icon: <PlugInIcon />,
|
|
name: "Pengaturan",
|
|
path: "/pengaturan",
|
|
},
|
|
];
|
|
|
|
// Menu demo "Others" tidak dipakai untuk POS.
|
|
const othersItems: NavItem[] = [];
|
|
|
|
const AppSidebar: React.FC = () => {
|
|
const { isExpanded, isMobileOpen, isHovered, setIsHovered } = useSidebar();
|
|
const location = useLocation();
|
|
const [storeName, setStoreName] = useState("POS");
|
|
const [storeLogo, setStoreLogo] = useState("");
|
|
const [role, setRole] = useState<"admin" | "kasir" | "gudang" | null>(null);
|
|
|
|
const [openSubmenu, setOpenSubmenu] = useState<{
|
|
type: "main" | "others";
|
|
index: number;
|
|
} | null>(null);
|
|
const [subMenuHeight, setSubMenuHeight] = useState<Record<string, number>>(
|
|
{}
|
|
);
|
|
const subMenuRefs = useRef<Record<string, HTMLDivElement | null>>({});
|
|
|
|
// const isActive = (path: string) => location.pathname === path;
|
|
const isActive = useCallback(
|
|
(path: string) => location.pathname === path,
|
|
[location.pathname]
|
|
);
|
|
|
|
const visibleNavItems = useMemo(() => {
|
|
if (!role || role === "admin") return navItems;
|
|
const allow =
|
|
role === "kasir"
|
|
? new Set(["/", "/transaksi", "/piutang", "/laporan-harian", "/laporan-bulanan"])
|
|
: new Set([
|
|
"/",
|
|
"/produk",
|
|
"/stok-gudang",
|
|
"/transfer-stok",
|
|
"/penerimaan-barang",
|
|
]);
|
|
|
|
return navItems
|
|
.map((item) => {
|
|
if (item.path) {
|
|
return allow.has(item.path) ? item : null;
|
|
}
|
|
if (item.subItems) {
|
|
const subItems = item.subItems.filter((sub) => allow.has(sub.path));
|
|
if (subItems.length === 0) return null;
|
|
return { ...item, subItems };
|
|
}
|
|
return null;
|
|
})
|
|
.filter(Boolean) as NavItem[];
|
|
}, [role]);
|
|
|
|
useEffect(() => {
|
|
let submenuMatched = false;
|
|
["main", "others"].forEach((menuType) => {
|
|
const items = menuType === "main" ? visibleNavItems : othersItems;
|
|
items.forEach((nav, index) => {
|
|
if (nav.subItems) {
|
|
nav.subItems.forEach((subItem) => {
|
|
if (isActive(subItem.path)) {
|
|
setOpenSubmenu({
|
|
type: menuType as "main" | "others",
|
|
index,
|
|
});
|
|
submenuMatched = true;
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
if (!submenuMatched) {
|
|
setOpenSubmenu(null);
|
|
}
|
|
}, [location, isActive, visibleNavItems]);
|
|
|
|
useEffect(() => {
|
|
let mounted = true;
|
|
Promise.all([
|
|
apiGet<{ data: { toko?: { nama?: string; logo?: string } } }>(
|
|
"/admin-api/pengaturan"
|
|
),
|
|
apiGet<{ data?: { role?: string } }>("/admin-api/me"),
|
|
])
|
|
.then(([resSet, resMe]) => {
|
|
const name = resSet?.data?.toko?.nama?.trim();
|
|
const logo = resSet?.data?.toko?.logo || "";
|
|
const userRole = (resMe?.data?.role as
|
|
| "admin"
|
|
| "kasir"
|
|
| "gudang"
|
|
| undefined) ?? null;
|
|
if (mounted && name) setStoreName(name);
|
|
if (mounted) setStoreLogo(logo);
|
|
if (mounted) setRole(userRole || "admin");
|
|
})
|
|
.catch(() => {
|
|
if (mounted) setRole("admin");
|
|
});
|
|
return () => {
|
|
mounted = false;
|
|
};
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (openSubmenu !== null) {
|
|
const key = `${openSubmenu.type}-${openSubmenu.index}`;
|
|
if (subMenuRefs.current[key]) {
|
|
setSubMenuHeight((prevHeights) => ({
|
|
...prevHeights,
|
|
[key]: subMenuRefs.current[key]?.scrollHeight || 0,
|
|
}));
|
|
}
|
|
}
|
|
}, [openSubmenu]);
|
|
|
|
const handleSubmenuToggle = (index: number, menuType: "main" | "others") => {
|
|
setOpenSubmenu((prevOpenSubmenu) => {
|
|
if (
|
|
prevOpenSubmenu &&
|
|
prevOpenSubmenu.type === menuType &&
|
|
prevOpenSubmenu.index === index
|
|
) {
|
|
return null;
|
|
}
|
|
return { type: menuType, index };
|
|
});
|
|
};
|
|
|
|
const renderMenuItems = (items: NavItem[], menuType: "main" | "others") => (
|
|
<ul className="flex flex-col gap-4">
|
|
{items.map((nav, index) => (
|
|
<li key={nav.name}>
|
|
{nav.subItems ? (
|
|
<button
|
|
onClick={() => handleSubmenuToggle(index, menuType)}
|
|
className={`menu-item group ${
|
|
openSubmenu?.type === menuType && openSubmenu?.index === index
|
|
? "menu-item-active"
|
|
: "menu-item-inactive"
|
|
} cursor-pointer ${
|
|
!isExpanded && !isHovered
|
|
? "lg:justify-center"
|
|
: "lg:justify-start"
|
|
}`}
|
|
>
|
|
<span
|
|
className={`menu-item-icon-size ${
|
|
openSubmenu?.type === menuType && openSubmenu?.index === index
|
|
? "menu-item-icon-active"
|
|
: "menu-item-icon-inactive"
|
|
}`}
|
|
>
|
|
{nav.icon}
|
|
</span>
|
|
{(isExpanded || isHovered || isMobileOpen) && (
|
|
<span className="menu-item-text">{nav.name}</span>
|
|
)}
|
|
{(isExpanded || isHovered || isMobileOpen) && (
|
|
<ChevronDownIcon
|
|
className={`ml-auto w-5 h-5 transition-transform duration-200 ${
|
|
openSubmenu?.type === menuType &&
|
|
openSubmenu?.index === index
|
|
? "rotate-180 text-brand-500"
|
|
: ""
|
|
}`}
|
|
/>
|
|
)}
|
|
</button>
|
|
) : (
|
|
nav.path && (
|
|
<Link
|
|
to={nav.path}
|
|
className={`menu-item group ${
|
|
isActive(nav.path) ? "menu-item-active" : "menu-item-inactive"
|
|
}`}
|
|
>
|
|
<span
|
|
className={`menu-item-icon-size ${
|
|
isActive(nav.path)
|
|
? "menu-item-icon-active"
|
|
: "menu-item-icon-inactive"
|
|
}`}
|
|
>
|
|
{nav.icon}
|
|
</span>
|
|
{(isExpanded || isHovered || isMobileOpen) && (
|
|
<span className="menu-item-text">{nav.name}</span>
|
|
)}
|
|
</Link>
|
|
)
|
|
)}
|
|
{nav.subItems && (isExpanded || isHovered || isMobileOpen) && (
|
|
<div
|
|
ref={(el) => {
|
|
subMenuRefs.current[`${menuType}-${index}`] = el;
|
|
}}
|
|
className="overflow-hidden transition-all duration-300"
|
|
style={{
|
|
height:
|
|
openSubmenu?.type === menuType && openSubmenu?.index === index
|
|
? `${subMenuHeight[`${menuType}-${index}`]}px`
|
|
: "0px",
|
|
}}
|
|
>
|
|
<ul className="mt-2 space-y-1 ml-9">
|
|
{nav.subItems.map((subItem) => (
|
|
<li key={subItem.name}>
|
|
<Link
|
|
to={subItem.path}
|
|
className={`menu-dropdown-item ${
|
|
isActive(subItem.path)
|
|
? "menu-dropdown-item-active"
|
|
: "menu-dropdown-item-inactive"
|
|
}`}
|
|
>
|
|
{subItem.name}
|
|
<span className="flex items-center gap-1 ml-auto">
|
|
{subItem.new && (
|
|
<span
|
|
className={`ml-auto ${
|
|
isActive(subItem.path)
|
|
? "menu-dropdown-badge-active"
|
|
: "menu-dropdown-badge-inactive"
|
|
} menu-dropdown-badge`}
|
|
>
|
|
new
|
|
</span>
|
|
)}
|
|
{subItem.pro && (
|
|
<span
|
|
className={`ml-auto ${
|
|
isActive(subItem.path)
|
|
? "menu-dropdown-badge-active"
|
|
: "menu-dropdown-badge-inactive"
|
|
} menu-dropdown-badge`}
|
|
>
|
|
pro
|
|
</span>
|
|
)}
|
|
</span>
|
|
</Link>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
);
|
|
|
|
return (
|
|
<aside
|
|
className={`fixed mt-16 flex flex-col lg:mt-0 top-0 px-5 left-0 bg-white dark:bg-gray-900 dark:border-gray-800 text-gray-900 h-screen transition-all duration-300 ease-in-out z-50 border-r border-gray-200
|
|
${
|
|
isExpanded || isMobileOpen
|
|
? "w-[290px]"
|
|
: isHovered
|
|
? "w-[290px]"
|
|
: "w-[90px]"
|
|
}
|
|
${isMobileOpen ? "translate-x-0" : "-translate-x-full"}
|
|
lg:translate-x-0`}
|
|
onMouseEnter={() => !isExpanded && setIsHovered(true)}
|
|
onMouseLeave={() => setIsHovered(false)}
|
|
>
|
|
<div
|
|
className={`py-8 flex ${
|
|
!isExpanded && !isHovered ? "lg:justify-center" : "justify-start"
|
|
}`}
|
|
>
|
|
<Link to="/">
|
|
{isExpanded || isHovered || isMobileOpen ? (
|
|
<div className="flex items-center gap-3">
|
|
{storeLogo ? (
|
|
<img
|
|
src={storeLogo}
|
|
alt="Logo"
|
|
className="h-9 w-9 rounded-lg border border-gray-200 bg-white object-contain dark:border-gray-800"
|
|
/>
|
|
) : null}
|
|
<div className="flex flex-col">
|
|
<span className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
|
{storeName}
|
|
</span>
|
|
<span className="text-xs text-gray-500 dark:text-gray-400">
|
|
Point of Sale
|
|
</span>
|
|
</div>
|
|
</div>
|
|
) : storeLogo ? (
|
|
<img
|
|
src={storeLogo}
|
|
alt="Logo"
|
|
className="h-8 w-8 rounded-lg border border-gray-200 bg-white object-contain dark:border-gray-800"
|
|
/>
|
|
) : (
|
|
<div className="flex h-8 w-8 items-center justify-center rounded-full bg-gray-100 text-xs font-semibold text-gray-600 dark:bg-gray-800 dark:text-gray-300">
|
|
{storeName.trim().charAt(0) || "P"}
|
|
</div>
|
|
)}
|
|
</Link>
|
|
</div>
|
|
<div className="flex flex-col overflow-y-auto duration-300 ease-linear no-scrollbar">
|
|
<nav className="mb-6">
|
|
<div className="flex flex-col gap-4">
|
|
<div>
|
|
<h2
|
|
className={`mb-4 text-xs uppercase flex leading-[20px] text-gray-400 ${
|
|
!isExpanded && !isHovered
|
|
? "lg:justify-center"
|
|
: "justify-start"
|
|
}`}
|
|
>
|
|
{isExpanded || isHovered || isMobileOpen ? (
|
|
"Menu"
|
|
) : (
|
|
<HorizontaLDots className="size-6" />
|
|
)}
|
|
</h2>
|
|
{renderMenuItems(visibleNavItems, "main")}
|
|
</div>
|
|
{othersItems.length > 0 ? (
|
|
<div className="">
|
|
<h2
|
|
className={`mb-4 text-xs uppercase flex leading-[20px] text-gray-400 ${
|
|
!isExpanded && !isHovered
|
|
? "lg:justify-center"
|
|
: "justify-start"
|
|
}`}
|
|
>
|
|
{isExpanded || isHovered || isMobileOpen ? (
|
|
"Others"
|
|
) : (
|
|
<HorizontaLDots />
|
|
)}
|
|
</h2>
|
|
{renderMenuItems(othersItems, "others")}
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
</nav>
|
|
{isExpanded || isHovered || isMobileOpen}
|
|
</div>
|
|
</aside>
|
|
);
|
|
};
|
|
|
|
export default AppSidebar;
|