From 8c8ab580d5098b4a0736b5e4f85a1d3719e6bf24 Mon Sep 17 00:00:00 2001 From: Amit Yathirajadasan Date: Thu, 2 May 2024 14:26:25 -0700 Subject: [PATCH] refactor: replace table with datatable in signal selection --- nerdlets/signal-selection/listing-table.js | 171 ++++++++++++--------- nerdlets/signal-selection/styles.scss | 8 +- 2 files changed, 107 insertions(+), 72 deletions(-) diff --git a/nerdlets/signal-selection/listing-table.js b/nerdlets/signal-selection/listing-table.js index ade9d99b..8a96f32c 100644 --- a/nerdlets/signal-selection/listing-table.js +++ b/nerdlets/signal-selection/listing-table.js @@ -1,13 +1,14 @@ -import React, { useCallback } from 'react'; +import React, { useCallback, useEffect, useRef, useState } from 'react'; import PropTypes from 'prop-types'; import { + DataTable, + DataTableHeader, + DataTableHeaderCell, + DataTableBody, + DataTableRow, + DataTableRowCell, EmptyState, - Table, - TableHeader, - TableHeaderCell, - TableRow, - TableRowCell, } from 'nr1'; import { SIGNAL_TYPES, UI_CONTENT } from '../../src/constants'; @@ -33,28 +34,46 @@ const ListingTable = ({ onLoadMore, onSelect, }) => { - const selectedHandler = useCallback( - ({ item: { guid } = {} }) => { - let selectionLookup = {}; - if (type === SIGNAL_TYPES.ENTITY) - selectionLookup = selectedEntities.reduce( - (acc, { guid }) => ({ ...acc, [guid]: null }), - {} - ); - if (type === SIGNAL_TYPES.ALERT) - selectionLookup = selectedAlerts.reduce( - (acc, { guid }) => ({ ...acc, [guid]: null }), - {} - ); - return guid in selectionLookup; + const [selection, setSelection] = useState({}); + const selectionsSet = useRef(); + + useEffect(() => { + let selectedItems, sel; + if (type === SIGNAL_TYPES.ENTITY) selectedItems = selectedEntities; + if (type === SIGNAL_TYPES.ALERT) selectedItems = selectedAlerts; + const selectionLookup = selectedItems.reduce( + (acc, { guid }) => ({ ...acc, [guid]: true }), + {} + ); + const selectionReducer = (acc, { guid }, idx) => + selectionLookup[guid] ? { ...acc, [idx]: true } : acc; + if (type === SIGNAL_TYPES.ENTITY) + sel = entities.reduce(selectionReducer, {}); + if (type === SIGNAL_TYPES.ALERT) sel = alerts.reduce(selectionReducer, {}); + if (sel) { + setSelection(sel); + selectionsSet.current = new Set(Object.keys(sel)); + } + }, [type, entities, alerts, selectedEntities, selectedAlerts]); + + const itemSelectionHandler = useCallback( + (sel) => { + const curSelectionsSet = new Set(Object.keys(sel)); + const added = curSelectionsSet.difference(selectionsSet.current); + const removed = selectionsSet.current.difference(curSelectionsSet); + selectionsSet.current = curSelectionsSet; + let items = []; + if (type === SIGNAL_TYPES.ENTITY) items = entities; + if (type === SIGNAL_TYPES.ALERT) items = alerts; + if (added.size === 1) { + onSelect?.(type, true, items[added.keys().next().value]); + } else if (removed.size === 1) { + onSelect?.(type, false, items[removed.keys().next().value]); + } }, - [type, selectedEntities, selectedAlerts] + [type, entities, alerts, onSelect] ); - const selectItemHandler = ({ target: { checked } = {} } = {}, { item }) => { - if (onSelect) onSelect(type, checked, item); - }; - if ( (type === SIGNAL_TYPES.ALERT && !alerts.length) || (type === SIGNAL_TYPES.ENTITY && !entities.length) @@ -63,56 +82,66 @@ const ListingTable = ({ if (type === SIGNAL_TYPES.ENTITY) return ( - - - item?.name}> - Name - - item?.type}> - Entity type - - item?.account?.id}> - Account - - - {({ item }) => ( - - {item?.name} - - {item?.domain}/{item?.type} - - - {item?.account?.name} - {item?.account?.id} - - - )} -
+
+ + + + Name + + + Entity type + + + + {() => ( + + + + + )} + + +
); if (type === SIGNAL_TYPES.ALERT) return ( - - - item?.name}> - Name - - - {({ item }) => ( - - {item?.name} - - )} -
+
+ + + + Condition + + + Policy + + + + {() => ( + + + + + )} + + +
); return EMPTY_STATE; diff --git a/nerdlets/signal-selection/styles.scss b/nerdlets/signal-selection/styles.scss index 89ee1a78..86bfe0cc 100644 --- a/nerdlets/signal-selection/styles.scss +++ b/nerdlets/signal-selection/styles.scss @@ -31,11 +31,17 @@ .listing { flex: 1 1 auto; - overflow: auto; + overflow: hidden; display: flex; gap: 16px; + .data-table { + flex: 1; + overflow-y: scroll; + } + .selection { + width: 30%; padding: 8px; display: flex; flex-direction: column;