Files
sam-react-prod/src/components/ui/chart-wrapper.tsx

66 lines
1.5 KiB
TypeScript
Raw Normal View History

import { memo, useState, useEffect } from 'react';
import type { ReactNode } from 'react';
/**
*
* UI
*/
interface ChartWrapperProps {
children: ReactNode;
delay?: number;
height?: number;
}
export const ChartWrapper = memo(function ChartWrapper({
children,
delay = 100,
height = 300
}: ChartWrapperProps) {
const [showChart, setShowChart] = useState(false);
useEffect(() => {
const timer = setTimeout(() => setShowChart(true), delay);
return () => clearTimeout(timer);
}, [delay]);
if (!showChart) {
return (
<div
className="w-full animate-pulse bg-muted rounded-lg"
style={{ height: `${height}px` }}
>
<div className="h-full flex items-center justify-center">
<div className="text-muted-foreground text-sm"> ...</div>
</div>
</div>
);
}
return <>{children}</>;
});
/**
*
*/
interface OptimizedChartProps {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: any;
children: ReactNode;
height?: number;
}
export const OptimizedChart = memo(function OptimizedChart({
data: _data,
children,
height = 300
}: OptimizedChartProps) {
return (
<ChartWrapper height={height}>
{children}
</ChartWrapper>
);
}, (prevProps, nextProps) => {
// 데이터가 같으면 리렌더링 스킵
return prevProps.data === nextProps.data;
});