Skip to content

Commit 9061f8d

Browse files
committed
StatsHouse UI: fix style
remove data context
1 parent b02dd01 commit 9061f8d

File tree

11 files changed

+53
-100
lines changed

11 files changed

+53
-100
lines changed

statshouse-ui/src/components2/Plot/PlotControl/PlotControlEventOverlay.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { memo, useCallback, useMemo } from 'react';
88
import { Select, type SelectOptionProps } from '@/components/Select';
99
import cn from 'classnames';
10-
import { isNotNil, parseURLSearchParams } from '@/common/helpers';
10+
import { emptyArray, isNotNil, parseURLSearchParams } from '@/common/helpers';
1111
import { produce } from 'immer';
1212
import { dequal } from 'dequal/lite';
1313
import { PLOT_TYPE } from '@/api/enum';
@@ -16,9 +16,10 @@ import { globalSettings } from '@/common/settings';
1616
import { arrToObj, type PlotKey, type PlotParams, toPlotKey, toTreeObj, urlDecode } from '@/url2';
1717
import { addPlot, getMetricFullName } from '@/store2/helpers';
1818
import { useWidgetPlotContext } from '@/contexts/useWidgetPlotContext';
19-
import { useWidgetPlotDataContext } from '@/contexts/useWidgetPlotDataContext';
2019
import { StatsHouseStore, useStatsHouse } from '@/store2';
2120
import { setParams } from '@/store2/methods';
21+
import { usePlotsDataStore } from '@/store2/plotDataStore';
22+
import { useShallow } from 'zustand/react/shallow';
2223

