Skip to content

Commit

Permalink
fix moving between edit/view mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra committed Oct 10, 2024
1 parent ea89ca9 commit 5d95295
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 39 deletions.
30 changes: 20 additions & 10 deletions x-pack/plugins/lens/public/react_embeddable/data_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
*/

import type { DefaultInspectorAdapters, RenderMode } from '@kbn/expressions-plugin/common';
import { fetch$, apiHasExecutionContext, type FetchContext } from '@kbn/presentation-publishing';
import {
fetch$,
apiHasExecutionContext,
type FetchContext,
apiPublishesViewMode,
getInheritedViewMode,
} from '@kbn/presentation-publishing';
import { apiPublishesSearchSession } from '@kbn/presentation-publishing/interfaces/fetch/publishes_search_session';
import { type KibanaExecutionContext } from '@kbn/core/public';
import { BehaviorSubject, type Subscription, distinctUntilChanged, skip } from 'rxjs';
Expand Down Expand Up @@ -133,7 +139,9 @@ export function loadEmbeddableData(
syncCursor: currentState.syncCursor,
syncTooltips: currentState.syncTooltips,
},
renderMode: currentState.viewMode as RenderMode,
renderMode: (apiPublishesViewMode(parentApi) && parentApi.viewMode
? getInheritedViewMode(parentApi)
: 'view') as RenderMode,
services,
searchSessionId,
abortController: internalApi.expressionAbortController$.getValue(),
Expand Down Expand Up @@ -192,15 +200,17 @@ export function loadEmbeddableData(
api.savedObjectId.pipe(distinctUntilChanged(fastIsEqual), skip(1)).subscribe(reload),
internalApi.overrides$.pipe(distinctUntilChanged(fastIsEqual), skip(1)).subscribe(reload),
internalApi.disableTriggers$.pipe(distinctUntilChanged(fastIsEqual), skip(1)).subscribe(reload),

// reload on view mode change only if drilldowns are set
internalApi.viewMode$.subscribe(() => {
// only reload if drilldowns are set
if (getState().enhancements?.dynamicActions) {
reload();
}
}),
];
if (apiPublishesViewMode(parentApi)) {
subscriptions.push(
parentApi.viewMode.subscribe(() => {
// only reload if drilldowns are set
if (getState().enhancements?.dynamicActions) {
reload();
}
})
);
}

