"use client" import * as React from 'react'; import Box from '@mui/material/Box'; import Stack from '@mui/material/Stack'; import SvgIcon, { SvgIconProps } from '@mui/material/SvgIcon'; import { styled } from '@mui/material/styles'; import { SimpleTreeView } from '@mui/x-tree-view/SimpleTreeView'; import { TreeItem, treeItemClasses } from '@mui/x-tree-view/TreeItem'; import { Minus, MinusSquare, Plus, SquarePlus } from 'lucide-react'; import { Button } from "@/components/ui/button"; import { mainNav, additionalNav } from "@/config/menuConfig"; import { PermissionDialog } from './permissionDialog'; import { useParams } from 'next/navigation'; import { useTranslation } from '@/i18n/client'; // ------------------- Custom TreeItem Style ------------------- const CustomTreeItem = styled(TreeItem)({ [`& .${treeItemClasses.iconContainer}`]: { '& .close': { opacity: 0.3, }, }, }); function CloseSquare(props: SvgIconProps) { return ( {/* tslint:disable-next-line: max-line-length */} ); } interface SelectedKey { key: string; title: string; } export default function PermissionsTree() { const params = useParams(); const lng = (params?.lng as string) || 'ko'; const { t } = useTranslation(lng, 'menu'); const [expandedItems, setExpandedItems] = React.useState([]); const [dialogOpen, setDialogOpen] = React.useState(false); const [selectedKey, setSelectedKey] = React.useState(null); const handleExpandedItemsChange = ( event: React.SyntheticEvent, itemIds: string[], ) => { setExpandedItems(itemIds); }; const handleExpandClick = () => { if (expandedItems.length === 0) { // 모든 노드를 펼치기 // 실제로는 mainNav와 additionalNav를 순회해 itemId를 전부 수집하는 방식 setExpandedItems([...collectAllIds()]); } else { setExpandedItems([]); } }; // (4) 수동으로 "모든 TreeItem의 itemId"를 수집하는 함수 const collectAllIds = React.useCallback(() => { const ids: string[] = []; // mainNav: 상위 = section.titleKey, 하위 = item.titleKey mainNav.forEach((section, sectionIndex) => { ids.push(`main-section-${sectionIndex}-${section.titleKey}`); // 상위 section.items.forEach((itm, itemIndex) => { const lastSegment = itm.href.split("/").pop() || itm.titleKey; ids.push(`main-item-${sectionIndex}-${itemIndex}-${lastSegment}`); }); }); // additionalNav items additionalNav.forEach((itm, index) => { const lastSegment = itm.href.split("/").pop() || itm.titleKey; ids.push(`additional-item-${index}-${lastSegment}`); }); return ids; }, []); function handleItemClick(key: SelectedKey) { // 1) Dialog 열기 setSelectedKey(key); // 이 값은 Dialog에서 어떤 메뉴인지 식별에 사용 setDialogOpen(true); } // (5) 실제 렌더 return (
{/* (A) mainNav를 트리로 렌더 */} {mainNav.map((section, sectionIndex) => ( {section.items.map((itm, itemIndex) => { const lastSegment = itm.href.split("/").pop() || itm.titleKey; const key = { key: lastSegment, title: t(itm.titleKey) } const uniqueItemId = `main-item-${sectionIndex}-${itemIndex}-${lastSegment}`; return ( handleItemClick(key)} /> ); })} ))} {additionalNav.map((itm, index) => { const lastSegment = itm.href.split("/").pop() || itm.titleKey; const key = { key: lastSegment, title: t(itm.titleKey) } const uniqueItemId = `additional-item-${index}-${lastSegment}`; return ( handleItemClick(key)} /> ); })}
); }