"use client";
import React, { useState, useCallback } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import {
Users,
LogOut,
ChevronsRight,
ChevronsLeft,
FileText,
UserCog,
Key,
ChartBarIncreasing,
BookOpen,
WashingMachine,
} from "lucide-react";
import { Button } from "@/shared/components/ui/button";
import { ScrollArea } from "@/shared/components/ui/scroll-area";
import { cn } from "@/shared/utils/utils";
import Image from "next/image";
import { useAuthSession } from "@/shared/hooks/use-session";
import { getRoleLabelById, hasPermission } from "@/shared/config/role";
import { handleLogout } from "@/shared/hooks/use-auth-api";
import { appConfig } from "@/shared/config/app-config";
interface MenuItem {
name: string;
href: string;
icon: React.ReactNode;
module: string; // default ke 'read' jika tidak diisi
}
const menuItems: MenuItem[] = [
{
name: "Manajemen Peta",
href: "/admin/mapset",
module: "mapset",
icon: ,
},
{
name: "Manajemen User",
href: "/admin/user",
icon: ,
module: "user",
},
{
name: "Manajemen Konten",
href: "/admin/news",
icon: ,
module: "news",
},
];
const settingsItems: MenuItem[] = [
{
name: "Perangkat Daerah",
href: "/admin/organization",
icon: ,
module: "organization",
},
{
name: "Kategori",
href: "/admin/category",
icon: ,
module: "category",
},
{
name: "Mapserver & Metadata",
href: "/admin/map-source",
icon: ,
module: "map-source",
},
{
name: "Kredensial",
href: "/admin/credential",
icon: ,
module: "credential",
},
];
const Sidebar = () => {
const pathname = usePathname();
const [collapsed, setCollapsed] = useState(false);
const { session } = useAuthSession();
const userRole = session?.user?.role;
const isActive = useCallback(
(href: string) => {
if (pathname === href) return true;
if (href !== "/" && pathname.startsWith(href)) {
const nextChar = pathname.charAt(href.length);
return nextChar === "" || nextChar === "/";
}
return false;
},
[pathname]
);
const filteredMenuItems = menuItems.filter(
(item) => userRole && hasPermission(userRole, item.module, "read")
);
const filteredSettingsItems = settingsItems.filter(
(item) => userRole && hasPermission(userRole, item.module, "read")
);
return (
{!collapsed && (
)}
{/* Features Section */}
{filteredMenuItems.length > 0 && !collapsed && (
Features
)}
{filteredSettingsItems.length > 0 && !collapsed && (
Settings
)}
{session?.user?.name?.[0] ?? "U"}
{!collapsed && (
{session?.user?.name}
{getRoleLabelById(userRole?.name ?? "")}
)}
);
};
export default Sidebar;