Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseValueSuggestions hook #3506

Merged
merged 6 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
};