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 2 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,50 @@
/*
* 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 { shallow } from 'enzyme';
import { EuiLink } from '@elastic/eui';
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 contain a link with the correct text if datagrid is selected', () => {
const element = document.createElement('div');
const component = shallow(<OptionsPopover onClose={jest.fn()} anchorElement={element} />);
expect(component.find(EuiLink).length).toBe(1);
expect(component.find(EuiLink).text()).toBe('Switch to legacy table');
});

test('should contain a link with 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 = shallow(<OptionsPopover onClose={jest.fn()} anchorElement={element} />);
expect(component.find(EuiLink).text()).toBe('Switch to data grid');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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, EuiLink, EuiText, EuiWrappingPopover } 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 linkText = isLegacy
? i18n.translate('discover.openOptionsPopover.switchToDataGridText', {
defaultMessage: 'Switch to data grid',
})
: i18n.translate('discover.openOptionsPopover.switchToLegacyText', {
defaultMessage: 'Switch to legacy table',
});

return (
<EuiWrappingPopover ownFocus button={props.anchorElement} closePopover={props.onClose} isOpen>
<div className="dscOptionsPopover">
<EuiLink href={addBasePath('/app/management/kibana/settings?query=Use legacy table')}>
{linkText}
</EuiLink>
<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. Enable this option if you prefer to fall back to the legacy table."
/>
Copy link
Member

@kertal kertal Apr 15, 2021

Choose a reason for hiding this comment

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

thx a lot for taking care of this 🙇 , so here's my first thought while playing with this: I think also the link description should be different depending on which state is selected, FYI @gchaps & @shaunmcgough for finding the right words here

Choose a reason for hiding this comment

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

Perhaps: "The new data grid features faster loading, better comparison, a flyout window, enhanced options, and is going to replace the legacy table soon. Enable this option if you prefer to fall back to the legacy table."

@gchaps what say you?

</EuiText>
</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);
}