Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discover] Adds an Options menu for switching between the two table modes #97120

Merged
merged 8 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ function getProps(): DiscoverTopNavProps {
discover: {
save: true,
},
advancedSettings: {
save: true,
},
},
uiSettings: mockUiSettings,
} as unknown) as DiscoverServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const services = ({
discover: {
save: true,
},
advancedSettings: {
save: true,
},
},
} as unknown) as DiscoverServices;

Expand All @@ -36,6 +39,13 @@ test('getTopNavLinks result', () => {
});
expect(topNavLinks).toMatchInlineSnapshot(`
Array [
Object {
"description": "Options",
"id": "options",
"label": "Options",
"run": [Function],
"testId": "discoverOptionsButton",
},
Object {
"description": "New Search",
"id": "new",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SavedSearch } from '../../../saved_searches';
import { onSaveSearch } from './on_save_search';
import { GetStateReturn } from '../../angular/discover_state';
import { IndexPattern, ISearchSource } from '../../../kibana_services';
import { openOptionsPopover } from './open_options_popover';

/**
* Helper function to build the top nav links
Expand All @@ -38,6 +39,22 @@ export const getTopNavLinks = ({
onOpenInspector: () => void;
searchSource: ISearchSource;
}) => {
const options = {
id: 'options',
label: i18n.translate('discover.localMenu.localMenu.optionsTitle', {
defaultMessage: 'Options',
}),
description: i18n.translate('discover.localMenu.optionsDescription', {
defaultMessage: 'Options',
}),
run: (anchorElement: HTMLElement) =>
openOptionsPopover({
I18nContext: services.core.i18n.Context,
anchorElement,
}),
testId: 'discoverOptionsButton',
};

const newSearch = {
id: 'new',
label: i18n.translate('discover.localMenu.localMenu.newSearchTitle', {
Expand Down Expand Up @@ -128,6 +145,7 @@ export const getTopNavLinks = ({
};

return [
...(services.capabilities.advancedSettings.save ? [options] : []),
newSearch,
...(services.capabilities.discover.save ? [saveSearch] : []),
openSearch,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$dscOptionsPopoverWidth: $euiSizeL * 12;

.dscOptionsPopover {
width: $dscOptionsPopoverWidth;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { mountWithIntl } from '@kbn/test/jest';
import { findTestSubject } from '@elastic/eui/lib/test';
import { getServices } from '../../../kibana_services';

jest.mock('../../../kibana_services', () => {
const mockUiSettings = new Map();
return {
getServices: () => ({
core: {
uiSettings: {
get: (key: string) => {
return mockUiSettings.get(key);
},
set: (key: string, value: boolean) => {
mockUiSettings.set(key, value);
},
},
},
addBasePath: (path: string) => path,
}),
};
});

import { OptionsPopover } from './open_options_popover';

test('should display the correct text if datagrid is selected', () => {
const element = document.createElement('div');
const component = mountWithIntl(<OptionsPopover onClose={jest.fn()} anchorElement={element} />);
expect(findTestSubject(component, 'docTableMode').text()).toBe('Legacy table');
});

test('should display the correct text if legacy table is selected', () => {
const {
core: { uiSettings },
} = getServices();
uiSettings.set('doc_table:legacy', true);
const element = document.createElement('div');
const component = mountWithIntl(<OptionsPopover onClose={jest.fn()} anchorElement={element} />);
expect(findTestSubject(component, 'docTableMode').text()).toBe('Data grid');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import ReactDOM from 'react-dom';
import { I18nStart } from 'kibana/public';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiSpacer, EuiButton, EuiText, EuiWrappingPopover, EuiCode } from '@elastic/eui';
import { getServices } from '../../../kibana_services';
import './open_options_popover.scss';

let isOpen = false;

interface OptionsPopoverProps {
onClose: () => void;
anchorElement: HTMLElement;
}

export function OptionsPopover(props: OptionsPopoverProps) {
const {
core: { uiSettings },
addBasePath,
} = getServices();
const isLegacy = uiSettings.get('doc_table:legacy');

const mode = isLegacy
? i18n.translate('discover.openOptionsPopover.dataGridText', {
defaultMessage: 'Data grid',
})
: i18n.translate('discover.openOptionsPopover.legacyTableText', {
defaultMessage: 'Legacy table',
});
Copy link
Member

@kertal kertal Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be inverted to like (same with tests then)

isLegacy ? 'Legacy table' : 'Data grid'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are so right Matthias 😱

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :D


return (
<EuiWrappingPopover ownFocus button={props.anchorElement} closePopover={props.onClose} isOpen>
<div className="dscOptionsPopover">
<EuiText color="subdued" size="s">
<p>
<strong>Current view mode:</strong>{' '}
<EuiCode data-test-subj="docTableMode">{mode}</EuiCode>
</p>
</EuiText>
<EuiSpacer size="s" />
<EuiText color="subdued" size="s">
<FormattedMessage
id="discover.topNav.openOptionsPopover.description"
defaultMessage="The new data grid layout includes better data sorting, drag-and-drop columns, and a full
screen view. Toggle between data grid and legacy mode in Advanced Settings."
/>
</EuiText>
<EuiSpacer />
<EuiButton
iconType="tableDensityNormal"
fullWidth
href={addBasePath('/app/management/kibana/settings?query=Use legacy table')}
>
{i18n.translate('discover.openOptionsPopover.goToAdvancedSettings', {
defaultMessage: 'Go to Advanced Settings',
})}
</EuiButton>
</div>
</EuiWrappingPopover>
);
}

export function openOptionsPopover({
I18nContext,
anchorElement,
}: {
I18nContext: I18nStart['Context'];
anchorElement: HTMLElement;
}) {
if (isOpen) {
return;
}

isOpen = true;
const container = document.createElement('div');
const onClose = () => {
ReactDOM.unmountComponentAtNode(container);
document.body.removeChild(container);
isOpen = false;
};

document.body.appendChild(container);

const element = (
<I18nContext>
<OptionsPopover onClose={onClose} anchorElement={anchorElement} />
</I18nContext>
);
ReactDOM.render(element, container);
}