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

[Cloud Security] Fix for issue where date is not retained when opening new tab #205128

Merged
merged 6 commits into from
Jan 3, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import React, { memo, useCallback, useEffect, useState } from 'react';
import { encode } from '@kbn/rison';
import { capitalize } from 'lodash';
import type { Criteria, EuiBasicTableColumn } from '@elastic/eui';
import { EuiSpacer, EuiPanel, EuiText, EuiBasicTable, EuiIcon, EuiLink } from '@elastic/eui';
Expand All @@ -26,6 +27,7 @@ import {
OPEN_IN_ALERTS_TITLE_STATUS,
OPEN_IN_ALERTS_TITLE_USERNAME,
} from '../../../overview/components/detection_response/translations';
import { URL_PARAM_KEY } from '../../../common/hooks/use_url_state';
import { useNavigateToAlertsPageWithFilters } from '../../../common/hooks/use_navigate_to_alerts_page_with_filters';
import { DocumentDetailsPreviewPanelKey } from '../../../flyout/document_details/shared/constants/panel_keys';
import { useGlobalTime } from '../../../common/containers/use_global_time';
Expand Down Expand Up @@ -93,6 +95,16 @@ export const AlertsDetailsTable = memo(
};

const { to, from } = useGlobalTime();
const timerange = encode({
global: {
[URL_PARAM_KEY.timerange]: {
kind: 'absolute',
from,
to,
},
},
});

const { signalIndexName } = useSignalIndex();
const { data, setQuery } = useQueryAlerts({
query: buildEntityAlertsQuery(field, to, from, value, 500, ''),
Expand Down Expand Up @@ -254,9 +266,10 @@ export const AlertsDetailsTable = memo(
fieldName: 'kibana.alert.workflow_status',
},
],
true
true,
timerange
),
[field, openAlertsPageWithFilters, value]
[field, openAlertsPageWithFilters, timerange, value]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,54 @@ describe('useNavigateToAlertsPageWithFilters', () => {
expect.objectContaining({ deepLinkId: SecurityPageName.alerts })
);
});

it('navigates to alerts page in new tab', () => {
const filter = {
title: 'test filter',
selectedOptions: ['test value'],
fieldName: 'test field',
exclude: false,
existsSelected: false,
};
const openInNewTab = true;

const {
result: { current: navigateToAlertsPageWithFilters },
} = renderHook(() => useNavigateToAlertsPageWithFilters());

navigateToAlertsPageWithFilters(filter, openInNewTab);

expect(mockNavigateTo).toHaveBeenCalledWith({
deepLinkId: SecurityPageName.alerts,
path: "?pageFilters=!((exclude:!f,existsSelected:!f,fieldName:'test field',hideActionBar:!f,selectedOptions:!('test value'),title:'test filter'))",
openInNewTab: true,
});
});

it('navigates to alerts page with timerange', () => {
const filter = {
title: 'test filter',
selectedOptions: ['test value'],
fieldName: 'test field',
exclude: false,
existsSelected: false,
};

const timerange =
'(global:(timerange:(from:"2024-12-12T17:03:23.481Z",kind:absolute,to:"2025-01-04T07:59:59.999Z")))';

const openInNewTab = true;

const {
result: { current: navigateToAlertsPageWithFilters },
} = renderHook(() => useNavigateToAlertsPageWithFilters());

navigateToAlertsPageWithFilters(filter, openInNewTab, timerange);

expect(mockNavigateTo).toHaveBeenCalledWith({
deepLinkId: SecurityPageName.alerts,
path: `?pageFilters=!((exclude:!f,existsSelected:!f,fieldName:'test field',hideActionBar:!f,selectedOptions:!('test value'),title:'test filter'))&timerange=(global:(timerange:(from:"2024-12-12T17:03:23.481Z",kind:absolute,to:"2025-01-04T07:59:59.999Z")))`,
openInNewTab: true,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@ import { URL_PARAM_KEY } from './use_url_state';
export const useNavigateToAlertsPageWithFilters = () => {
const { navigateTo } = useNavigation();

return (filterItems: FilterControlConfig | FilterControlConfig[], openInNewTab = false) => {
return (
/**
* Pass one or more filter control configurations to be applied to the alerts page filters
*/
filterItems: FilterControlConfig | FilterControlConfig[],
/**
* If true, the alerts page will be opened in a new tab
*/
openInNewTab = false,
/**
* Allows to customize the timerange url parameter. Should only be used in combination with the openInNewTab=true parameter
*/
timerange?: string
) => {
const urlFilterParams = encode(
formatPageFilterSearchParam(Array.isArray(filterItems) ? filterItems : [filterItems])
);

const timerangePath = timerange ? `&timerange=${timerange}` : '';
navigateTo({
deepLinkId: SecurityPageName.alerts,
path: `?${URL_PARAM_KEY.pageFilter}=${urlFilterParams}`,
path: `?${URL_PARAM_KEY.pageFilter}=${urlFilterParams}${timerangePath}`,
openInNewTab,
});
};
Expand Down
Loading