Skip to content

Commit

Permalink
[8.13] [Discover] Support grid column settings on Surrounding Documen…
Browse files Browse the repository at this point in the history
…ts page (elastic#177003) (elastic#177231)

# Backport

This will backport the following commits from `main` to `8.13`:
- [[Discover] Support grid column settings on Surrounding Documents page
(elastic#177003)](elastic#177003)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Julia
Rechkunova","email":"julia.rechkunova@elastic.co"},"sourceCommit":{"committedDate":"2024-02-19T18:56:05Z","message":"[Discover]
Support grid column settings on Surrounding Documents page
(elastic#177003)\n\n- Closes
https://github.com/elastic/kibana/issues/174485\r\n\r\n###
Summary\r\n\r\nThis PR keeps track of column resizing on Surrounding
documents page: if\r\nuser resizes columns and thne loads more
documents, the grid will\r\nrerender accordingly and it will keep custom
column widths.\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"a482a7a98fc16a4efad27eac38dd67eafe4b64a6","branchLabelMapping":{"^v8.14.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:DataDiscovery","backport:prev-minor","v8.14.0"],"title":"[Discover]
Support grid column settings on Surrounding Documents
page","number":177003,"url":"https://github.com/elastic/kibana/pull/177003","mergeCommit":{"message":"[Discover]
Support grid column settings on Surrounding Documents page
(elastic#177003)\n\n- Closes
https://github.com/elastic/kibana/issues/174485\r\n\r\n###
Summary\r\n\r\nThis PR keeps track of column resizing on Surrounding
documents page: if\r\nuser resizes columns and thne loads more
documents, the grid will\r\nrerender accordingly and it will keep custom
column widths.\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"a482a7a98fc16a4efad27eac38dd67eafe4b64a6"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.14.0","branchLabelMappingKey":"^v8.14.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/177003","number":177003,"mergeCommit":{"message":"[Discover]
Support grid column settings on Surrounding Documents page
(elastic#177003)\n\n- Closes
https://github.com/elastic/kibana/issues/174485\r\n\r\n###
Summary\r\n\r\nThis PR keeps track of column resizing on Surrounding
documents page: if\r\nuser resizes columns and thne loads more
documents, the grid will\r\nrerender accordingly and it will keep custom
column widths.\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"a482a7a98fc16a4efad27eac38dd67eafe4b64a6"}}]}]
BACKPORT-->

Co-authored-by: Julia Rechkunova <julia.rechkunova@elastic.co>
  • Loading branch information
kibanamachine and jughosta authored Feb 19, 2024
1 parent 1dc7a70 commit 08c27fd
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { DataView } from '@kbn/data-views-plugin/public';
import { SortDirection } from '@kbn/data-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { CellActionsProvider } from '@kbn/cell-actions';
import type { DiscoverGridSettings } from '@kbn/saved-search-plugin/common';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import {
type SearchResponseWarning,
Expand All @@ -38,6 +39,7 @@ import { MAX_CONTEXT_SIZE, MIN_CONTEXT_SIZE } from './services/constants';
import { DocTableContext } from '../../components/doc_table/doc_table_context';
import { useDiscoverServices } from '../../hooks/use_discover_services';
import { DiscoverGridFlyout } from '../../components/discover_grid_flyout';
import { onResizeGridColumn } from '../../utils/on_resize_grid_column';

export interface ContextAppContentProps {
columns: string[];
Expand Down Expand Up @@ -92,6 +94,7 @@ export function ContextAppContent({
}: ContextAppContentProps) {
const { uiSettings: config, uiActions } = useDiscoverServices();
const services = useDiscoverServices();
const [gridSettings, setGridSettings] = useState<DiscoverGridSettings>();

const [expandedDoc, setExpandedDoc] = useState<DataTableRecord | undefined>();
const isAnchorLoading =
Expand Down Expand Up @@ -148,6 +151,15 @@ export function ContextAppContent({
[addFilter, dataView, onAddColumn, onRemoveColumn]
);

const onResize = useCallback(
(colSettings) => {
setGridSettings((currentGridSettings) =>
onResizeGridColumn(colSettings, currentGridSettings)
);
},
[setGridSettings]
);

return (
<Fragment>
<WrapperWithPadding>
Expand Down Expand Up @@ -209,6 +221,8 @@ export function ContextAppContent({
renderDocumentView={renderDocumentView}
services={services}
configHeaderRowHeight={3}
settings={gridSettings}
onResize={onResize}
/>
</CellActionsProvider>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import { useSavedSearchInitial } from '../../services/discover_state_provider';
import { useFetchMoreRecords } from './use_fetch_more_records';
import { SelectedVSAvailableCallout } from './selected_vs_available_callout';
import { useDiscoverCustomization } from '../../../../customizations';
import { onResizeGridColumn } from '../../../../utils/on_resize_grid_column';

const containerStyles = css`
position: relative;
Expand All @@ -86,12 +87,7 @@ export const onResize = (
stateContainer: DiscoverStateContainer
) => {
const state = stateContainer.appState.getState();
const grid = { ...(state.grid || {}) };
const newColumns = { ...(grid.columns || {}) };
newColumns[colSettings.columnId] = {
width: Math.round(colSettings.width),
};
const newGrid = { ...grid, columns: newColumns };
const newGrid = onResizeGridColumn(colSettings, state.grid);
stateContainer.appState.update({ grid: newGrid });
};

Expand Down
56 changes: 56 additions & 0 deletions src/plugins/discover/public/utils/on_resize_grid_column.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 { onResizeGridColumn } from './on_resize_grid_column';

describe('Discover onResizeGridColumn', () => {
test('should handle empty initial value', () => {
const newGrid = onResizeGridColumn(
{
columnId: 'test',
width: 50,
},
undefined
);

expect(newGrid).toMatchInlineSnapshot(`
Object {
"columns": Object {
"test": Object {
"width": 50,
},
},
}
`);
});

test('should set rounded width to state on resize column', () => {
const grid = { columns: { timestamp: { width: 173 }, someField: { width: 197 } } };

const newGrid = onResizeGridColumn(
{
columnId: 'someField',
width: 205.5435345534,
},
grid
);

expect(newGrid).toMatchInlineSnapshot(`
Object {
"columns": Object {
"someField": Object {
"width": 206,
},
"timestamp": Object {
"width": 173,
},
},
}
`);
});
});
21 changes: 21 additions & 0 deletions src/plugins/discover/public/utils/on_resize_grid_column.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 type { DiscoverGridSettings } from '@kbn/saved-search-plugin/common';

export const onResizeGridColumn = (
colSettings: { columnId: string; width: number },
gridState: DiscoverGridSettings | undefined
): DiscoverGridSettings => {
const grid = { ...(gridState || {}) };
const newColumns = { ...(grid.columns || {}) };
newColumns[colSettings.columnId] = {
width: Math.round(colSettings.width),
};
return { ...grid, columns: newColumns };
};

0 comments on commit 08c27fd

Please sign in to comment.