Skip to content

Commit

Permalink
computed columns UI WIP 3
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin Reynolds <[email protected]>
  • Loading branch information
gsreynolds committed Jul 5, 2024
1 parent a3a9929 commit d36a15d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/components/ColumnsModal/ColumnsModalComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
import {
defaultColumns,
customAlertColumns,
customComputedColumns,
// columnsForSavedColumns,
} from 'src/config/column-generator';

Expand Down Expand Up @@ -97,6 +98,11 @@ const TableColumnsModalComponent = () => {
value = column.Header
+ (column.accessorPath ? `:${column.accessorPath}` : '')
+ (column.aggregator ? `:${column.aggregator}` : '');
} else if (column.columnType === 'computed') {
// Computed column based on expression
value = column.Header
+ (column.accessorPath ? `:${column.accessorPath}` : '')
+ (column.expression ? `:${column.expression}` : '');
} else {
// Incident column
value = column.Header;
Expand All @@ -106,11 +112,11 @@ const TableColumnsModalComponent = () => {

const getAllAvailableColumns = () => {
// eslint-disable-next-line max-len
const v = [...defaultColumns(), ...customAlertColumns(alertCustomDetailFields)].sort((a, b) => columnValue(a).localeCompare(columnValue(b)));
const v = [...defaultColumns(), ...customAlertColumns(alertCustomDetailFields), ...customComputedColumns(computedFields)].sort((a, b) => columnValue(a).localeCompare(columnValue(b)));
return v;
};

const allAvailableColumns = useMemo(getAllAvailableColumns, [alertCustomDetailFields]);
const allAvailableColumns = useMemo(getAllAvailableColumns, [alertCustomDetailFields, computedFields]);

// const getSelectedColumns = () => columnsForSavedColumns(incidentTableColumns).map((column) => {
// // Recreate original value used from react-select in order to populate dual list
Expand Down
54 changes: 54 additions & 0 deletions src/config/column-generator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,47 @@ export const customAlertColumnForSavedColumn = (savedColumn) => {
return column;
};

export const customComputedColumnForSavedColumn = (savedColumn) => {
const {
Header: header, accessorPath, aggregator, width, expression, expressionType,
} = savedColumn;
if (!(header && accessorPath)) {
return null;
}
const accessor = (incident) => {
const path = accessorPath;
let result = null;
try {
result = JSONPath({
path,
json: incident,
});
} catch (e) {
result = null;
}
if (aggregator) {
return result;
}
return result[0];
};
const column = incidentColumn({
id: accessorPath,
header,
columnType: 'computed',
accessor,
accessorPath,
expression,
expressionType,
minWidth: 100,
renderer: renderPlainTextAlertCell,
});

if (width) {
column.width = width;
}
return column;
};

export const defaultColumns = () => [...defaultIncidentColumns(), ...defaultAlertsColumns()];

export const customAlertColumns = (savedColumns) => {
Expand All @@ -887,6 +928,19 @@ export const customAlertColumns = (savedColumns) => {
});
};

export const customComputedColumns = (savedColumns) => {
const allColumns = defaultColumns();
return savedColumns.map((column) => {
if (
column.columnType === 'computed'
&& !allColumns.find((c) => c.originalHeader === column.Header)
) {
return customComputedColumnForSavedColumn(column);
}
return undefined;
});
};

export const columnsForSavedColumns = (savedColumns) => {
const allColumns = defaultColumns();
const columns = savedColumns
Expand Down

0 comments on commit d36a15d

Please sign in to comment.