Skip to content

Commit

Permalink
[Discover] Don't reset the vis on save
Browse files Browse the repository at this point in the history
  • Loading branch information
jughosta committed Jan 17, 2024
1 parent c6ee408 commit 55e3641
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,14 @@ const createAppStateObservable = (state$: Observable<DiscoverAppState>) => {
changes.chartHidden = curr.hideChart;
}

if (!isEqual(prev?.visContext, curr.visContext)) {
console.log('noticed changes in discover vis context', curr.visContext);
if (!isEqual(prev?.visContext?.attributes, curr.visContext?.attributes)) {
console.log(
'noticed changes in discover vis context',
'prev:',
prev?.visContext?.attributes,
'next:',
curr.visContext?.attributes
);
changes.externalVisContext = curr.visContext;
}

Expand Down
8 changes: 8 additions & 0 deletions src/plugins/unified_histogram/public/chart/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export function Chart({

const onSuggestionSelectorChange = useCallback(
(s: Suggestion | undefined) => {
console.log('suggestion change detected, generating a new vis context');
onSuggestionChange?.(s);
onVisContextChanged?.(s ? getLensAttributesCallback(s, undefined) : undefined);
},
Expand Down Expand Up @@ -280,6 +281,13 @@ export function Chart({
});
}

console.log(
'active suggestion',
currentSuggestion,
'active vis context',
externalVisContext?.attributes
);

return (
<EuiFlexGroup
{...a11yCommonProps}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export const useLensProps = ({
const updateLensPropsContext = useStableCallback(() => setLensPropsContext(buildLensProps()));

// TODO: find a better solution
// // Reverting saved search changes would change only attributesContext without a refetch
// useEffect(() => {
// updateLensPropsContext();
// }, [attributesContext, updateLensPropsContext]);
// Reverting saved search changes would change only attributesContext without a refetch
useEffect(() => {
updateLensPropsContext();
}, [attributesContext, updateLensPropsContext]);

useEffect(() => {
const subscription = refetch$.subscribe(updateLensPropsContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ export const getLensAttributes = ({

let shouldUpdateVisContextDueToIncompatibleSuggestion = false;

if (externalVisContext) {
if (externalVisContext && suggestion) {
if (
isEqual(
externalVisContext.attributes?.state?.query,
query
// TODO: check that's compatible based on suggestion deps
) /* && isEqual(externalVisContext.requestData, requestData) */ &&
isEqual(externalVisContext.attributes?.state?.query, query) &&
isSuggestionAndVisContextCompatible(suggestion, externalVisContext)
) {
console.log('using the external lens attributes');
return externalVisContext;
} else {
console.log('external vis is not compatible with the current suggestion');
shouldUpdateVisContextDueToIncompatibleSuggestion = true;
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/plugins/unified_histogram/public/container/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import React, { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useState } from 'react';
import { Subject } from 'rxjs';
import { pick } from 'lodash';
import useMount from 'react-use/lib/useMount';
Expand All @@ -25,6 +25,7 @@ import {
currentSuggestionSelector,
externalVisContextSelector,
} from './utils/state_selectors';
import { getStableVisContext } from '../utils/external_vis_context';

type LayoutProps = Pick<
UnifiedHistogramLayoutProps,
Expand Down Expand Up @@ -89,7 +90,7 @@ export type UnifiedHistogramApi = {
export const UnifiedHistogramContainer = forwardRef<
UnifiedHistogramApi,
UnifiedHistogramContainerProps
>((containerProps, ref) => {
>(({ onVisContextChanged, ...containerProps }, ref) => {
const [layoutProps, setLayoutProps] = useState<LayoutProps>();
const [stateService, setStateService] = useState<UnifiedHistogramStateService>();
const [lensSuggestionsApi, setLensSuggestionsApi] = useState<LensSuggestionsApi>();
Expand Down Expand Up @@ -144,6 +145,16 @@ export const UnifiedHistogramContainer = forwardRef<
requestAdapter,
});

const handleVisContextChange: typeof onVisContextChanged | undefined = useMemo(() => {
if (!onVisContextChanged) {
return undefined;
}

return (visContext) => {
onVisContextChanged(getStableVisContext(visContext));
};
}, [onVisContextChanged]);

// Don't render anything until the container is initialized
if (!layoutProps || !lensSuggestionsApi || !api) {
return null;
Expand All @@ -156,6 +167,7 @@ export const UnifiedHistogramContainer = forwardRef<
{...stateProps}
currentSuggestion={currentSuggestion}
externalVisContext={externalVisContext}
onVisContextChanged={handleVisContextChange}
isChartLoading={Boolean(isChartLoading)}
topPanelHeight={topPanelHeight}
input$={input$}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,21 @@ export const useLensSuggestions = ({
let currentSuggestion = originalSuggestion ?? suggestions.firstSuggestion;

if (externalVisContext) {
const matchingSuggestion = allSuggestions.find((suggestion) =>
const matchingSuggestion = suggestions.allSuggestions.find((suggestion) =>
isSuggestionAndVisContextCompatible(suggestion, externalVisContext)
);

currentSuggestion = matchingSuggestion || currentSuggestion;
}

console.log(
'use_lens_suggestion',
'selected suggestion',
currentSuggestion,
'current vis context',
externalVisContext?.attributes
);

const suggestionDeps = useRef(getSuggestionDeps({ dataView, query, columns }));
const histogramQuery = useRef<AggregateQuery | undefined>();

Expand Down Expand Up @@ -155,6 +163,10 @@ export const useLensSuggestions = ({
suggestions.allSuggestions,
]);

if (histogramSuggestion) {
console.log('using histogram suggestion');
}

return {
allSuggestions,
currentSuggestion: histogramSuggestion ?? currentSuggestion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import type { Suggestion } from '@kbn/lens-plugin/public';
import type { ExternalVisContext } from '../types';
import type { ExternalVisContext, LensAttributesContext } from '../types';

export const toExternalVisContextJSONString = (
visContext: ExternalVisContext | undefined
Expand Down Expand Up @@ -57,3 +57,8 @@ export const isSuggestionAndVisContextCompatible = (
externalVisContext?.attributes?.state?.visualization?.shape
);
};

export const getStableVisContext = (visContext: LensAttributesContext | undefined) => {
// clearing out undefined values from the object
return visContext ? JSON.parse(JSON.stringify(visContext)) : undefined;
};

0 comments on commit 55e3641

Please sign in to comment.