import { Responsive, WidthProvider } from 'react-grid-layout';
import 'react-grid-layout/css/styles.css';
import 'react-resizable/css/styles.css';
const ResponsiveGridLayout = WidthProvider(Responsive);
function DraggableDashboard() {
// Define layouts for different screen sizes
const layouts = {
lg: [
{ i: "sales-by-region", x: 0, y: 0, w: 6, h: 2 },
{ i: "revenue-trend", x: 6, y: 0, w: 6, h: 2 },
{ i: "top-products", x: 0, y: 2, w: 4, h: 2 },
{ i: "customer-segments", x: 4, y: 2, w: 4, h: 2 },
{ i: "sales-funnel", x: 8, y: 2, w: 4, h: 2 }
],
md: [
{ i: "sales-by-region", x: 0, y: 0, w: 6, h: 2 },
{ i: "revenue-trend", x: 6, y: 0, w: 6, h: 2 },
{ i: "top-products", x: 0, y: 2, w: 4, h: 2 },
{ i: "customer-segments", x: 4, y: 2, w: 4, h: 2 },
{ i: "sales-funnel", x: 0, y: 4, w: 8, h: 2 }
],
sm: [
{ i: "sales-by-region", x: 0, y: 0, w: 6, h: 2 },
{ i: "revenue-trend", x: 0, y: 2, w: 6, h: 2 },
{ i: "top-products", x: 0, y: 4, w: 3, h: 2 },
{ i: "customer-segments", x: 3, y: 4, w: 3, h: 2 },
{ i: "sales-funnel", x: 0, y: 6, w: 6, h: 2 }
]
};
const [currentLayouts, setCurrentLayouts] = useState(layouts);
// Save layout changes
const handleLayoutChange = (layout, layouts) => {
setCurrentLayouts(layouts);
// You might want to save this to localStorage or your backend
localStorage.setItem('dashboard-layouts', JSON.stringify(layouts));
};
// Render charts based on their IDs
const renderChart = (id) => {
switch (id) {
case 'sales-by-region':
return <SalesByRegionChart />;
case 'revenue-trend':
return <RevenueTrendChart />;
case 'top-products':
return <TopProductsChart />;
case 'customer-segments':
return <CustomerSegmentsChart />;
case 'sales-funnel':
return <SalesFunnelChart />;
default:
return <div>Unknown chart: {id}</div>;
}
};
return (
<div className="draggable-dashboard">
<h1>Sales Performance Dashboard</h1>
<p className="dashboard-description">
Drag and resize dashboard components to customize your view
</p>
<ResponsiveGridLayout
className="layout"
layouts={currentLayouts}
breakpoints={{ lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 }}
cols={{ lg: 12, md: 12, sm: 6, xs: 4, xxs: 2 }}
rowHeight={200}
onLayoutChange={handleLayoutChange}
isDraggable
isResizable
>
<div key="sales-by-region" className="dashboard-item">
{renderChart('sales-by-region')}
</div>
<div key="revenue-trend" className="dashboard-item">
{renderChart('revenue-trend')}
</div>
<div key="top-products" className="dashboard-item">
{renderChart('top-products')}
</div>
<div key="customer-segments" className="dashboard-item">
{renderChart('customer-segments')}
</div>
<div key="sales-funnel" className="dashboard-item">
{renderChart('sales-funnel')}
</div>
</ResponsiveGridLayout>
</div>
);
}