172 lines
5.1 KiB
TypeScript
172 lines
5.1 KiB
TypeScript
import { useState, useEffect, useRef } from "react";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { ChevronRight } from "lucide-react";
|
|
import { getConfig } from "@common/config";
|
|
import { ENDPOINT } from "../../config";
|
|
import { cn, setCookie } from "../../lib";
|
|
import { getMenuList, type MenuItem } from "../../api";
|
|
import {
|
|
Sidebar,
|
|
SidebarContent,
|
|
SidebarMenu,
|
|
SidebarMenuButton,
|
|
SidebarMenuItem,
|
|
SidebarMenuSub,
|
|
} from "../sidebar";
|
|
import {
|
|
Collapsible,
|
|
CollapsibleContent,
|
|
CollapsibleTrigger,
|
|
} from "../collapsible";
|
|
import { getParentIds } from "./lib";
|
|
|
|
const { SITE_ID } = getConfig();
|
|
|
|
export function LNB({
|
|
viewOnly,
|
|
currentPath,
|
|
handleMenuClick,
|
|
}: {
|
|
viewOnly?: boolean;
|
|
currentPath?: string;
|
|
handleMenuClick?: (item: MenuItem) => void;
|
|
}) {
|
|
const { data: menuList } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.MENU_ROLES],
|
|
queryFn: () => getMenuList(SITE_ID ?? ""),
|
|
enabled: !viewOnly,
|
|
});
|
|
|
|
const [openMenuIds, setOpenMenuIds] = useState<number[]>([]);
|
|
const userClosedMenusRef = useRef(new Set());
|
|
const prevPathRef = useRef(currentPath);
|
|
|
|
function isPathMatched(path: string) {
|
|
if (!path) return false;
|
|
return currentPath?.startsWith(path);
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (!menuList || !currentPath) return;
|
|
|
|
const matchedMenuIds: number[] = [];
|
|
|
|
const findMatchedMenus = (items: MenuItem[]) => {
|
|
items.forEach((item) => {
|
|
if (isPathMatched(item.urlPath)) {
|
|
const parentIds = getParentIds(item.menuCd)(menuList);
|
|
matchedMenuIds.push(...parentIds, item.menuCd);
|
|
}
|
|
|
|
if (item.children) {
|
|
findMatchedMenus(item.children);
|
|
}
|
|
});
|
|
};
|
|
|
|
findMatchedMenus(menuList);
|
|
|
|
if (matchedMenuIds.length > 0) {
|
|
const filteredIds = matchedMenuIds.filter(
|
|
(id) => !userClosedMenusRef.current.has(id),
|
|
);
|
|
setOpenMenuIds((prev) => [...new Set([...prev, ...filteredIds])]);
|
|
}
|
|
|
|
if (prevPathRef.current !== currentPath) {
|
|
setOpenMenuIds(matchedMenuIds);
|
|
prevPathRef.current = currentPath;
|
|
}
|
|
}, [menuList, currentPath]);
|
|
|
|
function MenuItem({ item }: { item: MenuItem }) {
|
|
const isMatched = isPathMatched(item.urlPath);
|
|
|
|
const handleClick = () => {
|
|
setCookie("menu-code-key", item.menuCd.toString());
|
|
handleMenuClick?.(item);
|
|
};
|
|
|
|
if (item.children) {
|
|
return <MenuGroup item={item} />;
|
|
}
|
|
|
|
return (
|
|
<SidebarMenuItem>
|
|
<SidebarMenuButton
|
|
className={cn(
|
|
"h-auto py-4 text-[1.4rem] text-label transition-colors hover:bg-accent",
|
|
isMatched && "bg-accent !text-primary",
|
|
)}
|
|
data-matched={isMatched}
|
|
onClick={handleClick}
|
|
>
|
|
<i className={cn("diveicon ml-2", `di-${item.iconNm}`)} />
|
|
{item.menuKornNm}
|
|
</SidebarMenuButton>
|
|
</SidebarMenuItem>
|
|
);
|
|
}
|
|
|
|
function MenuGroup({ item }: { item: MenuItem }) {
|
|
const isOpen = openMenuIds.includes(item.menuCd);
|
|
|
|
const handleOpenChange = (open: boolean) => {
|
|
if (open) {
|
|
// 열 때는 부모들도 함께 열기
|
|
if (!openMenuIds.includes(item.menuCd)) {
|
|
const parentIds = getParentIds(item.menuCd)(menuList ?? []);
|
|
setOpenMenuIds((prev) => [...prev, ...parentIds, item.menuCd]);
|
|
}
|
|
userClosedMenusRef.current.delete(item.menuCd);
|
|
} else {
|
|
// 닫을 때는 현재 메뉴만 닫기
|
|
setOpenMenuIds((prev) => prev.filter((id) => id !== item.menuCd));
|
|
userClosedMenusRef.current.add(item.menuCd);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Collapsible open={isOpen} onOpenChange={handleOpenChange}>
|
|
<SidebarMenuItem>
|
|
<CollapsibleTrigger asChild>
|
|
<div className="group">
|
|
<SidebarMenuButton className="group/menu-button flex h-auto items-center justify-between py-4 pr-2 text-[1.4rem] group-data-[state=open]:bg-transparent">
|
|
<div className="flex items-center gap-2">
|
|
<i
|
|
className={cn(
|
|
"diveicon ml-2 text-label",
|
|
`di-${item.iconNm}`,
|
|
)}
|
|
/>
|
|
<span className="text-label group-hover/menu-button:text-foreground">
|
|
{item.menuKornNm}
|
|
</span>
|
|
</div>
|
|
<ChevronRight className="!size-6 text-label group-data-[state=open]:rotate-90" />
|
|
</SidebarMenuButton>
|
|
</div>
|
|
</CollapsibleTrigger>
|
|
<CollapsibleContent>
|
|
<SidebarMenuSub className="mr-0">
|
|
{item.children?.map((child) => (
|
|
<MenuItem key={child.menuCd} item={child} />
|
|
))}
|
|
</SidebarMenuSub>
|
|
</CollapsibleContent>
|
|
</SidebarMenuItem>
|
|
</Collapsible>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Sidebar className="border-border bg-background px-4 py-[6rem]">
|
|
<SidebarContent className="bg-background">
|
|
<SidebarMenu className="gap-0">
|
|
{menuList?.map((item) => <MenuItem key={item.menuCd} item={item} />)}
|
|
</SidebarMenu>
|
|
</SidebarContent>
|
|
</Sidebar>
|
|
);
|
|
}
|