Skip to content

Commit

Permalink
[Log Explorer] Hide hard coded flyout actions using customization ext…
Browse files Browse the repository at this point in the history
…ension point (#166638)

## 📓 Summary

Closes #165217 

This work implements a new extension point in the Discover customization
framework to allow customizing the flyout content. Although it enables
customizing only the actions displayed on top, it could be enhanced for
additional flyout customizations in the future.

To keep it simple and familiar to other customizations, it relies on a
similar API as the `top_nav` customization, allowing one to disable
default actions and insert additional ones.

```ts
/**
 * Hide flyout actions to prevent rendering hard-coded actions.
 */
customizations.set({
  id: 'flyout',
  actions: {
    defaultActions: {
      viewSingleDocument: { disabled: true },
      viewSurroundingDocument: { disabled: true },
    },
  },
});
```

## 🧪 Testing 
- Navigate to `/app/observability-log-explorer` and expand the flyout
for any grid entry.
The actions on top of the flyout should not be displayed, only the title
and the pagination control should appear.
- Navigate to `/app/discover` and expand the flyout for any grid entry. 
The actions for viewing a single documents or surrounding documents
should be displayed.

---------

Co-authored-by: Marco Antonio Ghiani <[email protected]>
  • Loading branch information
tonyghiani and Marco Antonio Ghiani authored Sep 21, 2023
1 parent c4a0ba2 commit 0743a11
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ import { act } from 'react-dom/test-utils';
import { ReactWrapper } from 'enzyme';
import { setUnifiedDocViewerServices } from '@kbn/unified-doc-viewer-plugin/public/plugin';
import { mockUnifiedDocViewerServices } from '@kbn/unified-doc-viewer-plugin/public/__mocks__';
import { FlyoutCustomization, useDiscoverCustomization } from '../../customizations';
import { EuiFlexItem } from '@elastic/eui';

const mockFlyoutCustomization: FlyoutCustomization = {
id: 'flyout',
actions: {},
};

jest.mock('../../customizations', () => ({
...jest.requireActual('../../customizations'),
useDiscoverCustomization: jest.fn(),
}));

const waitNextTick = () => new Promise((resolve) => setTimeout(resolve, 0));

Expand Down Expand Up @@ -94,6 +106,13 @@ describe('Discover flyout', function () {
return { component, props };
};

beforeEach(() => {
mockFlyoutCustomization.actions.defaultActions = undefined;
jest.clearAllMocks();

(useDiscoverCustomization as jest.Mock).mockImplementation(() => mockFlyoutCustomization);
});

it('should be rendered correctly using an data view without timefield', async () => {
const { component, props } = await mountComponent({});

Expand Down Expand Up @@ -206,4 +225,45 @@ describe('Discover flyout', function () {
const flyoutTitle = findTestSubject(component, 'docTableRowDetailsTitle');
expect(flyoutTitle.text()).toBe('Expanded row');
});

describe('when customizations actions exists', () => {
it('should display actions added by getActionItems', async () => {
mockFlyoutCustomization.actions = {
getActionItems: jest.fn(() => [
{
id: 'action-item-1',
enabled: true,
Content: () => <EuiFlexItem data-test-subj="customActionItem1">Action 1</EuiFlexItem>,
},
{
id: 'action-item-2',
enabled: true,
Content: () => <EuiFlexItem data-test-subj="customActionItem2">Action 2</EuiFlexItem>,
},
]),
};

const { component } = await mountComponent({});

const action1 = findTestSubject(component, 'customActionItem1');
const action2 = findTestSubject(component, 'customActionItem2');

expect(action1.text()).toBe('Action 1');
expect(action2.text()).toBe('Action 2');
});

it('should allow disabling default actions', async () => {
mockFlyoutCustomization.actions = {
defaultActions: {
viewSingleDocument: { disabled: true },
viewSurroundingDocument: { disabled: true },
},
};

const { component } = await mountComponent({});

const singleDocumentView = findTestSubject(component, 'docTableRowAction');
expect(singleDocumentView.length).toBeFalsy();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@ import {
EuiFlyout,
EuiFlyoutBody,
EuiFlyoutHeader,
EuiIconTip,
EuiTitle,
EuiButtonEmpty,
EuiText,
EuiSpacer,
EuiPortal,
EuiPagination,
EuiHideFor,
keys,
} from '@elastic/eui';
import type { Filter, Query, AggregateQuery } from '@kbn/es-query';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import type { DocViewFilterFn } from '@kbn/unified-doc-viewer/types';
import { UnifiedDocViewer } from '@kbn/unified-doc-viewer-plugin/public';
import { useNavigationProps } from '../../hooks/use_navigation_props';
import { useDiscoverServices } from '../../hooks/use_discover_services';
import { isTextBasedQuery } from '../../application/main/utils/is_text_based_query';
import { useFlyoutActions } from './use_flyout_actions';

export interface DiscoverGridFlyoutProps {
savedSearchId?: string;
Expand Down Expand Up @@ -104,9 +100,14 @@ export function DiscoverGridFlyout({
[activePage, setPage]
);

const { singleDocHref, contextViewHref, onOpenSingleDoc, onOpenContextView } = useNavigationProps(
{ dataView, rowIndex: hit.raw._index, rowId: hit.raw._id, columns, filters, savedSearchId }
);
const { flyoutActions } = useFlyoutActions({
dataView,
rowIndex: hit.raw._index,
rowId: hit.raw._id,
columns,
filters,
savedSearchId,
});

return (
<EuiPortal>
Expand Down Expand Up @@ -136,77 +137,8 @@ export function DiscoverGridFlyout({

<EuiSpacer size="s" />
<EuiFlexGroup responsive={false} gutterSize="s" alignItems="center">
{!isPlainRecord && (
<>
<EuiHideFor sizes={['xs', 's', 'm']}>
<EuiFlexItem grow={false}>
<EuiText size="s">
<strong>
{i18n.translate('discover.grid.tableRow.viewText', {
defaultMessage: 'View:',
})}
</strong>
</EuiText>
</EuiFlexItem>
</EuiHideFor>
<EuiFlexItem grow={false}>
{/* eslint-disable-next-line @elastic/eui/href-or-on-click */}
<EuiButtonEmpty
size="s"
iconSize="s"
iconType="document"
flush="left"
data-test-subj="docTableRowAction"
href={singleDocHref}
onClick={onOpenSingleDoc}
>
{i18n.translate('discover.grid.tableRow.viewSingleDocumentLinkTextSimple', {
defaultMessage: 'Single document',
})}
</EuiButtonEmpty>
</EuiFlexItem>
{dataView.isTimeBased() && dataView.id && (
<EuiFlexGroup alignItems="center" responsive={false} gutterSize="none">
<EuiFlexItem grow={false}>
{/* eslint-disable-next-line @elastic/eui/href-or-on-click */}
<EuiButtonEmpty
size="s"
iconSize="s"
iconType="documents"
flush="left"
onClick={onOpenContextView}
href={contextViewHref}
data-test-subj="docTableRowAction"
>
{i18n.translate(
'discover.grid.tableRow.viewSurroundingDocumentsLinkTextSimple',
{
defaultMessage: 'Surrounding documents',
}
)}
</EuiButtonEmpty>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiIconTip
content={i18n.translate(
'discover.grid.tableRow.viewSurroundingDocumentsHover',
{
defaultMessage:
'Inspect documents that occurred before and after this document. Only pinned filters remain active in the Surrounding documents view.',
}
)}
type="questionInCircle"
color="subdued"
position="right"
iconProps={{
className: 'eui-alignTop',
}}
/>
</EuiFlexItem>
</EuiFlexGroup>
)}
</>
)}
{!isPlainRecord &&
flyoutActions.map((action) => action.enabled && <action.Content key={action.id} />)}
{activePage !== -1 && (
<EuiFlexItem data-test-subj={`dscDocNavigationPage-${activePage}`}>
<EuiPagination
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* 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 {
EuiButtonEmpty,
EuiFlexGroup,
EuiFlexItem,
EuiHideFor,
EuiIconTip,
EuiText,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { useDiscoverCustomization } from '../../customizations';
import { UseNavigationProps, useNavigationProps } from '../../hooks/use_navigation_props';

interface FlyoutActionProps {
onClick: React.MouseEventHandler<Element>;
href: string;
}

const staticViewDocumentItem = {
id: 'viewDocument',
enabled: true,
Content: () => <ViewDocument />,
};

export const useFlyoutActions = (navigationProps: UseNavigationProps) => {
const { dataView } = navigationProps;
const { singleDocHref, contextViewHref, onOpenSingleDoc, onOpenContextView } =
useNavigationProps(navigationProps);

const flyoutCustomization = useDiscoverCustomization('flyout');

const {
viewSingleDocument = { disabled: false },
viewSurroundingDocument = { disabled: false },
} = flyoutCustomization?.actions?.defaultActions ?? {};
const customActions = [...(flyoutCustomization?.actions?.getActionItems?.() ?? [])];

const flyoutActions = [
{
id: 'singleDocument',
enabled: !viewSingleDocument.disabled,
Content: () => <SingleDocument href={singleDocHref} onClick={onOpenSingleDoc} />,
},
{
id: 'surroundingDocument',
enabled: Boolean(!viewSurroundingDocument.disabled && dataView.isTimeBased() && dataView.id),
Content: () => <SurroundingDocuments onClick={onOpenContextView} href={contextViewHref} />,
},
...customActions,
];

const hasEnabledActions = flyoutActions.some((action) => action.enabled);

if (hasEnabledActions) {
flyoutActions.unshift(staticViewDocumentItem);
}

return { flyoutActions, hasEnabledActions };
};

const ViewDocument = () => {
return (
<EuiHideFor sizes={['xs', 's', 'm']}>
<EuiFlexItem grow={false}>
<EuiText size="s">
<strong>
{i18n.translate('discover.grid.tableRow.viewText', {
defaultMessage: 'View:',
})}
</strong>
</EuiText>
</EuiFlexItem>
</EuiHideFor>
);
};

const SingleDocument = (props: FlyoutActionProps) => {
return (
<EuiFlexItem grow={false}>
<EuiButtonEmpty
size="s"
iconSize="s"
iconType="document"
flush="left"
data-test-subj="docTableRowAction"
{...props}
>
{i18n.translate('discover.grid.tableRow.viewSingleDocumentLinkTextSimple', {
defaultMessage: 'Single document',
})}
</EuiButtonEmpty>
</EuiFlexItem>
);
};

const SurroundingDocuments = (props: FlyoutActionProps) => {
return (
<EuiFlexGroup alignItems="center" responsive={false} gutterSize="none">
<EuiFlexItem grow={false}>
<EuiButtonEmpty
size="s"
iconSize="s"
iconType="documents"
flush="left"
data-test-subj="docTableRowAction"
{...props}
>
{i18n.translate('discover.grid.tableRow.viewSurroundingDocumentsLinkTextSimple', {
defaultMessage: 'Surrounding documents',
})}
</EuiButtonEmpty>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiIconTip
content={i18n.translate('discover.grid.tableRow.viewSurroundingDocumentsHover', {
defaultMessage:
'Inspect documents that occurred before and after this document. Only pinned filters remain active in the Surrounding documents view.',
})}
type="questionInCircle"
color="subdued"
position="right"
iconProps={{
className: 'eui-alignTop',
}}
/>
</EuiFlexItem>
</EuiFlexGroup>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

import { filter, map, Observable, startWith, Subject } from 'rxjs';
import type {
FlyoutCustomization,
SearchBarCustomization,
TopNavCustomization,
UnifiedHistogramCustomization,
} from './customization_types';

export type DiscoverCustomization =
| FlyoutCustomization
| SearchBarCustomization
| TopNavCustomization
| UnifiedHistogramCustomization;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

export interface FlyoutDefaultActionItem {
disabled?: boolean;
}

export interface FlyoutDefaultActions {
viewSingleDocument?: FlyoutDefaultActionItem;
viewSurroundingDocument?: FlyoutDefaultActionItem;
}

export interface FlyoutActionItem {
id: string;
Content: React.ElementType;
enabled: boolean;
}

export interface FlyoutCustomization {
id: 'flyout';
actions: {
defaultActions?: FlyoutDefaultActions;
getActionItems?: () => FlyoutActionItem[];
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

export * from './flyout_customization';
export * from './search_bar_customization';
export * from './top_nav_customization';
export * from './histogram_customization';
Loading

0 comments on commit 0743a11

Please sign in to comment.