Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Layer column config: sometimes a suggested field pair will hard crash #2351

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof ColumnSelectorFactory>) {
Expand Down Expand Up @@ -103,7 +118,7 @@ function LayerColumnConfigFactory(ColumnSelector: ReturnType<typeof ColumnSelect
label={(columnLabels && columnLabels[key]) || key}
key={key}
allFields={fields}
fieldPairs={enhancedFieldPairs}
fieldPairs={getFieldPairsSuggestionsForColumn(enhancedFieldPairs, columnPairs, key)}
onSelect={val => onUpdateColumn(key, val)}
/>
))}
Expand Down
6 changes: 6 additions & 0 deletions src/layers/src/base-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 44 additions & 0 deletions test/node/utils/kepler-table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
}
]
}
];

Expand Down
Loading