import { useState, useRef, useEffect } from 'react' import GridLayout from 'react-grid-layout' import 'react-grid-layout/css/styles.css' import { useDashboardConfig } from '../hooks' import { WidgetContainer } from '../components/widgets' import AddWidgetModal from '../components/AddWidgetModal' import EditWidgetModal from '../components/EditWidgetModal' export default function Dashboard() { const { config, addWidget, removeWidget, updateWidget, exportConfig, importConfig } = useDashboardConfig() const [isModalOpen, setIsModalOpen] = useState(false) const [editingWidget, setEditingWidget] = useState(null) const [gridWidth, setGridWidth] = useState(1200) const gridContainerRef = useRef(null) // Update grid width on resize useEffect(() => { const updateWidth = () => { if (gridContainerRef.current) { setGridWidth(gridContainerRef.current.offsetWidth) } } updateWidth() window.addEventListener('resize', updateWidth) return () => window.removeEventListener('resize', updateWidth) }, []) const handleLayoutChange = (newLayout: GridLayout.Layout[]) => { // Update widget positions when layout changes newLayout.forEach((item) => { const widget = config.widgets.find((w) => w.id === item.i) if (widget) { updateWidget(item.i, { position: { x: item.x, y: item.y, w: item.w, h: item.h, }, }) } }) } const layout = config.widgets.map((widget) => ({ i: widget.id, x: widget.position?.x || 0, y: widget.position?.y || 0, w: widget.position?.w || 1, h: widget.position?.h || 2, minW: 1, minH: 1, maxW: 4, })) const handleExport = () => { const json = exportConfig() const blob = new Blob([json], { type: 'application/json' }) const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `dashboard-config-${new Date().toISOString().split('T')[0]}.json` a.click() URL.revokeObjectURL(url) } const handleImport = (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (!file) return const reader = new FileReader() reader.onload = (event) => { try { const text = event.target?.result as string const parsed = JSON.parse(text) importConfig(parsed) alert('Dashboard configuration imported successfully!') } catch (error) { alert('Failed to import configuration') console.error(error) } } reader.readAsText(file) } return (

Dashboard

Customize your view with modular widgets

{config.widgets.length === 0 ? (

No Widgets Yet

Get started by adding your first widget. Choose from line charts, stat cards, gauges, or AI insights.

) : (
{config.widgets.map((widget) => (
removeWidget(widget.id)} onEdit={() => setEditingWidget(widget.id)} />
))}
)} setIsModalOpen(false)} onAdd={(widget) => { addWidget(widget) setIsModalOpen(false) }} /> w.id === editingWidget) || null} onClose={() => setEditingWidget(null)} onSave={(widgetId, updates) => { updateWidget(widgetId, updates) setEditingWidget(null) }} />
) }