Files
frontend/apps/st/src/pages/statistics/ui/statistics-page.tsx
T
rsone001 d9d5b49057 st: refactor:
- Real Grid 컴포넌트 @rsone/components 로 모듈화
- 컴포넌트 동적 import 경로 수정(engName -> urlPath)
2026-01-09 10:21:00 +09:00

77 lines
2.6 KiB
TypeScript

"use client";
import { Suspense, useRef, useEffect } from "react";
import dynamic from "next/dynamic";
import { useTabStore } from "@/store/tab-store";
import Dashboard from "@/components/Dashboard";
import { SkeletonPage } from "@/shared/ui/skeleton-page";
import "@rsone/components/index.css";
const StatisticsPage = () => {
const { tabs, activeTabId } = useTabStore();
// 컴포넌트 캐시를 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]);
// 탭이 없으면 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}/${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;