Skip to content

Commit

Permalink
simplify investigation fields hook
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliidm committed Mar 6, 2024
1 parent 9626eb9 commit c4649d3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ const StepAboutRuleComponent: FC<StepAboutRuleProps> = ({
);

const { investigationFields, isLoading: isInvestigationFieldsLoading } = useInvestigationFields({
esqlQuery,
isEsqlRule: isEsqlRuleValue,
esqlQuery: isEsqlRuleValue ? esqlQuery : undefined,
indexPatternsFields: indexPattern.fields,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe('useInvestigationFields', () => {
const { result } = renderHook(
() =>
useInvestigationFields({
isEsqlRule: true,
esqlQuery: mockEsqlQuery,
indexPatternsFields: mockIndexPatternFields,
}),
Expand All @@ -69,7 +68,6 @@ describe('useInvestigationFields', () => {
const { result, waitForNextUpdate } = renderHook(
() =>
useInvestigationFields({
isEsqlRule: true,
esqlQuery: '',
indexPatternsFields: mockIndexPatternFields,
}),
Expand All @@ -81,12 +79,11 @@ describe('useInvestigationFields', () => {
expect(result.current.investigationFields).toEqual(mockIndexPatternFields);
});

it('should return only index pattern fields when rule type is not ES|QL', async () => {
it('should return only index pattern fields when ES|QL query is undefined', async () => {
const { result } = renderHook(
() =>
useInvestigationFields({
isEsqlRule: false,
esqlQuery: mockEsqlQuery,
esqlQuery: undefined,
indexPatternsFields: mockIndexPatternFields,
}),
{ wrapper }
Expand All @@ -101,7 +98,6 @@ describe('useInvestigationFields', () => {
const { result } = renderHook(
() =>
useInvestigationFields({
isEsqlRule: true,
esqlQuery: mockEsqlQuery,
indexPatternsFields: mockIndexPatternFields,
}),
Expand All @@ -123,7 +119,6 @@ describe('useInvestigationFields', () => {
const { result } = renderHook(
() =>
useInvestigationFields({
isEsqlRule: true,
esqlQuery: mockEsqlQuery,
indexPatternsFields: mockIndexPatternFields,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const useEsqlFields: UseEsqlFields = (esqlQuery) => {
};

type UseInvestigationFields = (params: {
isEsqlRule: boolean;
esqlQuery: string | undefined;
indexPatternsFields: DataViewFieldBase[];
}) => {
Expand All @@ -68,14 +67,13 @@ type UseInvestigationFields = (params: {
};

export const useInvestigationFields: UseInvestigationFields = ({
isEsqlRule,
esqlQuery,
indexPatternsFields,
}) => {
const { fields: esqlFields, isLoading } = useEsqlFields(esqlQuery);

const investigationFields = useMemo(() => {
if (!esqlQuery || !isEsqlRule) {
if (!esqlQuery) {
return indexPatternsFields;
}

Expand All @@ -84,7 +82,7 @@ export const useInvestigationFields: UseInvestigationFields = ({
const isEsqlQueryAggregating = computeIsESQLQueryAggregating(esqlQuery);

return isEsqlQueryAggregating ? esqlFields : [...esqlFields, ...indexPatternsFields];
}, [esqlFields, esqlQuery, indexPatternsFields, isEsqlRule]);
}, [esqlFields, esqlQuery, indexPatternsFields]);

return {
investigationFields,
Expand Down

0 comments on commit c4649d3

Please sign in to comment.