Skip to content

Commit

Permalink
fixing state issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Jan 24, 2024
1 parent 6cff500 commit c61b3df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const useLensSuggestions = ({
timeRange,
lensSuggestionsApi,
onSuggestionChange,
table,
}: {
dataView: DataView;
query?: Query | AggregateQuery;
Expand All @@ -51,7 +50,6 @@ export const useLensSuggestions = ({
fieldName: '',
textBasedColumns: columns,
query: query && isOfAggregateQueryType(query) ? query : undefined,
table,
};
const allSuggestions = isPlainRecord
? lensSuggestionsApi(context, dataView, ['lnsDatatable']) ?? []
Expand All @@ -60,10 +58,11 @@ export const useLensSuggestions = ({
const [firstSuggestion] = allSuggestions;

return { firstSuggestion, allSuggestions };
}, [dataView, columns, query, table, isPlainRecord, lensSuggestionsApi]);
}, [dataView, columns, query, isPlainRecord, lensSuggestionsApi]);

const [allSuggestions, setAllSuggestions] = useState(suggestions.allSuggestions);
const currentSuggestion = originalSuggestion || suggestions.firstSuggestion;

const suggestionDeps = useRef(getSuggestionDeps({ dataView, query, columns }));
const histogramQuery = useRef<AggregateQuery | undefined>();
const histogramSuggestion = useMemo(() => {
Expand Down Expand Up @@ -125,7 +124,7 @@ export const useLensSuggestions = ({
}, [currentSuggestion, dataView, query, timeRange, data, lensSuggestionsApi]);

useEffect(() => {
const newSuggestionsDeps = getSuggestionDeps({ dataView, query, columns, table });
const newSuggestionsDeps = getSuggestionDeps({ dataView, query, columns });

if (!isEqual(suggestionDeps.current, newSuggestionsDeps)) {
setAllSuggestions(suggestions.allSuggestions);
Expand All @@ -136,7 +135,6 @@ export const useLensSuggestions = ({
}, [
columns,
dataView,
table,
onSuggestionChange,
query,
suggestions.firstSuggestion,
Expand All @@ -156,10 +154,8 @@ const getSuggestionDeps = ({
dataView,
query,
columns,
table,
}: {
dataView: DataView;
query?: Query | AggregateQuery;
columns?: DatatableColumn[];
table?: Datatable;
}) => [dataView.id, columns, query, table];
}) => [dataView.id, columns, query];
11 changes: 10 additions & 1 deletion src/plugins/unified_histogram/public/layout/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ResizableLayoutMode,
ResizableLayoutDirection,
} from '@kbn/resizable-layout';
import { TextBasedPersistedState } from '@kbn/lens-plugin/public/datasources/text_based/types';
import { Chart, checkChartAvailability } from '../chart';
import type {
UnifiedHistogramChartContext,
Expand Down Expand Up @@ -238,9 +239,17 @@ export const UnifiedHistogramLayout = ({
data: services.data,
lensSuggestionsApi,
onSuggestionChange,
table,
});

// apply table to current suggestion
if (table) {
const { layers } = currentSuggestion.datasourceState as TextBasedPersistedState;
Object.keys(layers).forEach((key) => {
const layer = layers[key];
layer.table = table;
});
}

const chart = suggestionUnsupported ? undefined : originalChart;
const isChartAvailable = checkChartAvailability({ chart, dataView, isPlainRecord });

Expand Down
15 changes: 2 additions & 13 deletions x-pack/plugins/lens/public/lens_suggestions_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
*/
import type { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';
import { Datatable } from '@kbn/expressions-plugin/common';
import { TextBasedPersistedState } from './datasources/text_based/types';
import { getSuggestions } from './editor_frame_service/editor_frame/suggestion_helpers';
import type { DatasourceMap, VisualizationMap, VisualizeEditorContext } from './types';
import type { DataViewsState } from './state_management';

interface SuggestionsApi {
context: (VisualizeFieldContext | VisualizeEditorContext) & { table?: Datatable };
context: VisualizeFieldContext | VisualizeEditorContext;
dataView: DataView;
visualizationMap?: VisualizationMap;
datasourceMap?: DatasourceMap;
Expand All @@ -27,7 +25,7 @@ export const suggestionsApi = ({
visualizationMap,
excludedVisualizations,
}: SuggestionsApi) => {
const { table, ...initialContext } = context;
const initialContext = context;
if (!datasourceMap || !visualizationMap || !dataView.id) return undefined;
const datasourceStates = {
formBased: {
Expand Down Expand Up @@ -92,14 +90,5 @@ export const suggestionsApi = ({
// we want to sort XY first
const sortXYFirst = suggestionsList.sort((a, b) => (a.visualizationId === 'lnsXY' ? -1 : 1));

if (table) {
sortXYFirst.forEach((suggestion) => {
const { layers } = suggestion.datasourceState as TextBasedPersistedState;
Object.keys(layers).forEach((key) => {
const layer = layers[key];
layer.table = table;
});
});
}
return sortXYFirst;
};

0 comments on commit c61b3df

Please sign in to comment.