Skip to content

Commit

Permalink
fix selection
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc committed May 18, 2023
1 parent b743ea0 commit eb0b111
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, { useEffect, useState, useCallback } from 'react';
import React, { useEffect, useState, useCallback, useMemo } from 'react';
import type { MouseEvent } from 'react';
import { Query, EuiFlexGroup, EuiFlexItem, EuiText, EuiHealth, EuiBadge } from '@elastic/eui';
import type { FieldValueOptionType } from '@elastic/eui';
Expand Down Expand Up @@ -39,6 +39,12 @@ export interface Params {
fixedTagReferences?: Tag[] | null;
}

const getTotalActiveFilters = (options: TagOptionItem[], fixedTagReferences?: Tag[] | null) =>
Math.max(
fixedTagReferences?.length ?? 0,
options.filter((option) => option.checked === 'on').length ?? 0
);

export const useTagFilterPanel = ({
query,
tagsToTableItemMap,
Expand All @@ -55,7 +61,10 @@ export const useTagFilterPanel = ({
const [isInUse, setIsInUse] = useState(false);
const [options, setOptions] = useState<TagOptionItem[]>([]);
const [tagSelection, setTagSelection] = useState<TagSelection>({});
const totalActiveFilters = Object.keys(tagSelection).length;
const totalActiveFilters = useMemo(
() => getTotalActiveFilters(options, fixedTagReferences),
[fixedTagReferences, options]
);

const onSelectChange = useCallback(
(updatedOptions: TagOptionItem[]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,9 @@ describe('TableListView', () => {
await act(async () => {
testBed = await setupTagFiltering({
findItems,
fixedTagReferences: [{ id: 'id-tag-1', name: 'tag-1', description: '', color: '' }],
fixedTagReferences: [
{ id: 'id-tag-1', name: 'tag-1', description: '', color: '', type: 'tag' },
],
});
});

Expand All @@ -758,7 +760,9 @@ describe('TableListView', () => {
await act(async () => {
testBed = await setupTagFiltering({
findItems,
fixedTagReferences: [{ id: 'id-tag-1', name: 'tag-1', description: '', color: '' }],
fixedTagReferences: [
{ id: 'id-tag-1', name: 'tag-1', description: '', color: '', type: 'tag' },
],
});
});

Expand Down
32 changes: 24 additions & 8 deletions packages/content-management/table_list/src/table_list_view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { getReducer } from './reducer';
import type { SortColumnField } from './components';
import { useTags } from './use_tags';
import { useInRouterContext, useUrlState } from './use_url_state';
import { RowActions, TableItemsRowActions, Tag } from './types';
import { RowActions, TableItemsRowActions, TagReference } from './types';

interface ContentEditorConfig
extends Pick<OpenContentEditorParams, 'isReadonly' | 'onSave' | 'customValidators'> {
Expand Down Expand Up @@ -108,7 +108,7 @@ export interface Props<T extends UserContentCommonSchema = UserContentCommonSche
contentEditor?: ContentEditorConfig;
restrictPageSectionWidth?: boolean;
pageSectionPadding?: EuiPaddingSize;
fixedTagReferences?: Tag[] | null;
fixedTagReferences?: TagReference[] | null;
}

export interface State<T extends UserContentCommonSchema = UserContentCommonSchema> {
Expand Down Expand Up @@ -244,16 +244,32 @@ const appendQuery = (q: Query, tagName: string) => {
return q.addOrFieldValue('tag', tagName, true, 'eq');
};

const getDefaultQuery = (initialQuery: string, fixedTagReferences: Tag[] | null | undefined) => {
const getDefaultQuery = (
initialQuery: string,
fixedTagReferences: TagReference[] | null | undefined
) => {
const query = new Query(Ast.create([]), undefined, initialQuery);

const uniqueQueryArray = fixedTagReferences?.reduce<string[]>((acc, { name }) => {
if (name && acc.indexOf(name) === -1) {
acc.push(name);
}
return acc;
}, []);
return (
fixedTagReferences?.reduce((q, ref) => {
return appendQuery(q, ref.name);
uniqueQueryArray?.reduce((q, ref) => {
return appendQuery(q, ref);
}, query) ?? query
);
};

const getFindItemReference = (
references: SavedObjectsFindOptionsReference[] | undefined,
fixedTagReferences?: TagReference[] | null | undefined
): SavedObjectsFindOptionsReference[] | undefined => {
const fixedTagFindReferences = fixedTagReferences?.map(({ id, type }) => ({ id, type })) ?? [];
return [...(references ?? []), ...fixedTagFindReferences];
};

function TableListViewComp<T extends UserContentCommonSchema>({
additionalRightSideActions = [],
children,
Expand Down Expand Up @@ -404,7 +420,7 @@ function TableListViewComp<T extends UserContentCommonSchema>({
};

const response = await findItems(searchQueryParsed, {
references,
references: getFindItemReference(references, fixedTagReferences),
referencesToExclude,
});

Expand All @@ -426,7 +442,7 @@ function TableListViewComp<T extends UserContentCommonSchema>({
data: err,
});
}
}, [searchQueryParser, searchQuery.text, findItems]);
}, [searchQueryParser, searchQuery.text, findItems, fixedTagReferences]);

const updateQuery = useCallback(
(query: Query) => {
Expand Down
9 changes: 9 additions & 0 deletions packages/content-management/table_list/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ export interface Tag {
name: string;
description: string;
color: string;
type: string;
}

export interface TagReference {
id: string;
name: string;
description: string;
color: string;
type: string;
}

export type TableRowAction = 'delete';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ test('fixedTagReferences is passed through', async () => {
pluginServices.getServices().dashboardCapabilities.showWriteControls = false;

let component: ReactWrapper;
const fixedTagReferences = [{ id: 'mockTagId', name: 'mockTagName', description: '', color: '' }];
const fixedTagReferences = [
{ id: 'mockTagId', name: 'mockTagName', description: '', color: '', type: 'tag' },
];

await act(async () => {
({ component } = mountWith({ props: { fixedTagReferences } }));
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/saved_objects_tagging_oss/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
*/

export interface Tag {
color: string;
description: string;
id: string;
name: string;
description: string;
color: string;
type: string;
}

export interface TagAttributes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export const useFetchSecurityTags = () => {

const tagsResult = useMemo(() => {
if (tags?.length) {
return tags.map((t) => ({ id: t.id, ...t.attributes }));
return tags.map((t) => ({ id: t.id, type: 'tag', ...t.attributes }));
}
return tag ? [{ id: tag.id, ...tag.attributes }] : undefined;
return tag ? [{ id: tag.id, type: 'tag', ...tag.attributes }] : undefined;
}, [tags, tag]);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ const getInitialFilterString = (securityTags: Tag[] | null | undefined) => {
if (!securityTags) {
return;
}
const queryArray = securityTags?.reduce<string[]>((acc, { name }) => {
if (name) {
acc.push(`"${name}"`);
const uniqueQueryArray = securityTags?.reduce<string[]>((acc, { name }) => {
const nameString = `"${name}"`;
if (name && acc.indexOf(nameString) === -1) {
acc.push(nameString);
}
return acc;
}, []);

const query = [...new Set(queryArray)].join(' or');
const query = [uniqueQueryArray].join(' or');
return `tag:(${query})`;
};

Expand Down

0 comments on commit eb0b111

Please sign in to comment.