"use client"; import React from "react"; import { Sidebar } from "flowbite-react"; import { useEffect, useState } from "react"; import classNames from "classnames"; import { css } from "@emotion/css"; import { FaChevronDown, FaChevronUp } from "react-icons/fa"; import { SidebarLinkBetter } from "../ui/link-better"; import { detectCase } from "@/lib/utils/detectCase"; import { useLocal } from "@/lib/utils/use-local"; import { get_user } from "@/lib/utils/get_user"; import { siteurl } from "@/lib/utils/siteurl"; import { ScrollArea } from "../ui/scroll-area"; import { LuChevronsLeftRight } from "react-icons/lu"; import { Popover } from "../Popover/Popover"; import { HiEye } from "react-icons/hi"; import { MdOutlineMoreVert } from "react-icons/md"; import api from "@/lib/utils/axios"; import { PiUserSwitch } from "react-icons/pi"; import { IoIosLogOut } from "react-icons/io"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "../ui/tooltip"; import ImageBetter from "../ui/Image"; import { ListUI } from "../list/ListUI"; import { apix } from "@/lib/utils/apix"; import { getNumber } from "@/lib/utils/getNumber"; interface TreeMenuItem { title: string; href?: string; children?: TreeMenuItem[]; icon?: any; } interface TreeMenuProps { data: TreeMenuItem[]; minimaze: () => void; onClick?: () => void; mini: boolean; } const SidebarBetterTree: React.FC = ({ data, minimaze, mini, onClick, }) => { const [currentPage, setCurrentPage] = useState(""); const [notification, setNotification] = useState(false as boolean); const [profile, setProfile] = useState(false as boolean); const local = useLocal({ data: data, ready: false as boolean, }); useEffect(() => { if (typeof location === "object") { const newPage = window.location.pathname; setCurrentPage(newPage); } const run = async () => { local.ready = false; local.render(); setTimeout(() => { local.ready = true; local.render(); }, 1000); }; if (typeof window === "object") { run(); } }, []); const isChildActive = (items: TreeMenuItem[]): boolean => { return items.some((item) => { if (item.href && currentPage.startsWith(item.href)) return true; if (item.children) return isChildActive(item.children); // Rekursif return false; }); }; const renderTree = (items: TreeMenuItem[], depth: number = 0) => { return items.map((item, index) => { const hasChildren = item.children && item.children.length > 0; let isActive = item.href && detectCase(currentPage, item.href); let isParentActive = hasChildren && isChildActive(item.children!); const [isOpen, setIsOpen] = useState(isParentActive); useEffect(() => { if (isParentActive) { setIsOpen(true); } }, [isParentActive]); const itemStyle = { paddingLeft: mini && !depth ? "10px" : !hasChildren && !depth ? "13px" : !mini ? `${depth * 16}px` : "0px", }; return ( {hasChildren ? (
  • {mini && !depth ? ( <>
    span { white-space: wrap !important; } `, mini ? isParentActive ? "bg-linear-sidebar-active text-white font-normal p-1 shadow-md w-[50px]" : isActive ? !depth ? "bg-linear-sidebar-active text-white font-normal p-1 shadow-md" : " bg-layer text-primary font-bold p-1 " : "text-primary bg-transparent p-1 hover:bg-card-layer hover:shadow-md w-[50px]" : isActive ? !depth ? " bg-linear-sidebar-active font-bold text-white shadow-md " : " bg-layer text-primary font-bold " : "text-white" )} onClick={() => { if (mini) { minimaze(); } setIsOpen(!isOpen); }} style={mini ? {} : itemStyle} >
    {!depth ? (
    {item.icon}
    ) : ( <> )} {!mini ? ( <>
    {item.title}
    {isOpen ? ( ) : ( )}
    ) : ( <> )}

    {item.title}

    ) : ( <>
    span { white-space: wrap !important; } `, mini ? isActive ? !depth ? "bg-linear-sidebar-active text-white font-normal p-1 shadow-md" : " bg-layer text-primary font-bold p-1 " : "text-primary bg-transparent p-1 hover:bg-card-layer hover:shadow-md w-[50px]" : isActive ? !depth ? " bg-linear-sidebar-active font-bold text-white shadow-md " : " bg-layer text-primary font-bold " : "text-sidebar-label" )} onClick={() => { if (mini) { minimaze(); } setIsOpen(!isOpen); }} style={mini ? {} : itemStyle} >
    {!depth ? (
    {item.icon}
    ) : ( <> )} {!mini ? ( <>
    {item.title}
    {isOpen ? : }
    ) : ( <> )}
    )}
    {renderTree(item.children!, depth + 1)}
  • ) : (
  • {mini ? (
    { if (item?.href) setCurrentPage(item.href); if (typeof onClick === "function") { onClick(); } }} className={classNames( "transition-all font-bold relative flex-row flex items-center cursor-pointer items-center text-base flex flex-row ", isActive ? " text-base" : "hover:bg-card-layer hover:shadow-md hover:text-primary", mini ? "transition-all duration-200 justify-center ml-0 rounded-lg" : "py-2.5 px-4 rounded-md flex-grow mx-2", !depth && !hasChildren ? "px-2" : "", css` & > span { white-space: wrap !important; } `, mini ? isActive ? !depth ? "bg-linear-sidebar-active text-white font-normal p-1 shadow-md" : " bg-layer text-primary font-bold p-1 " : "text-primary bg-transparent p-1 hover:bg-card-layer hover:shadow-md" : isActive ? !depth ? " bg-linear-sidebar-active font-bold text-white shadow-md" : " bg-linear-sidebar-active text-white font-bold " : "text-sidebar-label" )} style={mini ? {} : itemStyle} // Terapkan gaya berdasarkan depth >
    {!depth ? (
    {item.icon}
    ) : ( <> )} {!mini ? ( <>
    {item.title}
    ) : ( <> )}

    {item.title}

    ) : ( { if (item?.href) setCurrentPage(item.href); if (typeof onClick === "function") { onClick(); } }} className={classNames( "transition-all font-bold relative flex-row flex items-center cursor-pointer items-center text-base flex flex-row ", isActive ? " text-base" : "hover:bg-card-layer hover:shadow-md hover:text-primary", mini ? "transition-all duration-200 justify-center ml-0 rounded-lg" : "py-2.5 px-4 rounded-md flex-grow mx-2", !depth && !hasChildren ? "px-2" : "", css` & > span { white-space: wrap !important; } `, mini ? isActive ? !depth ? "bg-linear-sidebar-active text-white font-normal p-1 shadow-md" : " bg-layer text-primary font-bold p-1 " : "text-primary bg-transparent p-1 hover:bg-card-layer hover:shadow-md" : isActive ? !depth ? " bg-linear-sidebar-active font-bold text-white shadow-md" : " bg-linear-sidebar-active text-white font-bold " : "text-sidebar-label" )} style={mini ? {} : itemStyle} // Terapkan gaya berdasarkan depth >
    {!depth ? (
    {item.icon}
    ) : ( <> )} {!mini ? ( <>
    {item.title}
    ) : ( <> )}
    )}
  • )}
    ); }); }; return (
    { minimaze(); localStorage.setItem("mini", !mini ? "true" : "false"); }} >
    div { border-radius: 0px; background: transparent; padding-top: 0; padding-right: 0; padding-left: 0; padding-bottom: 0; } ` )} >
    {renderTree(data)}
    { setNotification(open); }} open={notification} content={
    Notifications
    { return ( <>
    New message from  Bonnie Green : "Hey, what's up? All set for the presentation?"
    a few moments ago
    ); }} onLoad={async (param: any) => { const params = await events("onload-param", { ...param, }); const result: any = await apix({ port: "recruitment", value: "data.data.job_postings", path: `/api/job-postings${params}`, validate: "array", }); return result; }} onCount={async () => { const result: any = await apix({ port: "recruitment", value: "data.data.total", path: `/api/job-postings?page=1&page_size=1`, validate: "object", }); return getNumber(result); }} />
    View all
    } >
    {!mini && (
    Notifications{" "}
    912
    )}
    {!mini && "Sign Out"}
    {!mini && (
    {get_user("employee.name") ? get_user("employee.name") : "-"}
    {get_user("email") ? get_user("email") : "-"}
    { setProfile(open); }} open={profile} content={
    Profile
    { if (typeof window === "object") navigate( `${process.env.NEXT_PUBLIC_API_PORTAL}/choose-roles?state=${process.env.NEXT_PUBLIC_NAME}` ); }} className="cursor-pointer px-4 py-2 flex border-y hover:bg-gray-100 dark:border-gray-600 dark:hover:bg-gray-600" >
    Switch Role
    { await api.delete( process.env.NEXT_PUBLIC_BASE_URL + "/api/destroy-cookies" ); localStorage.removeItem("user"); if (typeof window === "object") navigate( `${process.env.NEXT_PUBLIC_API_PORTAL}/logout` ); }} className="cursor-pointer px-4 py-2 flex hover:bg-gray-100 dark:border-gray-600 dark:hover:bg-gray-600" >
    Sign Out
    } >
    )}
    ); }; export default SidebarBetterTree;