Skip to content

Commit

Permalink
[Lens][Inspector] Close the inspector on Lens unmount (elastic#114317)
Browse files Browse the repository at this point in the history
* 🐛 First attempt to close the inspector automatically on app unmount

* 👌 Integrate feedback

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
2 people authored and artem-shelkovnikov committed Oct 20, 2021
1 parent db67c4b commit 49d7874
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 43 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ describe('Lens App', () => {

await runInspect(instance);

expect(services.inspector.open).toHaveBeenCalledTimes(1);
expect(services.inspector.inspect).toHaveBeenCalledTimes(1);
});
});

Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
getLastKnownDocWithoutPinnedFilters,
runSaveLensVisualization,
} from './save_modal_container';
import { getLensInspectorService, LensInspector } from '../lens_inspector_service';
import { LensInspector } from '../lens_inspector_service';
import { getEditPath } from '../../common';

export type SaveProps = Omit<OnSaveProps, 'onTitleDuplicate' | 'newDescription'> & {
Expand Down Expand Up @@ -66,7 +66,7 @@ export function App({
data,
chrome,
uiSettings,
inspector,
inspector: lensInspector,
application,
notifications,
savedObjectsTagging,
Expand Down Expand Up @@ -101,8 +101,6 @@ export function App({
const [isSaveModalVisible, setIsSaveModalVisible] = useState(false);
const [lastKnownDoc, setLastKnownDoc] = useState<Document | undefined>(undefined);

const lensInspector = getLensInspectorService(inspector);

useEffect(() => {
if (currentDoc) {
setLastKnownDoc(currentDoc);
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/lens/public/app_plugin/mounter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
LensState,
} from '../state_management';
import { getPreloadedState } from '../state_management/lens_slice';
import { getLensInspectorService } from '../lens_inspector_service';

export async function getLensServices(
coreStart: CoreStart,
Expand All @@ -65,7 +66,7 @@ export async function getLensServices(
return {
data,
storage,
inspector,
inspector: getLensInspectorService(inspector),
navigation,
fieldFormats,
stateTransfer,
Expand Down Expand Up @@ -278,6 +279,7 @@ export async function mountApp(
return () => {
data.search.session.clear();
unmountComponentAtNode(params.element);
lensServices.inspector.close();
unlistenParentHistory();
lensStore.dispatch(navigateAway());
};
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/lens/public/app_plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type {
import type { DataPublicPluginStart } from '../../../../../src/plugins/data/public';
import type { UsageCollectionStart } from '../../../../../src/plugins/usage_collection/public';
import type { DashboardStart } from '../../../../../src/plugins/dashboard/public';
import type { Start as InspectorStart } from '../../../../../src/plugins/inspector/public';
import type { LensEmbeddableInput } from '../embeddable/embeddable';
import type { NavigationPublicPluginStart } from '../../../../../src/plugins/navigation/public';
import type { LensAttributeService } from '../lens_attribute_service';
Expand Down Expand Up @@ -105,7 +104,7 @@ export interface LensAppServices {
dashboard: DashboardStart;
fieldFormats: FieldFormatsStart;
data: DataPublicPluginStart;
inspector: InspectorStart;
inspector: LensInspector;
uiSettings: IUiSettingsClient;
application: ApplicationStart;
notifications: NotificationsStart;
Expand Down
13 changes: 12 additions & 1 deletion x-pack/plugins/lens/public/lens_inspector_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@
import type {
Adapters,
InspectorOptions,
InspectorSession,
Start as InspectorStartContract,
} from '../../../../src/plugins/inspector/public';

import { createDefaultInspectorAdapters } from '../../../../src/plugins/expressions/public';

export const getLensInspectorService = (inspector: InspectorStartContract) => {
const adapters: Adapters = createDefaultInspectorAdapters();
let overlayRef: InspectorSession | undefined;
return {
adapters,
inspect: (options?: InspectorOptions) => inspector.open(adapters, options),
inspect: (options?: InspectorOptions) => {
overlayRef = inspector.open(adapters, options);
overlayRef.onClose.then(() => {
if (overlayRef) {
overlayRef = undefined;
}
});
return overlayRef;
},
close: () => overlayRef?.close(),
};
};

Expand Down
7 changes: 6 additions & 1 deletion x-pack/plugins/lens/public/mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
DatasourceMap,
VisualizationMap,
} from './types';
import { getLensInspectorService } from './lens_inspector_service';

export function mockDatasourceStates() {
return {
Expand Down Expand Up @@ -417,7 +418,11 @@ export function makeDefaultServices(
navigation: navigationStartMock,
notifications: core.notifications,
attributeService: makeAttributeService(),
inspector: inspectorPluginMock.createStartContract(),
inspector: {
adapters: getLensInspectorService(inspectorPluginMock.createStartContract()).adapters,
inspect: jest.fn(),
close: jest.fn(),
},
dashboard: dashboardPluginMock.createStartContract(),
presentationUtil: presentationUtilPluginMock.createStartContract(core),
savedObjectsClient: core.savedObjects.client,
Expand Down
6 changes: 6 additions & 0 deletions x-pack/test/functional/apps/lens/inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const PageObjects = getPageObjects(['visualize', 'lens', 'common', 'header']);
const elasticChart = getService('elasticChart');
const inspector = getService('inspector');
const testSubjects = getService('testSubjects');

describe('lens inspector', () => {
before(async () => {
Expand Down Expand Up @@ -55,5 +56,10 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
await inspector.openInspectorRequestsView();
expect(await inspector.getRequestNames()).to.be('Data,Other bucket');
});

it('should close the inspector when navigating away from Lens', async () => {
await PageObjects.visualize.navigateToNewVisualization();
expect(await testSubjects.exists('inspectorPanel')).to.be(false);
});
});
}

0 comments on commit 49d7874

Please sign in to comment.