194 lines
6.8 KiB
TypeScript
194 lines
6.8 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import { usePathname } from "next/navigation";
|
|
|
|
import {
|
|
getTabStoreState as getGridTabStoreState,
|
|
useTabStore as gridTabStore,
|
|
} from "@rsone/components";
|
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import { ENDPOINT } from "@/shared/config";
|
|
import { getCookie, useAlertStore, useMenuStore } from "@/shared/lib";
|
|
import { isVerificationSelectionReportMenu } from "@/shared/lib/verification-selection-report";
|
|
import { StatisticsPageLayout } from "@/shared/ui/statistics-page-layout";
|
|
import { tokenManager } from "@/entities/auth";
|
|
import type { MenuItem } from "@/entities/menu";
|
|
import { MenuApi } from "@/entities/menu";
|
|
import { BaseLayout } from "@common/components";
|
|
import { getConfig } from "@common/config";
|
|
|
|
import { getTabStoreState, useTabStore } from "@/store/tab-store";
|
|
|
|
import VerificationSelectionReport from "@/components/statistics/VerificationSelectionReport";
|
|
|
|
const appConfig = getConfig();
|
|
const userId = getCookie(appConfig.AUTH_USER_ID ?? "");
|
|
const appSiteId = getCookie("site-id-key") ?? "";
|
|
const isLoggedIn = !!userId;
|
|
|
|
export default function WithBaseLayout({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode;
|
|
}) {
|
|
const pathname = usePathname();
|
|
const queryClient = useQueryClient();
|
|
|
|
/** 탭 추가 없이 메뉴만 눌렀을 때 패널 유지 (다른 메뉴 클릭해도 유지) */
|
|
const [isVerificationSelectionReportPanelOpen, setReportPanelOpen] =
|
|
useState(false);
|
|
|
|
const setCurrentMenuInfo = useMenuStore(
|
|
(state) => state.setCurrentMenuInfo,
|
|
);
|
|
const currentMenuInfo = useMenuStore((state) => state.currentMenuInfo);
|
|
|
|
const addTab = useTabStore((state) => state.addTab);
|
|
const setActiveTab = useTabStore((state) => state.setActiveTab);
|
|
const setMenuList = useTabStore((state) => state.setMenuList);
|
|
const activeTabId = useTabStore((state) => state.activeTabId);
|
|
const menuList = useTabStore((state) => state.menuList);
|
|
const tabs = useTabStore((state) => state.tabs);
|
|
const addTabForGrid = gridTabStore((state) => state.addTab);
|
|
const setActiveTabForGrid = gridTabStore((state) => state.setActiveTab);
|
|
const setMenuListForGrid = gridTabStore((state) => state.setMenuList);
|
|
const alert = useAlertStore((state) => state.setMessage);
|
|
|
|
const { data: menuListData } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.MENU_ROLES],
|
|
queryFn: () => MenuApi.getMenuRoles(appSiteId),
|
|
enabled: isLoggedIn,
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (menuListData) {
|
|
setMenuList(menuListData);
|
|
setMenuListForGrid(menuListData);
|
|
}
|
|
}, [menuListData, setMenuList, setMenuListForGrid]);
|
|
|
|
// 새로고침 후 persist로 앱 탭만 복원된 경우 rsone 그리드 스토어에 탭을 맞춤
|
|
useEffect(() => {
|
|
if (!menuList) return;
|
|
const appTabs = getTabStoreState().tabs;
|
|
if (appTabs.length === 0) return;
|
|
if (getGridTabStoreState().tabs.length > 0) return;
|
|
|
|
for (const tab of appTabs) {
|
|
getGridTabStoreState().addTab(tab.menuItem);
|
|
}
|
|
|
|
const activeId = getTabStoreState().activeTabId;
|
|
if (activeId) {
|
|
const activeTab = appTabs.find((t) => t.id === activeId);
|
|
const gridActiveId = activeTab?.menuItem.menuEngNm;
|
|
if (gridActiveId) {
|
|
getGridTabStoreState().setActiveTab(gridActiveId);
|
|
}
|
|
}
|
|
}, [menuList, tabs]);
|
|
|
|
useEffect(() => {
|
|
if (!pathname) return;
|
|
|
|
// pathname이 변경될 때만 실행되도록 tabs를 직접 가져옴
|
|
const currentTabs = getTabStoreState().tabs;
|
|
const tab = currentTabs.find((t) => t.path === pathname);
|
|
if (tab) {
|
|
setActiveTab(tab.id);
|
|
const gridActiveId = tab.menuItem.menuEngNm;
|
|
if (gridActiveId) {
|
|
setActiveTabForGrid(gridActiveId);
|
|
}
|
|
}
|
|
}, [pathname, setActiveTab, setActiveTabForGrid]);
|
|
|
|
// 활성 탭의 메뉴를 찾아서 urlPath를 가져옴. 탭이 전부 닫히면 ''를 넘겨 LNB에서 메뉴 강조 제거
|
|
const currentPath = useMemo(() => {
|
|
if (tabs.length === 0) return "";
|
|
|
|
if (activeTabId && menuList) {
|
|
// menuList에서 활성 탭의 메뉴 찾기
|
|
const findMenuByEngNm = (items: MenuItem[]): MenuItem | null => {
|
|
for (const item of items) {
|
|
if (item.urlPath === activeTabId) {
|
|
return item;
|
|
}
|
|
if (item.children) {
|
|
const found = findMenuByEngNm(item.children);
|
|
if (found) return found;
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
|
|
const activeMenu = findMenuByEngNm(menuList);
|
|
|
|
if (activeMenu?.urlPath) {
|
|
return activeMenu.urlPath;
|
|
}
|
|
}
|
|
|
|
// 탭에서 직접 찾기
|
|
if (activeTabId) {
|
|
const currentTabs = getTabStoreState().tabs;
|
|
const activeTab = currentTabs.find((t) => t.id === activeTabId);
|
|
if (activeTab?.menuItem?.urlPath) {
|
|
return activeTab.menuItem.urlPath;
|
|
}
|
|
}
|
|
|
|
// currentMenuInfo에서 가져오기
|
|
if (currentMenuInfo?.urlPath) {
|
|
return currentMenuInfo.urlPath;
|
|
}
|
|
|
|
// 기본값으로 pathname 사용
|
|
return pathname ?? "";
|
|
}, [tabs.length, activeTabId, menuList, currentMenuInfo, pathname]);
|
|
|
|
const handleMenuClick = useCallback(
|
|
(item: MenuItem) => {
|
|
setCurrentMenuInfo(item);
|
|
|
|
if (isVerificationSelectionReportMenu(item)) {
|
|
setReportPanelOpen(true);
|
|
return;
|
|
}
|
|
|
|
const addTabResult: boolean = addTab(item);
|
|
|
|
// packages/rsone의 스토어에도 동기화
|
|
if (addTabResult) {
|
|
addTabForGrid(item);
|
|
}
|
|
|
|
if (!addTabResult) {
|
|
alert(`최대 추가 할 수 있는 화면 수(10개)를 \n초과 하였습니다.
|
|
기존 화면을 닫은 후 이용해주세요.`);
|
|
}
|
|
},
|
|
[setCurrentMenuInfo, addTab, addTabForGrid, alert, setReportPanelOpen],
|
|
);
|
|
|
|
return (
|
|
<BaseLayout
|
|
queryClient={queryClient}
|
|
tokenManager={tokenManager}
|
|
currentPath={currentPath}
|
|
handleMenuClick={handleMenuClick}
|
|
>
|
|
<div className="flex flex-1 h-full">
|
|
{isVerificationSelectionReportPanelOpen && (
|
|
<VerificationSelectionReport
|
|
onClose={() => setReportPanelOpen(false)}
|
|
/>
|
|
)}
|
|
<StatisticsPageLayout>{children}</StatisticsPageLayout>
|
|
</div>
|
|
</BaseLayout>
|
|
);
|
|
}
|