From 69c7dc8a4de0728f8d9781388b1b28a9a7a5d644 Mon Sep 17 00:00:00 2001 From: nickofthyme Date: Tue, 19 Jan 2021 16:43:14 -0600 Subject: [PATCH] fix: merge issues and code simplifications --- .../partition_chart/partition.test.tsx | 13 ------------- .../state/selectors/get_legend_items_extra.ts | 19 ++++--------------- src/components/legend/utils.ts | 9 ++------- 3 files changed, 6 insertions(+), 35 deletions(-) diff --git a/src/chart_types/partition_chart/partition.test.tsx b/src/chart_types/partition_chart/partition.test.tsx index 86fe54fc36..05f257e8d5 100644 --- a/src/chart_types/partition_chart/partition.test.tsx +++ b/src/chart_types/partition_chart/partition.test.tsx @@ -102,7 +102,6 @@ describe('Retain hierarchy even with arbitrary names', () => { { index: 0, value: 'A' }, ], depth: 0, - path: [0, 0], label: 'A', seriesIdentifier: { key: 'A', specId: 'spec1' }, }, @@ -115,7 +114,6 @@ describe('Retain hierarchy even with arbitrary names', () => { { index: 0, value: 'A' }, ], depth: 1, - path: [0, 0, 0], label: 'A', seriesIdentifier: { key: 'A', specId: 'spec1' }, }, @@ -128,7 +126,6 @@ describe('Retain hierarchy even with arbitrary names', () => { { index: 1, value: 'B' }, ], depth: 1, - path: [0, 0, 1], label: 'B', seriesIdentifier: { key: 'B', specId: 'spec1' }, }, @@ -140,7 +137,6 @@ describe('Retain hierarchy even with arbitrary names', () => { { index: 1, value: 'B' }, ], depth: 0, - path: [0, 1], label: 'B', seriesIdentifier: { key: 'B', specId: 'spec1' }, }, @@ -153,7 +149,6 @@ describe('Retain hierarchy even with arbitrary names', () => { { index: 0, value: 'A' }, ], depth: 1, - path: [0, 1, 0], label: 'A', seriesIdentifier: { key: 'A', specId: 'spec1' }, }, @@ -166,7 +161,6 @@ describe('Retain hierarchy even with arbitrary names', () => { { index: 1, value: 'B' }, ], depth: 1, - path: [0, 1, 1], label: 'B', seriesIdentifier: { key: 'B', specId: 'spec1' }, }, @@ -178,7 +172,6 @@ describe('Retain hierarchy even with arbitrary names', () => { { index: 2, value: 'C' }, ], depth: 0, - path: [0, 2], label: 'C', seriesIdentifier: { key: 'C', specId: 'spec1' }, }, @@ -191,7 +184,6 @@ describe('Retain hierarchy even with arbitrary names', () => { { index: 0, value: 'A' }, ], depth: 1, - path: [0, 2, 0], label: 'A', seriesIdentifier: { key: 'A', specId: 'spec1' }, }, @@ -204,7 +196,6 @@ describe('Retain hierarchy even with arbitrary names', () => { { index: 1, value: 'B' }, ], depth: 1, - path: [0, 2, 1], label: 'B', seriesIdentifier: { key: 'B', specId: 'spec1' }, }, @@ -228,7 +219,6 @@ describe('Retain hierarchy even with arbitrary names', () => { }, ], depth: 0, - path: [0, 0], label: 'A', seriesIdentifier: { key: 'A', specId: 'spec1' }, }, @@ -251,7 +241,6 @@ describe('Retain hierarchy even with arbitrary names', () => { ], depth: 1, - path: [0, 0, 0], label: 'A', seriesIdentifier: { key: 'A', specId: 'spec1' }, }, @@ -275,7 +264,6 @@ describe('Retain hierarchy even with arbitrary names', () => { }, ], depth: 0, - path: [0, 0], label: 'C', seriesIdentifier: { key: 'C', specId: 'spec1' }, }, @@ -297,7 +285,6 @@ describe('Retain hierarchy even with arbitrary names', () => { }, ], depth: 1, - path: [0, 0, 0], label: 'B', seriesIdentifier: { key: 'B', specId: 'spec1' }, }, diff --git a/src/chart_types/partition_chart/state/selectors/get_legend_items_extra.ts b/src/chart_types/partition_chart/state/selectors/get_legend_items_extra.ts index 9e8be5c3ea..a71d5464fe 100644 --- a/src/chart_types/partition_chart/state/selectors/get_legend_items_extra.ts +++ b/src/chart_types/partition_chart/state/selectors/get_legend_items_extra.ts @@ -19,8 +19,8 @@ import createCachedSelector from 're-reselect'; -import { LegendItemExtraValues } from '../../../../commons/legend'; -import { SeriesKey } from '../../../../commons/series_id'; +import { LegendItemExtraValues } from '../../../../common/legend'; +import { SeriesKey } from '../../../../common/series_id'; import { SettingsSpec } from '../../../../specs'; import { getChartIdSelector } from '../../../../state/selectors/get_chart_id'; import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs'; @@ -35,14 +35,7 @@ export const getLegendItemsExtra = createCachedSelector( (pieSpec, { legendMaxDepth }, tree): Map => { const legendExtraValues = new Map(); - if (!pieSpec) { - return legendExtraValues; - } - if (isInvalidLegendMaxDepth(legendMaxDepth)) { - return legendExtraValues; - } - - return getExtraValueMap(pieSpec, tree); + return pieSpec && !isInvalidLegendMaxDepth(legendMaxDepth) ? getExtraValueMap(pieSpec, tree) : legendExtraValues; }, )(getChartIdSelector); @@ -63,10 +56,6 @@ function getExtraValueMap( tree: HierarchyOfArrays, keys: Map = new Map(), ): Map { - if (tree.length === 0) { - return keys; - } - for (let i = 0; i < tree.length; i++) { const branch = tree[i]; const [key, arrayNode] = branch; @@ -77,7 +66,7 @@ function getExtraValueMap( const formattedValue = valueFormatter ? valueFormatter(value) : value; values.set(key, formattedValue); - keys.set(path.join('__'), values); + keys.set(path.map(({ value: v }) => v).join('__'), values); } getExtraValueMap({ layers, valueFormatter }, children, keys); diff --git a/src/components/legend/utils.ts b/src/components/legend/utils.ts index 618697dcda..bc2b3ec809 100644 --- a/src/components/legend/utils.ts +++ b/src/components/legend/utils.ts @@ -20,16 +20,11 @@ import { LegendItemExtraValues, LegendItem } from '../../common/legend'; /** @internal */ export function getExtra(extraValues: Map, item: LegendItem, totalItems: number) { - const { - seriesIdentifier: { key }, - defaultExtra, - childId, - path, - } = item; + const { defaultExtra, childId, path } = item; if (extraValues.size === 0) { return defaultExtra?.formatted ?? ''; } - const extraValueKey = path ? path.join('__') : key; + const extraValueKey = path.map(({ value: v }) => v).join('__'); const itemExtraValues = extraValues.get(extraValueKey); const actionExtra = (childId && itemExtraValues?.get(childId)) ?? null; if (extraValues.size !== totalItems) {