Skip to content

Commit

Permalink
[8.x] [Cloud Security] Fix for issue where date is not retained when …
Browse files Browse the repository at this point in the history
…opening new tab (elastic#205128) (elastic#205538)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Cloud Security] Fix for issue where date is not retained when
opening new tab
(elastic#205128)](elastic#205128)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Rickyanto
Ang","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-01-03T19:44:11Z","message":"[Cloud
Security] Fix for issue where date is not retained when opening new tab
(elastic#205128)\n\n## Summary\r\n\r\nThis PR addresses the issue where when
user opens new tab by clicking on\r\npopout icon on Alerts Data grid on
Alerts preview Contextual flyout, the\r\ndate from original tab is not
retained, as such there are scenario where\r\nuser don't see any alerts
on new tab because its defaulted to today when\r\nthe alerts only exist
from view days
ago\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/58a30d7d-18b3-47a8-ab4b-2ce143583368","sha":"32dbd14519584b118861a209fdc841b011b5b5a0","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Cloud
Security","backport:prev-minor","v8.18.0"],"title":"[Cloud Security] Fix
for issue where date is not retained when opening new
tab","number":205128,"url":"https://github.com/elastic/kibana/pull/205128","mergeCommit":{"message":"[Cloud
Security] Fix for issue where date is not retained when opening new tab
(elastic#205128)\n\n## Summary\r\n\r\nThis PR addresses the issue where when
user opens new tab by clicking on\r\npopout icon on Alerts Data grid on
Alerts preview Contextual flyout, the\r\ndate from original tab is not
retained, as such there are scenario where\r\nuser don't see any alerts
on new tab because its defaulted to today when\r\nthe alerts only exist
from view days
ago\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/58a30d7d-18b3-47a8-ab4b-2ce143583368","sha":"32dbd14519584b118861a209fdc841b011b5b5a0"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/205128","number":205128,"mergeCommit":{"message":"[Cloud
Security] Fix for issue where date is not retained when opening new tab
(elastic#205128)\n\n## Summary\r\n\r\nThis PR addresses the issue where when
user opens new tab by clicking on\r\npopout icon on Alerts Data grid on
Alerts preview Contextual flyout, the\r\ndate from original tab is not
retained, as such there are scenario where\r\nuser don't see any alerts
on new tab because its defaulted to today when\r\nthe alerts only exist
from view days
ago\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/58a30d7d-18b3-47a8-ab4b-2ce143583368","sha":"32dbd14519584b118861a209fdc841b011b5b5a0"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Rickyanto Ang <[email protected]>
  • Loading branch information
kibanamachine and animehart authored Jan 3, 2025
1 parent 4994699 commit 2c63bdb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
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, EuiTableSortingType } 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 @@ -114,6 +116,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({
Expand Down Expand Up @@ -327,9 +339,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

0 comments on commit 2c63bdb

Please sign in to comment.