mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-27 13:49:10 +02:00
update impact metrics state
This commit is contained in:
parent
69905185c5
commit
a9a7bc372d
@ -9,6 +9,8 @@ import { ChartItem } from './ChartItem.tsx';
|
|||||||
import { GridLayoutWrapper, type GridItem } from './GridLayoutWrapper.tsx';
|
import { GridLayoutWrapper, type GridItem } from './GridLayoutWrapper.tsx';
|
||||||
import { useImpactMetricsState } from './hooks/useImpactMetricsState.ts';
|
import { useImpactMetricsState } from './hooks/useImpactMetricsState.ts';
|
||||||
import type { ChartConfig, LayoutItem } from './types.ts';
|
import type { ChartConfig, LayoutItem } from './types.ts';
|
||||||
|
import useToast from 'hooks/useToast';
|
||||||
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
||||||
|
|
||||||
const StyledEmptyState = styled(Paper)(({ theme }) => ({
|
const StyledEmptyState = styled(Paper)(({ theme }) => ({
|
||||||
textAlign: 'center',
|
textAlign: 'center',
|
||||||
@ -21,9 +23,18 @@ const StyledEmptyState = styled(Paper)(({ theme }) => ({
|
|||||||
export const ImpactMetrics: FC = () => {
|
export const ImpactMetrics: FC = () => {
|
||||||
const [modalOpen, setModalOpen] = useState(false);
|
const [modalOpen, setModalOpen] = useState(false);
|
||||||
const [editingChart, setEditingChart] = useState<ChartConfig | undefined>();
|
const [editingChart, setEditingChart] = useState<ChartConfig | undefined>();
|
||||||
|
const { setToastApiError } = useToast();
|
||||||
|
|
||||||
const { charts, layout, addChart, updateChart, deleteChart, updateLayout } =
|
const {
|
||||||
useImpactMetricsState();
|
charts,
|
||||||
|
layout,
|
||||||
|
loading: settingsLoading,
|
||||||
|
error: settingsError,
|
||||||
|
addChart,
|
||||||
|
updateChart,
|
||||||
|
deleteChart,
|
||||||
|
updateLayout,
|
||||||
|
} = useImpactMetricsState();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
metadata,
|
metadata,
|
||||||
@ -51,20 +62,39 @@ export const ImpactMetrics: FC = () => {
|
|||||||
setModalOpen(true);
|
setModalOpen(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSaveChart = (config: Omit<ChartConfig, 'id'>) => {
|
const handleSaveChart = async (config: Omit<ChartConfig, 'id'>) => {
|
||||||
if (editingChart) {
|
try {
|
||||||
updateChart(editingChart.id, config);
|
if (editingChart) {
|
||||||
} else {
|
await updateChart(editingChart.id, config);
|
||||||
addChart(config);
|
} else {
|
||||||
|
await addChart(config);
|
||||||
|
}
|
||||||
|
setModalOpen(false);
|
||||||
|
} catch (error) {
|
||||||
|
setToastApiError(formatUnknownError(error));
|
||||||
}
|
}
|
||||||
setModalOpen(false);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLayoutChange = useCallback(
|
const handleLayoutChange = useCallback(
|
||||||
(layout: any[]) => {
|
async (layout: any[]) => {
|
||||||
updateLayout(layout as LayoutItem[]);
|
try {
|
||||||
|
await updateLayout(layout as LayoutItem[]);
|
||||||
|
} catch (error) {
|
||||||
|
setToastApiError(formatUnknownError(error));
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[updateLayout],
|
[updateLayout, setToastApiError],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleDeleteChart = useCallback(
|
||||||
|
async (id: string) => {
|
||||||
|
try {
|
||||||
|
await deleteChart(id);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to delete chart:', error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[deleteChart],
|
||||||
);
|
);
|
||||||
|
|
||||||
const gridItems: GridItem[] = useMemo(
|
const gridItems: GridItem[] = useMemo(
|
||||||
@ -79,7 +109,7 @@ export const ImpactMetrics: FC = () => {
|
|||||||
<ChartItem
|
<ChartItem
|
||||||
config={config}
|
config={config}
|
||||||
onEdit={handleEditChart}
|
onEdit={handleEditChart}
|
||||||
onDelete={deleteChart}
|
onDelete={handleDeleteChart}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
w: existingLayout?.w ?? 6,
|
w: existingLayout?.w ?? 6,
|
||||||
@ -92,10 +122,11 @@ export const ImpactMetrics: FC = () => {
|
|||||||
maxH: 8,
|
maxH: 8,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
[charts, layout, handleEditChart, deleteChart],
|
[charts, layout, handleEditChart, handleDeleteChart],
|
||||||
);
|
);
|
||||||
|
|
||||||
const hasError = metadataError;
|
const hasError = metadataError || settingsError;
|
||||||
|
const isLoading = metadataLoading || settingsLoading;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -111,14 +142,14 @@ export const ImpactMetrics: FC = () => {
|
|||||||
variant='contained'
|
variant='contained'
|
||||||
startIcon={<Add />}
|
startIcon={<Add />}
|
||||||
onClick={handleAddChart}
|
onClick={handleAddChart}
|
||||||
disabled={metadataLoading || !!hasError}
|
disabled={isLoading || !!hasError}
|
||||||
>
|
>
|
||||||
Add Chart
|
Add Chart
|
||||||
</Button>
|
</Button>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{charts.length === 0 && !metadataLoading && !hasError ? (
|
{charts.length === 0 && !isLoading && !hasError ? (
|
||||||
<StyledEmptyState>
|
<StyledEmptyState>
|
||||||
<Typography variant='h6' gutterBottom>
|
<Typography variant='h6' gutterBottom>
|
||||||
No charts configured
|
No charts configured
|
||||||
@ -135,7 +166,7 @@ export const ImpactMetrics: FC = () => {
|
|||||||
variant='contained'
|
variant='contained'
|
||||||
startIcon={<Add />}
|
startIcon={<Add />}
|
||||||
onClick={handleAddChart}
|
onClick={handleAddChart}
|
||||||
disabled={metadataLoading || !!hasError}
|
disabled={isLoading || !!hasError}
|
||||||
>
|
>
|
||||||
Add Chart
|
Add Chart
|
||||||
</Button>
|
</Button>
|
||||||
@ -153,7 +184,7 @@ export const ImpactMetrics: FC = () => {
|
|||||||
onSave={handleSaveChart}
|
onSave={handleSaveChart}
|
||||||
initialConfig={editingChart}
|
initialConfig={editingChart}
|
||||||
metricSeries={metricSeries}
|
metricSeries={metricSeries}
|
||||||
loading={metadataLoading}
|
loading={metadataLoading || settingsLoading}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
@ -184,7 +184,11 @@ export const ImpactMetricsChart: FC<ImpactMetricsChartProps> = ({
|
|||||||
background: theme.palette.background.elevation1,
|
background: theme.palette.background.elevation1,
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<Typography variant='caption' color='text.secondary'>
|
<Typography
|
||||||
|
variant='caption'
|
||||||
|
color='text.secondary'
|
||||||
|
sx={{ textWrap: 'break-all' }}
|
||||||
|
>
|
||||||
<code>{debug.query}</code>
|
<code>{debug.query}</code>
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
|
@ -1,72 +1,40 @@
|
|||||||
import { useCallback, useMemo } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { withDefault } from 'use-query-params';
|
import { useImpactMetricsSettings } from 'hooks/api/getters/useImpactMetricsSettings/useImpactMetricsSettings.js';
|
||||||
import { usePersistentTableState } from 'hooks/usePersistentTableState';
|
import {useImpactMetricsSettingsApi} from 'hooks/api/actions/useImpactMetricsSettingsApi/useImpactMetricsSettingsApi.js';
|
||||||
import type { ChartConfig, ImpactMetricsState, LayoutItem } from '../types.ts';
|
import type { ChartConfig, ImpactMetricsState, LayoutItem } from '../types.ts';
|
||||||
|
|
||||||
const createArrayParam = <T>() => ({
|
|
||||||
encode: (items: T[]): string =>
|
|
||||||
items.length > 0 ? btoa(JSON.stringify(items)) : '',
|
|
||||||
|
|
||||||
decode: (value: string | (string | null)[] | null | undefined): T[] => {
|
|
||||||
if (typeof value !== 'string' || !value) return [];
|
|
||||||
try {
|
|
||||||
return JSON.parse(atob(value));
|
|
||||||
} catch {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const ChartsParam = createArrayParam<ChartConfig>();
|
|
||||||
const LayoutParam = createArrayParam<LayoutItem>();
|
|
||||||
|
|
||||||
export const useImpactMetricsState = () => {
|
export const useImpactMetricsState = () => {
|
||||||
const stateConfig = {
|
const {
|
||||||
charts: withDefault(ChartsParam, []),
|
settings,
|
||||||
layout: withDefault(LayoutParam, []),
|
loading: settingsLoading,
|
||||||
};
|
error: settingsError,
|
||||||
|
refetch,
|
||||||
|
} = useImpactMetricsSettings();
|
||||||
|
|
||||||
const [tableState, setTableState] = usePersistentTableState(
|
const {
|
||||||
'impact-metrics-state',
|
updateSettings,
|
||||||
stateConfig,
|
loading: actionLoading,
|
||||||
);
|
errors: actionErrors,
|
||||||
|
} = useImpactMetricsSettingsApi();
|
||||||
const currentState: ImpactMetricsState = useMemo(
|
|
||||||
() => ({
|
|
||||||
charts: tableState.charts || [],
|
|
||||||
layout: tableState.layout || [],
|
|
||||||
}),
|
|
||||||
[tableState.charts, tableState.layout],
|
|
||||||
);
|
|
||||||
|
|
||||||
const updateState = useCallback(
|
|
||||||
(newState: ImpactMetricsState) => {
|
|
||||||
setTableState({
|
|
||||||
charts: newState.charts,
|
|
||||||
layout: newState.layout,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
[setTableState],
|
|
||||||
);
|
|
||||||
|
|
||||||
const addChart = useCallback(
|
const addChart = useCallback(
|
||||||
(config: Omit<ChartConfig, 'id'>) => {
|
async (config: Omit<ChartConfig, 'id'>) => {
|
||||||
const newChart: ChartConfig = {
|
const newChart: ChartConfig = {
|
||||||
...config,
|
...config,
|
||||||
id: `chart-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
|
id: `chart-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
const maxY =
|
const maxY =
|
||||||
currentState.layout.length > 0
|
settings.layout.length > 0
|
||||||
? Math.max(
|
? Math.max(
|
||||||
...currentState.layout.map((item) => item.y + item.h),
|
...settings.layout.map((item) => item.y + item.h),
|
||||||
)
|
)
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
updateState({
|
const newState: ImpactMetricsState = {
|
||||||
charts: [...currentState.charts, newChart],
|
charts: [...settings.charts, newChart],
|
||||||
layout: [
|
layout: [
|
||||||
...currentState.layout,
|
...settings.layout,
|
||||||
{
|
{
|
||||||
i: newChart.id,
|
i: newChart.id,
|
||||||
x: 0,
|
x: 0,
|
||||||
@ -79,46 +47,58 @@ export const useImpactMetricsState = () => {
|
|||||||
maxH: 8,
|
maxH: 8,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
};
|
||||||
|
|
||||||
|
await updateSettings(newState);
|
||||||
|
refetch();
|
||||||
},
|
},
|
||||||
[currentState.charts, currentState.layout, updateState],
|
[settings, updateSettings, refetch],
|
||||||
);
|
);
|
||||||
|
|
||||||
const updateChart = useCallback(
|
const updateChart = useCallback(
|
||||||
(id: string, updates: Partial<ChartConfig>) => {
|
async (id: string, updates: Partial<ChartConfig>) => {
|
||||||
updateState({
|
const updatedCharts = settings.charts.map((chart) =>
|
||||||
charts: currentState.charts.map((chart) =>
|
chart.id === id ? { ...chart, ...updates } : chart,
|
||||||
chart.id === id ? { ...chart, ...updates } : chart,
|
);
|
||||||
),
|
const newState: ImpactMetricsState = {
|
||||||
layout: currentState.layout,
|
charts: updatedCharts,
|
||||||
});
|
layout: settings.layout,
|
||||||
|
};
|
||||||
|
await updateSettings(newState);
|
||||||
|
refetch();
|
||||||
},
|
},
|
||||||
[currentState.charts, currentState.layout, updateState],
|
[settings, updateSettings, refetch],
|
||||||
);
|
);
|
||||||
|
|
||||||
const deleteChart = useCallback(
|
const deleteChart = useCallback(
|
||||||
(id: string) => {
|
async (id: string) => {
|
||||||
updateState({
|
const newState: ImpactMetricsState = {
|
||||||
charts: currentState.charts.filter((chart) => chart.id !== id),
|
charts: settings.charts.filter((chart) => chart.id !== id),
|
||||||
layout: currentState.layout.filter((item) => item.i !== id),
|
layout: settings.layout.filter((item) => item.i !== id),
|
||||||
});
|
};
|
||||||
|
await updateSettings(newState);
|
||||||
|
refetch();
|
||||||
},
|
},
|
||||||
[currentState.charts, currentState.layout, updateState],
|
[settings, updateSettings, refetch],
|
||||||
);
|
);
|
||||||
|
|
||||||
const updateLayout = useCallback(
|
const updateLayout = useCallback(
|
||||||
(newLayout: LayoutItem[]) => {
|
async (newLayout: LayoutItem[]) => {
|
||||||
updateState({
|
const newState: ImpactMetricsState = {
|
||||||
charts: currentState.charts,
|
charts: settings.charts,
|
||||||
layout: newLayout,
|
layout: newLayout,
|
||||||
});
|
};
|
||||||
|
await updateSettings(newState);
|
||||||
|
refetch();
|
||||||
},
|
},
|
||||||
[currentState.charts, updateState],
|
[settings, updateSettings, refetch],
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
charts: currentState.charts || [],
|
charts: settings.charts || [],
|
||||||
layout: currentState.layout || [],
|
layout: settings.layout || [],
|
||||||
|
loading: settingsLoading || actionLoading,
|
||||||
|
error: settingsError || Object.keys(actionErrors).length > 0 ? actionErrors : undefined,
|
||||||
addChart,
|
addChart,
|
||||||
updateChart,
|
updateChart,
|
||||||
deleteChart,
|
deleteChart,
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
import { useCallback } from 'react';
|
||||||
|
import useAPI from '../useApi/useApi.js';
|
||||||
|
import type { ImpactMetricsState } from 'component/impact-metrics/types.ts';
|
||||||
|
|
||||||
|
export const useImpactMetricsSettingsApi = () => {
|
||||||
|
const { makeRequest, createRequest, errors, loading } = useAPI({
|
||||||
|
propagateErrors: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const updateSettings = useCallback(
|
||||||
|
async (settings: ImpactMetricsState) => {
|
||||||
|
const path = `api/admin/impact-metrics/settings`;
|
||||||
|
const req = createRequest(
|
||||||
|
path,
|
||||||
|
{
|
||||||
|
method: 'PUT',
|
||||||
|
body: JSON.stringify(settings)
|
||||||
|
},
|
||||||
|
'updateImpactMetricsSettings',
|
||||||
|
);
|
||||||
|
|
||||||
|
return makeRequest(req.caller, req.id);
|
||||||
|
},
|
||||||
|
[makeRequest, createRequest],
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
updateSettings,
|
||||||
|
errors,
|
||||||
|
loading,
|
||||||
|
};
|
||||||
|
};
|
@ -0,0 +1,18 @@
|
|||||||
|
import { fetcher, useApiGetter } from '../useApiGetter/useApiGetter.js';
|
||||||
|
import { formatApiPath } from 'utils/formatPath';
|
||||||
|
import type { ImpactMetricsState } from 'component/impact-metrics/types.ts';
|
||||||
|
|
||||||
|
export const useImpactMetricsSettings = () => {
|
||||||
|
const PATH = `api/admin/impact-metrics/settings`;
|
||||||
|
const { data, refetch, loading, error } =
|
||||||
|
useApiGetter<ImpactMetricsState>(formatApiPath(PATH), () =>
|
||||||
|
fetcher(formatApiPath(PATH), 'Impact metrics settings'),
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
settings: data || { charts: [], layout: [] },
|
||||||
|
refetch,
|
||||||
|
loading,
|
||||||
|
error,
|
||||||
|
};
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user