86 lines
2.8 KiB
TypeScript
86 lines
2.8 KiB
TypeScript
"use client";
|
|
|
|
import { Suspense, useEffect, useRef } from "react";
|
|
import dynamic from "next/dynamic";
|
|
|
|
import { SkeletonPage } from "@/shared/ui/skeleton-page";
|
|
|
|
import "@rsone/components/index.css";
|
|
|
|
import Dashboard from "@/components/statistics/Dashboard";
|
|
import { useAdminStore } from "@/store/admin-store";
|
|
import { useTabStore } from "@/store/tab-store";
|
|
|
|
const StatisticsPage = () => {
|
|
const { tabs, activeTabId } = useTabStore();
|
|
const fetchAdminInfo = useAdminStore((state) => state.fetchAdminInfo);
|
|
|
|
// 컴포넌트 캐시를 useRef로 유지 (재렌더링 시에도 유지됨)
|
|
const componentCache = useRef(new Map<string, React.ComponentType>());
|
|
|
|
// 닫힌 탭의 컴포넌트는 캐시에서 제거 (메모리 정리)
|
|
useEffect(() => {
|
|
const currentTabIds = new Set(tabs.map((tab) => tab.id));
|
|
const cachedTabIds = Array.from(componentCache.current.keys());
|
|
|
|
cachedTabIds.forEach((cachedId) => {
|
|
if (!currentTabIds.has(cachedId)) {
|
|
componentCache.current.delete(cachedId);
|
|
}
|
|
});
|
|
}, [tabs]);
|
|
|
|
useEffect(() => {
|
|
fetchAdminInfo();
|
|
}, [fetchAdminInfo]);
|
|
|
|
// 탭이 없으면 Dashboard 표시
|
|
if (tabs.length === 0) {
|
|
return (
|
|
<div className="card card-border !mt-6">
|
|
<Dashboard />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="card card-border !mt-6">
|
|
{tabs.map((tab) => {
|
|
// 렌더링 시점에 컴포넌트가 없으면 바로 로드
|
|
if (!componentCache.current.has(tab.id)) {
|
|
const componentName = tab.urlPath;
|
|
const parentComponentName = tab.parentEngNm;
|
|
|
|
const DynamicComponent = dynamic(
|
|
() =>
|
|
import(
|
|
`@/components/statistics${parentComponentName ? `/${parentComponentName}` : ""}/${componentName}`
|
|
),
|
|
{ ssr: false, loading: () => <SkeletonPage /> },
|
|
);
|
|
|
|
componentCache.current.set(tab.id, DynamicComponent);
|
|
}
|
|
|
|
const Component = componentCache.current.get(tab.id);
|
|
if (!Component) return null;
|
|
|
|
const isActive = tab.id === activeTabId;
|
|
|
|
return (
|
|
<div
|
|
key={tab.id}
|
|
style={{ display: isActive ? "block" : "none" }}
|
|
>
|
|
<Suspense fallback={<SkeletonPage />}>
|
|
<Component />
|
|
</Suspense>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default StatisticsPage;
|