Skip to content

Commit

Permalink
fix: check categories field & optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
viajes7 committed Sep 24, 2024
1 parent 6c2c0c2 commit efc4981
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions packages/kbn-management/settings/application/hooks/use_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Ast, Query } from '@elastic/eui';
import { getFieldDefinitions } from '@kbn/management-settings-field-definition';
import { FieldDefinition } from '@kbn/management-settings-types';
import { UiSettingsScope } from '@kbn/core-ui-settings-common';
import { Clause } from '@elastic/eui/src/components/search_bar/query/ast';
import { useServices } from '../services';
import { useSettings } from './use_settings';

Expand All @@ -28,22 +29,20 @@ export const useFields = (scope: UiSettingsScope, query?: Query): FieldDefinitio
isCustom: (key) => isCustomSetting(key, scope),
isOverridden: (key) => isOverriddenSetting(key, scope),
});
if (query && fields.length) {
let ast = Ast.create([]);
if (query) {
const clauses: Clause[] = query.ast.clauses.map((clause) =>
// If the clause value contains `:` and is not a category filter, add it as a term clause
// This allows searching for settings that include `:` in their names
clause.type === 'field' && clause.field !== 'categories'
? {
type: 'term',
match: 'must',
value: `${clause.field}:${clause.value}`,
}
: clause
);

query.ast.clauses.forEach((clause) => {
if (clause.type !== 'field' || Object.keys(fields[0]).includes(clause.field)) {
ast = ast.addClause(clause);
} else {
ast = ast.addClause({
type: 'term',
match: 'must',
value: `${clause.field}:${clause.value}`,
});
}
});

return Query.execute(new Query(ast, undefined, query.text), fields);
return Query.execute(new Query(Ast.create(clauses), undefined, query.text), fields);
}
return fields;
};

0 comments on commit efc4981

Please sign in to comment.