Skip to content

Commit

Permalink
🐛 Fix potential missing layers bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Dec 28, 2021
1 parent 3a12158 commit 4d2987c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
24 changes: 11 additions & 13 deletions x-pack/plugins/lens/public/app_plugin/lens_top_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,27 +241,25 @@ export const LensTopNavMenu = ({
});

// Compute the list of visible columns, per layer/datatable, once
const tableVisibleColumnsPerLayer = React.useMemo(() => {
if (!activeData) {
const tableVisibleColumnsPerLayer = useMemo(() => {
if (!activeData || activeDatasourceId == null) {
return {};
}
const datatableColumns: Record<string, Datatable['columns']> = {};
const layerIds = Object.keys(activeData);
for (const layerId of layerIds) {
const visibleColumns = new Set(
activeDatasourceId
? datasourceMap[activeDatasourceId]
.getPublicAPI({
state: datasourceStates[activeDatasourceId].state,
layerId,
})
.getTableSpec()
.map(({ columnId }) => columnId)
: []
datasourceMap[activeDatasourceId]
.getPublicAPI({
state: datasourceStates[activeDatasourceId].state,
layerId,
})
.getTableSpec()
.map(({ columnId }) => columnId)
);

datatableColumns[layerId] = activeData[layerId].columns.filter(
({ id }) => visibleColumns.has(id) || !activeDatasourceId
datatableColumns[layerId] = activeData[layerId].columns.filter(({ id }) =>
visibleColumns.has(id)
);
}
return datatableColumns;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,18 +430,19 @@ export function getIndexPatternDatasource({

getPublicAPI({ state, layerId }: PublicAPIProps<IndexPatternPrivateState>) {
const columnLabelMap = indexPatternDatasource.uniqueLabels(state);
const layer = state ? state.layers[layerId] : undefined;

return {
datasourceId: 'indexpattern',

getTableSpec: () => {
return state.layers[layerId].columnOrder
.filter((colId) => !isReferenced(state.layers[layerId], colId))
.map((colId) => ({ columnId: colId }));
return layer
? layer.columnOrder
.filter((colId) => !isReferenced(layer, colId))
.map((colId) => ({ columnId: colId }))
: [];
},
getOperationForColumnId: (columnId: string) => {
const layer = state.layers[layerId];

if (layer && layer.columns[columnId]) {
if (!isReferenced(layer, columnId)) {
return columnToOperation(layer.columns[columnId], columnLabelMap[columnId]);
Expand All @@ -450,8 +451,7 @@ export function getIndexPatternDatasource({
return null;
},
getVisualDefaults: () => {
const layer = state.layers[layerId];
return getVisualDefaultsForLayer(layer);
return layer && getVisualDefaultsForLayer(layer);
},
};
},
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export interface DatasourcePublicAPI {
/**
* Collect all default visual values given the current state
*/
getVisualDefaults: () => Record<string, Record<string, unknown>>;
getVisualDefaults: () => Record<string, Record<string, unknown>> | undefined;
}

export interface DatasourceDataPanelProps<T = unknown> {
Expand Down

0 comments on commit 4d2987c

Please sign in to comment.