Skip to content

Commit

Permalink
Merge pull request #3506 from wazuh/enhancement/3500_useValueSuggestions
Browse files Browse the repository at this point in the history
UseValueSuggestions hook
  • Loading branch information
mpRegalado authored Jul 28, 2021
2 parents 06a2358 + 75d7e22 commit 8f5794e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ All notable changes to the Wazuh app project will be documented in this file.
- Added try catch strategy with ErrorOrchestrator service on FIM & SCA sections [#3417](https://github.com/wazuh/wazuh-kibana-app/pull/3417)
- Added sample data for office365 events [#3424](https://github.com/wazuh/wazuh-kibana-app/pull/3424)
- Created a separate component to check for sample data [#3475](https://github.com/wazuh/wazuh-kibana-app/pull/3475)
- Added a new hook for getting value suggestions [#3506](https://github.com/wazuh/wazuh-kibana-app/pull/3506)

### Changed

Expand Down
1 change: 1 addition & 0 deletions public/components/common/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from './useApiRequest';
export * from './use-app-config';
export * from './useRootScope';
export * from './use_async_action';
export { useValueSuggestions, IValueSuggestiions } from './use-value-suggestions';
76 changes: 76 additions & 0 deletions public/components/common/hooks/use-value-suggestions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Wazuh app - React hook for getting value suggestions
* Copyright (C) 2015-2021 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import { useState, useEffect } from 'react';
import { getDataPlugin } from '../../../kibana-services';
import { useIndexPattern } from '.';
import { IFieldType, IIndexPattern } from 'src/plugins/data/public';
import React from 'react';
import {
UI_ERROR_SEVERITIES,
UIErrorLog,
UIErrorSeverity,
UILogLevel,
} from '../../../react-services/error-orchestrator/types';
import { UI_LOGGER_LEVELS } from '../../../../common/constants';
import { getErrorOrchestrator } from '../../../react-services/common-services';

export interface IValueSuggestiions {
suggestedValues: string[] | boolean[];
isLoading: boolean;
setQuery: React.Dispatch<React.SetStateAction<string>>;
}

export const useValueSuggestions = (filterField: string, type: 'string' | 'boolean' = 'string') : IValueSuggestiions => {
const [suggestedValues, setSuggestedValues] = useState<string[] | boolean[]>([]);
const [query, setQuery] = useState<string>('');
const [isLoading, setIsLoading] = useState(true);
const data = getDataPlugin();
const indexPattern = useIndexPattern();

useEffect(() => {
if (indexPattern) {
setIsLoading(true);
(async () => {
const field = {
type: type,
name: filterField,
aggregatable: true,
} as IFieldType;
try {
setSuggestedValues(
await data.autocomplete.getValueSuggestions({
query,
indexPattern: indexPattern as IIndexPattern,
field,
})
);
} catch (error) {
const options: UIErrorLog = {
context: `${useValueSuggestions.name}.valueSuggestions`,
level: UI_LOGGER_LEVELS.ERROR as UILogLevel,
severity: UI_ERROR_SEVERITIES.UI as UIErrorSeverity,
error: {
error,
message: error.message || error,
title: error.name,
},
};
getErrorOrchestrator().handleError(options);
} finally {
setIsLoading(false);
}
})();
}
}, [indexPattern, query, filterField, type]);

return { suggestedValues, isLoading, setQuery };
};

0 comments on commit 8f5794e

Please sign in to comment.