Skip to content

Commit

Permalink
SO Management: revert incorrect sorting on tags column (elastic#139999
Browse files Browse the repository at this point in the history
)

* SO Management: revert incorrect sorting on `tags` column

* remove incorrect comment

(cherry picked from commit 5c9a11a)
  • Loading branch information
pgayvallet committed Sep 8, 2022
1 parent ef51737 commit 941cf4d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class Table extends PureComponent<TableProps, TableState> {
);
},
} as EuiTableFieldDataColumnType<SavedObjectWithMetadata<any>>,
...(taggingApi ? [taggingApi.ui.getTableColumnDefinition()] : []),
...(taggingApi ? [taggingApi.ui.getTableColumnDefinition({ serverPaging: true })] : []),
...columnRegistry.getAll().map((column) => {
column.setColumnContext({ capabilities });
column.registerOnFinishCallback(() => {
Expand Down
26 changes: 25 additions & 1 deletion src/plugins/saved_objects_tagging_oss/public/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export interface SavedObjectsTaggingApiUi {
* )
* ```
*/
getTableColumnDefinition(): EuiTableFieldDataColumnType<SavedObject>;
getTableColumnDefinition(
options?: GetTableColumnDefinitionOptions
): EuiTableFieldDataColumnType<SavedObject>;

/**
* Convert given tag name to a {@link SavedObjectsFindOptionsReference | reference }
Expand Down Expand Up @@ -251,6 +253,28 @@ export interface SavedObjectSaveModalTagSelectorComponentProps {
onTagsSelected: (ids: string[]) => void;
}

/**
* Options for the {@link SavedObjectsTaggingApiUi.getTableColumnDefinition | getTableColumnDefinition api}
*
* @public
*/
export interface GetTableColumnDefinitionOptions {
/**
* By default, the `tags` column definition will be automatically sortable
* by tag name.
*
* However, when paging is performed on the server, we need to remove the sorting
* capability from the column to avoid unexpected behavior by triggering fetch request
* when sorting by column.
*
* Should be set to `true` when generating the definition for a table that performs
* server-side paging.
*
* Defaults to false.
*/
serverPaging?: boolean;
}

/**
* Options for the {@link SavedObjectsTaggingApiUi.getSearchBarFilter | getSearchBarFilter api}
*
Expand Down
1 change: 1 addition & 0 deletions src/plugins/saved_objects_tagging_oss/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type {
ParseSearchQueryOptions,
SavedObjectSaveModalTagSelectorComponentProps,
SavedObjectTagDecoratorTypeGuard,
GetTableColumnDefinitionOptions,
} from './api';

export type { TagDecoratedSavedObject } from './decorator';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ describe('getTableColumnDefinition', () => {
// we know this returns a function even if the generic column signature allows other types
expect((sortable as Function)(savedObject)).toEqual('Tag 1');
});

it('returns a non-sortable definition when `serverPaging` is `true`', () => {
const { sortable } = getTableColumnDefinition({ serverPaging: true });

expect(sortable).toEqual(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ import { SavedObject, SavedObjectReference } from '@kbn/core/public';
import {
SavedObjectsTaggingApiUi,
SavedObjectsTaggingApiUiComponent,
GetTableColumnDefinitionOptions,
} from '@kbn/saved-objects-tagging-oss-plugin/public';
import { ITagsCache } from '../services';
import { getTagsFromReferences, byNameTagSorter } from '../utils';

export interface GetTableColumnDefinitionOptions {
export interface BuildGetTableColumnDefinitionOptions {
components: SavedObjectsTaggingApiUiComponent;
cache: ITagsCache;
}

export const buildGetTableColumnDefinition = ({
components,
cache,
}: GetTableColumnDefinitionOptions): SavedObjectsTaggingApiUi['getTableColumnDefinition'] => {
return () => {
}: BuildGetTableColumnDefinitionOptions): SavedObjectsTaggingApiUi['getTableColumnDefinition'] => {
return ({ serverPaging = false }: GetTableColumnDefinitionOptions = {}) => {
return {
field: 'references',
name: i18n.translate('xpack.savedObjectsTagging.uiApi.table.columnTagsName', {
Expand All @@ -33,11 +34,13 @@ export const buildGetTableColumnDefinition = ({
description: i18n.translate('xpack.savedObjectsTagging.uiApi.table.columnTagsDescription', {
defaultMessage: 'Tags associated with this saved object',
}),
sortable: (object: SavedObject) => {
const { tags } = getTagsFromReferences(object.references, cache.getState());
tags.sort(byNameTagSorter);
return tags.length ? tags[0].name : undefined;
},
sortable: serverPaging
? false
: (object: SavedObject) => {
const { tags } = getTagsFromReferences(object.references, cache.getState());
tags.sort(byNameTagSorter);
return tags.length ? tags[0].name : undefined;
},
render: (references: SavedObjectReference[], object: SavedObject) => {
return <components.TagList object={object} />;
},
Expand Down

0 comments on commit 941cf4d

Please sign in to comment.