2324
const eventPreset: (SelectOptionProps & { plot: PlotParams })[] = globalSettings.event_preset
2425
.map((url) => {
@@ -47,7 +48,14 @@ export const PlotControlEventOverlay = memo(function PlotControlEventOverlay({
4748
const {
4849
plot: { id, events },
4950
} = useWidgetPlotContext();
50-
const { plotData } = useWidgetPlotDataContext();
51+
const { metricName, whats } = usePlotsDataStore(
52+
useShallow(
53+
useCallback(
54+
({ plotsData }) => ({ metricName: plotsData[id]?.metricName ?? '', whats: plotsData[id]?.whats ?? emptyArray }),
55+
[id]
56+
)
57+
)
58+
);
5159

5260
const onChange = useCallback(
5361
(value: string | string[] = []) => {
@@ -96,7 +104,7 @@ export const PlotControlEventOverlay = memo(function PlotControlEventOverlay({
96104
return false;
97105
});
98106
const eventPlots: SelectOptionProps[] = plotsArr.map((p) => {
99-
const name = getMetricFullName(p, plotData);
107+
const name = getMetricFullName(p, { metricName, whats });
100108
return {
101109
value: p.id,
102110
name,
@@ -107,7 +115,7 @@ export const PlotControlEventOverlay = memo(function PlotControlEventOverlay({
107115
}
108116
eventPlots.unshift(...eventPresetFilter);
109117
return eventPlots;
110-
}, [plotData, plots]);
118+
}, [metricName, plots, whats]);
111119

112120
if (!list.length) {
113121
return null;

statshouse-ui/src/components2/Plot/PlotControl/PlotControlPromQLSwitch.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import { getHomePlot, promQLMetric } from '@/url2';
1616
import { useWidgetPlotContext } from '@/contexts/useWidgetPlotContext';
1717
import { useMetricMeta } from '@/hooks/useMetricMeta';
1818
import { useMetricName } from '@/hooks/useMetricName';
19-
import { useWidgetPlotDataContext } from '@/contexts/useWidgetPlotDataContext';
19+
import { usePlotsDataStore } from '@/store2/plotDataStore';
20+
import { emptyArray } from '@/common/helpers';
21+
import { useShallow } from 'zustand/react/shallow';
2022

2123
export type PlotControlPromQLSwitchProps = {
2224
className?: string;
@@ -26,9 +28,18 @@ export const PlotControlPromQLSwitch = memo(function PlotControlPromQLSwitch({
2628
className,
2729
}: PlotControlPromQLSwitchProps) {
2830
const { plot, setPlot } = useWidgetPlotContext();
29-
const {
30-
plotData: { metricName, promQL, whats },
31-
} = useWidgetPlotDataContext();
31+
const { metricName, promQL, whats } = usePlotsDataStore(
32+
useShallow(
33+
useCallback(
34+
({ plotsData }) => ({
35+
metricName: plotsData[plot.id]?.metricName ?? '',
36+
promQL: plotsData[plot.id]?.promQL ?? '',
37+
whats: plotsData[plot.id]?.whats ?? emptyArray,
38+
}),
39+
[plot.id]
40+
)
41+
)
42+
);
3243
const isPlotPromQL = isPromQL(plot);
3344
const meta = useMetricMeta(useMetricName(true));
3445

statshouse-ui/src/components2/Plot/PlotLegend/PlotLegendMaxHost.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import { ReactComponent as SVGCopy } from 'bootstrap-icons/icons/copy.svg';
1111
import { debug } from '@/common/debug';
1212
import { Button } from '@/components/UI';
1313
import type { PlotKey } from '@/url2';
14-
import { useWidgetPlotDataContext } from '@/contexts/useWidgetPlotDataContext';
14+
import { usePlotsDataStore } from '@/store2/plotDataStore';
15+
import { useWidgetPlotContext } from '@/contexts/useWidgetPlotContext';
16+
import { emptyArray } from '@/common/helpers';
1517

1618
type PlotLegendMaxHostProps = {
1719
value: string;
@@ -30,8 +32,12 @@ function copyItem(value?: string | string[]) {
3032

3133
export const PlotLegendMaxHost = memo(function PlotLegendMaxHost({ value, placeholder, idx }: PlotLegendMaxHostProps) {
3234
const {
33-
plotData: { maxHostLists },
34-
} = useWidgetPlotDataContext();
35+
plot: { id },
36+
} = useWidgetPlotContext();
37+
38+
const maxHostLists = usePlotsDataStore(
39+
useCallback(({ plotsData }) => plotsData[id]?.maxHostLists ?? emptyArray, [id])
40+
);
3541
const onCopyList = useCallback(() => {
3642
const list: string = maxHostLists[idx - 1]?.map(({ name }) => name).join('\r\n') ?? '';
3743
window.navigator.clipboard.writeText(list).then(() => {

statshouse-ui/src/components2/Plot/PlotView/PlotEvents.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import { formatByMetricType } from '@/common/formatByMetricType';
2424
import { emptyArray, isNotNil } from '@/common/helpers';
2525
import { freeKeyPrefix } from '@/url2';
2626
import { SeriesMetaTag } from '@/api/query';
27-
import { useWidgetPlotDataContext } from '@/contexts/useWidgetPlotDataContext';
2827
import { StatsHouseStore, useStatsHouseShallow } from '@/store2';
28+
import { usePlotsDataStore } from '@/store2/plotDataStore';
2929

3030
export type PlotEventsProps = {
3131
className?: string;
@@ -60,9 +60,10 @@ const selectorStore = ({ params: { timeRange, variables } }: StatsHouseStore) =>
6060

6161
export function PlotEvents({ className, onCursor, cursor, setTimeRange }: PlotEventsProps) {
6262
const { plot } = useWidgetPlotContext();
63-
const {
64-
plotData: { metricUnit },
65-
} = useWidgetPlotDataContext();
63+
64+
const metricUnit = usePlotsDataStore(
65+
useCallback(({ plotsData }) => plotsData[plot.id]?.metricUnit ?? METRIC_TYPE.none, [plot.id])
66+
);
6667

6768
const { timeRange, variables } = useStatsHouseShallow(selectorStore);
6869

statshouse-ui/src/components2/Plot/PlotView/PlotHealsStatus.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { useLiveModeStore } from '@/store2/liveModeStore';
1414
import { usePlotLoader } from '@/store2/plotQueryStore';
1515
import { useWidgetPlotContext } from '@/contexts/useWidgetPlotContext';
1616
import { refetchQuery } from '@/api/query';
17-
import { useWidgetPlotDataContext } from '@/contexts/useWidgetPlotDataContext';
1817
import { removePlotHeals } from '@/store2/methods';
18+
import { setPlotData, usePlotsDataStore } from '@/store2/plotDataStore';
1919

2020
export type PlotHealsStatusProps = {
2121
className?: string;
@@ -29,10 +29,8 @@ const selectorStore = ({ params: { timeRange, timeShifts, variables } }: StatsHo
2929

3030
export const PlotHealsStatus = memo(function PlotHealsStatus({ className }: PlotHealsStatusProps) {
3131
const { plot } = useWidgetPlotContext();
32-
const {
33-
plotData: { error },
34-
setPlotData,
35-
} = useWidgetPlotDataContext();
32+
const setPlotDataProduce = useMemo(() => setPlotData.bind(undefined, plot.id), [plot.id]);
33+
const error = usePlotsDataStore(useCallback(({ plotsData }) => plotsData[plot.id]?.error ?? '', [plot.id]));
3634
const { timeRange, timeShifts, variables } = useStatsHouseShallow(selectorStore);
3735
const id = plot.id;
3836
const interval = useLiveModeStore(({ interval }) => interval);
@@ -52,11 +50,11 @@ export const PlotHealsStatus = memo(function PlotHealsStatus({ className }: Plot
5250
return undefined;
5351
}, [interval, plotHealsTimeout]);
5452
const clearLastError = useCallback(() => {
55-
setPlotData((d) => {
53+
setPlotDataProduce((d) => {
5654
d.error = '';
5755
});
5856
removePlotHeals(id);
59-
}, [id, setPlotData]);
57+
}, [id, setPlotDataProduce]);
6058
const reload = useCallback(() => {
6159
refetchQuery(plot, timeRange, timeShifts, variables);
6260
clearLastError();

statshouse-ui/src/components2/PlotWidgets/EventWidget/EventWidget.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export function EventWidget({ className, isDashboard, isEmbed, fixRatio }: PlotW
359359
<PlotHealsStatus />
360360
</div>
361361
{/*header*/}
362-
<div className="d-flex flex-column flex-grow-1 overflow-force-wrap">
362+
<div className="d-flex flex-column flex-grow-1 w-0 overflow-force-wrap">
363363
<PlotHeader isDashboard={isDashboard} isEmbed={isEmbed} />
364364
{!compact && <PlotSubMenu />}
365365
</div>

statshouse-ui/src/contexts/WidgetPlotContextProvider.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import { defaultMetric, PlotKey } from '@/url2';
88
import { type ReactNode, useCallback, useMemo } from 'react';
99
import { WidgetPlotContext, WidgetPlotContextProps } from '@/contexts/WidgetPlotContext';
10-
import { WidgetPlotDataContextProvider } from '@/contexts/WidgetPlotDataContextProvider';
1110
import { useStatsHouse } from '@/store2';
1211
import { removePlot, setPlot } from '@/store2/methods';
1312

@@ -29,9 +28,5 @@ export function WidgetPlotContextProvider({ children, plotKey }: WidgetPlotConte
2928
}),
3029
[plot, removePlotMemo, setPlotMemo]
3130
);
32-
return (
33-
<WidgetPlotContext.Provider value={widgetContextValue}>
34-
<WidgetPlotDataContextProvider>{children}</WidgetPlotDataContextProvider>
35-
</WidgetPlotContext.Provider>
36-
);
31+
return <WidgetPlotContext.Provider value={widgetContextValue}>{children}</WidgetPlotContext.Provider>;
3732
}

statshouse-ui/src/contexts/WidgetPlotDataContext.tsx

Lines changed: 0 additions & 21 deletions
This file was deleted.

statshouse-ui/src/contexts/WidgetPlotDataContextProvider.tsx

Lines changed: 0 additions & 33 deletions
This file was deleted.

statshouse-ui/src/contexts/useWidgetPlotDataContext.tsx

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)