return {
getUserMessages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import { noop } from 'lodash';
import {
HasInPlaceLibraryTransforms,
PublishesPanelTitle,
PublishesViewMode,
PublishesWritablePanelTitle,
SerializedTitles,
StateComparators,
ViewMode,
getUnchangingComparator,
initializeTitles,
} from '@kbn/presentation-publishing';
Expand All @@ -37,7 +35,6 @@ type SerializedProps = SerializedTitles & LensPanelProps & LensOverrides & LensS

export interface DashboardServicesConfig {
api: PublishesPanelTitle &
PublishesViewMode &
PublishesWritablePanelTitle &
HasInPlaceLibraryTransforms &
Pick<IntegrationCallbacks, 'updateOverrides'>;
Expand All @@ -59,9 +56,6 @@ export function initializeDashboardServices(
): DashboardServicesConfig {
const { titlesApi, serializeTitles, titleComparators } = initializeTitles(initialState);

const [viewMode$, viewModeComparator] = buildObservableVariable<ViewMode | undefined>(
internalApi.viewMode$
);
const [defaultPanelTitle$] = buildObservableVariable<string | undefined>(
initialState.title || initialState.attributes.title
);
Expand All @@ -81,7 +75,6 @@ export function initializeDashboardServices(
api: {
defaultPanelTitle: defaultPanelTitle$,
...titlesApi,
viewMode: viewMode$ as PublishesViewMode['viewMode'],
libraryId$: stateConfig.api.savedObjectId,
updateOverrides: internalApi.updateOverrides,
saveToLibrary: async (title: string) => {
Expand Down Expand Up @@ -149,7 +142,6 @@ export function initializeDashboardServices(
...settings,
palette: initialState.palette,
overrides: overrides$.getValue(),
viewMode: viewMode$.getValue(),
disableTriggers: disableTriggers$.getValue(),
};
},
Expand All @@ -163,11 +155,11 @@ export function initializeDashboardServices(
syncTooltips: getUnchangingComparator<SerializedTitles & LensPanelProps, 'syncTooltips'>(),
executionContext: getUnchangingComparator<LensSharedProps, 'executionContext'>(),
noPadding: getUnchangingComparator<LensSharedProps, 'noPadding'>(),
viewMode: getUnchangingComparator<LensSharedProps, 'viewMode'>(),
style: getUnchangingComparator<LensSharedProps, 'style'>(),
className: getUnchangingComparator<LensSharedProps, 'className'>(),
overrides: overridesComparator,
disableTriggers: disabledTriggersComparator,
viewMode: viewModeComparator,
},
cleanup: noop,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import { BehaviorSubject } from 'rxjs';
import { ViewMode, apiPublishesViewMode } from '@kbn/presentation-publishing';
import type { DataView } from '@kbn/data-views-plugin/common';
import { buildObservableVariable } from '../helper';
import type {
Expand Down Expand Up @@ -36,11 +35,6 @@ export function initializeInternalApi(
const disableTriggers$ = new BehaviorSubject(initialState.disableTriggers);
const dataLoading$ = new BehaviorSubject<boolean | undefined>(undefined);

const viewMode$ = new BehaviorSubject<ViewMode | undefined>('view');
if (apiPublishesViewMode(parentApi)) {
viewMode$.next(parentApi.viewMode.getValue());
}

const dataViews$ = new BehaviorSubject<DataView[] | undefined>(undefined);

// No need to expose anything at public API right now, that would happen later on
Expand All @@ -54,7 +48,6 @@ export function initializeInternalApi(
expressionParams$,
expressionAbortController$,
renderCount$,
viewMode$,
dataViews: dataViews$,
// This function is used to force a re-render of the component (i.e. to refresh user messages)
updateRenderCount: () => renderCount$.next(renderCount$.getValue() + 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import {
HasEditCapabilities,
HasSupportedTriggers,
PublishesViewMode,
apiHasAppContext,
apiPublishesViewMode,
getInheritedViewMode,
} from '@kbn/presentation-publishing';
import { ENABLE_ESQL } from '@kbn/esql-utils';
import { noop } from 'lodash';
Expand Down Expand Up @@ -45,15 +46,13 @@ function getSupportedTriggers(

/**
* Initialize the edit API for the embeddable
* Note: this has also the side effect to update the viewMode$ if parent publishes it
**/
export function initializeEditApi(
uuid: string,
initialState: LensRuntimeState,
getState: GetStateType,
internalApi: LensInternalApi,
stateApi: StateManagementConfig['api'],
{ viewMode }: PublishesViewMode,
inspectorApi: LensInspectorAdapters,
isTextBasedLanguage: (currentState: LensRuntimeState) => boolean,
startDependencies: LensEmbeddableStartServices,
Expand Down Expand Up @@ -117,7 +116,7 @@ export function initializeEditApi(
const { uiSettings, capabilities, data } = startDependencies;

const canEdit = () => {
if (viewMode.getValue() !== 'edit') {
if (apiPublishesViewMode(parentApi) && getInheritedViewMode(parentApi) !== 'edit') {
return false;
}
// check if it's in ES|QL mode
Expand Down
15 changes: 11 additions & 4 deletions x-pack/plugins/lens/public/react_embeddable/lens_embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
*/

import React, { useEffect } from 'react';
import { useStateFromPublishingSubject } from '@kbn/presentation-publishing';
import {
apiPublishesViewMode,
getInheritedViewMode,
useStateFromPublishingSubject,
} from '@kbn/presentation-publishing';
import { ReactEmbeddableFactory } from '@kbn/embeddable-plugin/public';
import { DOC_TYPE } from '../../common/constants';
import {
Expand Down Expand Up @@ -96,7 +100,6 @@ export const createLensEmbeddableFactory = (
getState,
internalApi,
stateConfig.api,
dashboardConfig.api,
inspectorConfig.api,
isTextBasedLanguage,
services,
Expand Down Expand Up @@ -181,14 +184,18 @@ export const createLensEmbeddableFactory = (
return {
api,
Component: () => {
const { renderCount$, hasRenderCompleted$, expressionParams$, viewMode$ } = internalApi;
const { renderCount$, hasRenderCompleted$, expressionParams$ } = internalApi;
// Pick up updated params from the observable
const expressionParams = useStateFromPublishingSubject(expressionParams$);
// used for functional tests
const renderCount = useStateFromPublishingSubject(renderCount$);
// used for reporting/functional tests
const hasRendered = useStateFromPublishingSubject(hasRenderCompleted$);
const canEdit = Boolean(api.isEditingEnabled?.() && viewMode$.getValue() === 'edit');
const canEdit = Boolean(
api.isEditingEnabled?.() &&
apiPublishesViewMode(parentApi) &&
getInheritedViewMode(parentApi) === 'edit'
);

const [blockingErrors, warningOrErrors, infoMessages] = useMessages(
getUserMessages,
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/lens/public/react_embeddable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export type LensComponentProps = Simplify<
*/
export type LensComponentForwardedProps = Pick<
LensComponentProps,
'style' | 'className' | 'noPadding' | 'viewMode' | 'abortController' | 'executionContext'
'style' | 'className' | 'noPadding' | 'abortController' | 'executionContext' | 'viewMode'
>;

/**
Expand Down Expand Up @@ -373,7 +373,6 @@ export type LensInternalApi = Simplify<
expressionAbortController$: PublishingSubject<AbortController | undefined>;
updateAbortController: (newAbortController: AbortController | undefined) => void;
renderCount$: PublishingSubject<number>;
viewMode$: PublishingSubject<ViewMode | undefined>;
updateDataViews: (dataViews: DataView[] | undefined) => void;
}
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ export async function executeEditEmbeddableAction({
canBeCreatedInline: false, // this is the edit action
});
const openInlineEditor = prepareInlineEditPanel(
() => ({
attributes,
}),
() => ({ attributes }),
(newState: LensRuntimeState) =>
onUpdate(newState.attributes as TypedLensByValueInput['attributes']),
{
Expand Down

0 comments on commit 5d95295

Please sign in to comment.