From b21fd49290d992f12774a06b8b53799706902c5c Mon Sep 17 00:00:00 2001 From: Luke G <11671118+lgestc@users.noreply.github.com> Date: Thu, 7 Mar 2024 15:27:39 +0100 Subject: [PATCH] [Security Solution] Fix broken columns reset in alerts table (#177986) ## Summary This fixes https://github.com/elastic/kibana/issues/173241 --- .../hooks/use_columns/use_columns.test.ts | 38 +++++++++++++++++++ .../hooks/use_columns/use_columns.ts | 7 ++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_columns/use_columns.test.ts b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_columns/use_columns.test.ts index c4472aefe1f2a..237fad28e3027 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_columns/use_columns.test.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_columns/use_columns.test.ts @@ -330,4 +330,42 @@ describe('useColumn', () => { ); }); }); + + describe('onResetColumns', () => { + test('should restore visible columns defaults', () => { + const localStorageAlertsTable = getStorageAlertsTableByDefaultColumns(defaultColumns); + const { result } = renderHook(() => + useColumns({ + defaultColumns, + featureIds, + id, + storageAlertsTable: localStorageAlertsTable, + storage, + }) + ); + + expect(result.current.visibleColumns).toEqual([ + 'event.action', + '@timestamp', + 'kibana.alert.duration.us', + 'kibana.alert.reason', + ]); + + act(() => { + result.current.onToggleColumn(defaultColumns[0].id); + }); + expect(result.current.visibleColumns).not.toContain(['event.action']); + + act(() => { + result.current.onResetColumns(); + }); + + expect(result.current.visibleColumns).toEqual([ + 'event.action', + '@timestamp', + 'kibana.alert.duration.us', + 'kibana.alert.reason', + ]); + }); + }); }); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_columns/use_columns.ts b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_columns/use_columns.ts index e7fba777a6fdb..b3ff16cfd89ef 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_columns/use_columns.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_columns/use_columns.ts @@ -256,10 +256,9 @@ export const useColumns = ({ const onResetColumns = useCallback(() => { const populatedDefaultColumns = populateColumns(defaultColumns, browserFields, defaultColumns); - setColumnsAndSave( - populatedDefaultColumns, - populatedDefaultColumns.map((pdc) => pdc.id) - ); + const newVisibleColumns = populatedDefaultColumns.map((pdc) => pdc.id); + setVisibleColumns(newVisibleColumns); + setColumnsAndSave(populatedDefaultColumns, newVisibleColumns); }, [browserFields, defaultColumns, setColumnsAndSave]); const onColumnResize = useCallback(