import type { FC } from 'react'; import { useMemo, useState } from 'react'; import { Box, Typography, Button } from '@mui/material'; import Add from '@mui/icons-material/Add'; import { PageHeader } from 'component/common/PageHeader/PageHeader.tsx'; import { useImpactMetricsMetadata } from 'hooks/api/getters/useImpactMetricsMetadata/useImpactMetricsMetadata'; import { ChartConfigModal } from './ChartConfigModal.tsx'; import { ChartItem } from './ChartItem.tsx'; import { useUrlState } from './hooks/useUrlState.ts'; import type { ChartConfig } from './types.ts'; export const ImpactMetrics: FC = () => { const [modalOpen, setModalOpen] = useState(false); const [editingChart, setEditingChart] = useState(); const { charts, addChart, updateChart, deleteChart } = useUrlState(); const { metadata, loading: metadataLoading, error: metadataError, } = useImpactMetricsMetadata(); const metricSeries = useMemo(() => { if (!metadata?.series) { return []; } return Object.entries(metadata.series).map(([name, rest]) => ({ name, ...rest, })); }, [metadata]); const handleAddChart = () => { setEditingChart(undefined); setModalOpen(true); }; const handleEditChart = (config: ChartConfig) => { setEditingChart(config); setModalOpen(true); }; const handleSaveChart = (config: Omit) => { if (editingChart) { updateChart(editingChart.id, config); } else { addChart(config); } setModalOpen(false); }; const hasError = metadataError; return ( <> Impact Metrics } actions={ charts.length > 0 ? ( ) : null } /> ({ display: 'flex', flexDirection: 'column', gap: theme.spacing(2), width: '100%', })} > {charts.length === 0 && !metadataLoading && !hasError ? ( ({ textAlign: 'center', py: theme.spacing(8), })} > No charts configured Add your first impact metrics chart to start tracking performance ) : ( charts.map((config) => ( )) )} setModalOpen(false)} onSave={handleSaveChart} initialConfig={editingChart} metricSeries={metricSeries} loading={metadataLoading} /> ); };