Skip to content

Commit cc0d6c1

Browse files
authored
[Metrics UI] Converting legend key to optional (#83495)
* [Metrics UI] Converting legend key to optional * Adding check and default to legend component
1 parent 69e3ceb commit cc0d6c1

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

x-pack/plugins/infra/public/pages/metrics/inventory_view/components/layout.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { PageContent } from '../../../../components/page';
1616
import { useSnapshot } from '../hooks/use_snaphot';
1717
import { useWaffleTimeContext } from '../hooks/use_waffle_time';
1818
import { useWaffleFiltersContext } from '../hooks/use_waffle_filters';
19-
import { useWaffleOptionsContext } from '../hooks/use_waffle_options';
19+
import { DEFAULT_LEGEND, useWaffleOptionsContext } from '../hooks/use_waffle_options';
2020
import { useSourceContext } from '../../../../containers/source';
2121
import { InfraFormatterType } from '../../../../lib/lib';
2222
import { euiStyled } from '../../../../../../observability/public';
@@ -62,10 +62,14 @@ export const Layout = () => {
6262
false
6363
);
6464

65+
const legendPalette = legend?.palette ?? DEFAULT_LEGEND.palette;
66+
const legendSteps = legend?.steps ?? DEFAULT_LEGEND.steps;
67+
const legendReverseColors = legend?.reverseColors ?? DEFAULT_LEGEND.reverseColors;
68+
6569
const options = {
6670
formatter: InfraFormatterType.percent,
6771
formatTemplate: '{{value}}',
68-
legend: createLegend(legend.palette, legend.steps, legend.reverseColors),
72+
legend: createLegend(legendPalette, legendSteps, legendReverseColors),
6973
metric,
7074
sort,
7175
fields: source?.configuration?.fields,

x-pack/plugins/infra/public/pages/metrics/inventory_view/components/waffle/legend.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {
1717
import { GradientLegend } from './gradient_legend';
1818
import { LegendControls } from './legend_controls';
1919
import { StepLegend } from './steps_legend';
20-
import { useWaffleOptionsContext, WaffleLegendOptions } from '../../hooks/use_waffle_options';
20+
import {
21+
DEFAULT_LEGEND,
22+
useWaffleOptionsContext,
23+
WaffleLegendOptions,
24+
} from '../../hooks/use_waffle_options';
2125
import { SteppedGradientLegend } from './stepped_gradient_legend';
2226
interface Props {
2327
legend: InfraWaffleMapLegend;
@@ -52,7 +56,7 @@ export const Legend: React.FC<Props> = ({ dataBounds, legend, bounds, formatter
5256
return (
5357
<LegendContainer>
5458
<LegendControls
55-
options={legendOptions}
59+
options={legendOptions != null ? legendOptions : DEFAULT_LEGEND}
5660
dataBounds={dataBounds}
5761
bounds={bounds}
5862
autoBounds={autoBounds}

x-pack/plugins/infra/public/pages/metrics/inventory_view/hooks/use_waffle_options.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ import {
2323
import { useUrlState } from '../../../../utils/use_url_state';
2424
import { InventoryItemType, ItemTypeRT } from '../../../../../common/inventory_models/types';
2525

26+
export const DEFAULT_LEGEND: WaffleLegendOptions = {
27+
palette: 'cool',
28+
steps: 10,
29+
reverseColors: false,
30+
};
31+
2632
export const DEFAULT_WAFFLE_OPTIONS_STATE: WaffleOptionsState = {
2733
metric: { type: 'cpu' },
2834
groupBy: [],
@@ -34,11 +40,7 @@ export const DEFAULT_WAFFLE_OPTIONS_STATE: WaffleOptionsState = {
3440
accountId: '',
3541
region: '',
3642
customMetrics: [],
37-
legend: {
38-
palette: 'cool',
39-
steps: 10,
40-
reverseColors: false,
41-
},
43+
legend: DEFAULT_LEGEND,
4244
source: 'default',
4345
sort: { by: 'name', direction: 'desc' },
4446
};
@@ -183,10 +185,9 @@ export const WaffleOptionsStateRT = rt.intersection([
183185
accountId: rt.string,
184186
region: rt.string,
185187
customMetrics: rt.array(SnapshotCustomMetricInputRT),
186-
legend: WaffleLegendOptionsRT,
187188
sort: WaffleSortOptionRT,
188189
}),
189-
rt.partial({ source: rt.string }),
190+
rt.partial({ source: rt.string, legend: WaffleLegendOptionsRT }),
190191
]);
191192

192193
export type WaffleSortOption = rt.TypeOf<typeof WaffleSortOptionRT>;

0 commit comments

Comments
 (0)