From 4f771d82a4cb0feb360e6a6b3dc3fe6487543ebb Mon Sep 17 00:00:00 2001 From: Jacob Wasilkowski <4933392+jwasilgeo@users.noreply.github.com> Date: Wed, 14 Jun 2023 18:06:36 -0400 Subject: [PATCH] [fix] Layer column config: sometimes a suggested field pair will hard crash Signed-off-by: Ihor Dykhta --- .../layer-panel/layer-column-config.tsx | 17 ++++++- src/layers/src/base-layer.ts | 6 +++ test/node/utils/kepler-table-test.js | 44 +++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/components/src/side-panel/layer-panel/layer-column-config.tsx b/src/components/src/side-panel/layer-panel/layer-column-config.tsx index 65ba89e7c4..3c9328046f 100644 --- a/src/components/src/side-panel/layer-panel/layer-column-config.tsx +++ b/src/components/src/side-panel/layer-panel/layer-column-config.tsx @@ -42,6 +42,21 @@ const TopRow = styled.div` justify-content: space-between; `; +/** + * only provide suggested field pairs if there is a match, + * otherwise the user can select a suggested field pair that will create invalid columns and a hard crash + */ +function getFieldPairsSuggestionsForColumn( + enhancedFieldPairs, + columnPairs: ColumnPairs | null | undefined, + columnKey: string +) { + const matchingFieldPairs = enhancedFieldPairs?.filter(({pair}) => + pair.hasOwnProperty(columnPairs?.[columnKey]?.fieldPairKey) + ); + return matchingFieldPairs.length > 0 ? matchingFieldPairs : null; +} + LayerColumnConfigFactory.deps = [ColumnSelectorFactory]; function LayerColumnConfigFactory(ColumnSelector: ReturnType) { @@ -103,7 +118,7 @@ function LayerColumnConfigFactory(ColumnSelector: ReturnType onUpdateColumn(key, val)} /> ))} diff --git a/src/layers/src/base-layer.ts b/src/layers/src/base-layer.ts index e435ff7641..2d58535ede 100644 --- a/src/layers/src/base-layer.ts +++ b/src/layers/src/base-layer.ts @@ -553,6 +553,12 @@ class Layer { } const {pair: partnerKey, fieldPairKey} = this.columnPairs?.[key]; + + if (!pair[fieldPairKey]) { + // do not allow `key: undefined` to creep into the `updatedColumn` object + return this.config.columns; + } + const {fieldPairKey: partnerFieldPairKey} = this.columnPairs?.[partnerKey]; return { diff --git a/test/node/utils/kepler-table-test.js b/test/node/utils/kepler-table-test.js index de691bbe84..a9faf50e98 100644 --- a/test/node/utils/kepler-table-test.js +++ b/test/node/utils/kepler-table-test.js @@ -371,6 +371,50 @@ test('KeplerTable -> findPointFieldPairs', t => { suffix: ['latitude', 'longitude'] } ] + }, + { + fields: ['point_lat', 'point_lng', 'alt'], + expected: [ + { + defaultName: 'point', + pair: { + // no matching "alt" altitude found for this pair + lat: { + fieldIdx: 0, + value: 'point_lat' + }, + lng: { + fieldIdx: 1, + value: 'point_lng' + } + }, + suffix: ['lat', 'lng'] + } + ] + }, + { + fields: ['point_lat', 'point_lng', 'point_alt'], + expected: [ + { + defaultName: 'point', + pair: { + // a matching "point_alt" altitude was found for this pair + lat: { + fieldIdx: 0, + value: 'point_lat' + }, + lng: { + fieldIdx: 1, + value: 'point_lng' + }, + alt: { + fieldIdx: 2, + value: 'point_alt' + } + }, + suffix: ['lat', 'lng'] + } + ] } ];