fleet/frontend/pages/queries/edit/components/QueryResults/QueryResultsTableConfig.tsx
jacobshandling 885e1d5a25
UI - Determine query result column sort type from actual data present (#24734)
## Addresses #23011 

- In the same scan through results that the UI currently determines
unique column names, determine which of thsoe columns can be sorted as
alphanumeric.

<img width="1464" alt="Screenshot 2024-12-12 at 3 15 24 PM"
src="https://github.com/user-attachments/assets/49c7c7a5-632a-475f-9e16-891119274708"
/>

<img width="1464" alt="Screenshot 2024-12-12 at 3 14 25 PM"
src="https://github.com/user-attachments/assets/2ede4f28-4c00-43af-b144-3828c42b7fbc"
/>


- [x] Changes file added for user-visible changes in `changes/`,
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2024-12-17 09:41:30 -08:00

71 lines
2.5 KiB
TypeScript

/* eslint-disable react/prop-types */
// disable this rule as it was throwing an error in Header and Cell component
// definitions for the selection row for some reason when we dont really need it.
import React from "react";
import { CellProps, Column, HeaderProps } from "react-table";
import DefaultColumnFilter from "components/TableContainer/DataTable/DefaultColumnFilter";
import HeaderCell from "components/TableContainer/DataTable/HeaderCell/HeaderCell";
import {
getUniqueColsAreNumTypeFromRows,
internallyTruncateText,
} from "utilities/helpers";
const _unshiftHostname = <T extends object>(columns: Column<T>[]) => {
const newHeaders = [...columns];
const displayNameIndex = columns.findIndex(
(h) => h.id === "host_display_name"
);
if (displayNameIndex >= 0) {
// remove hostname header from headers
const [displayNameHeader] = newHeaders.splice(displayNameIndex, 1);
// reformat title and insert at start of headers array
newHeaders.unshift({ ...displayNameHeader, id: "Host" });
}
// TODO: Remove after v5 when host_hostname is removed rom API response.
const hostNameIndex = columns.findIndex((h) => h.id === "host_hostname");
if (hostNameIndex >= 0) {
newHeaders.splice(hostNameIndex, 1);
}
// end remove
return newHeaders;
};
const generateColumnConfigsFromRows = <T extends Record<keyof T, unknown>>(
results: T[] // {col:val, ...} for each row of query results
): Column<T>[] => {
const colsAreNumTypes = getUniqueColsAreNumTypeFromRows(results) as Map<
string,
boolean
>;
const columnConfigs = Array.from(colsAreNumTypes.keys()).map<Column<T>>(
(colName) => {
return {
id: colName,
Header: (headerProps: HeaderProps<T>) => (
<HeaderCell
value={headerProps.column.id}
isSortedDesc={headerProps.column.isSortedDesc}
/>
),
// generic for convenience, can assume keyof T is a string
accessor: (data) => data[colName as keyof T],
Cell: (cellProps: CellProps<T>) => {
const val = cellProps?.cell?.value;
return !!val?.length && val.length > 300
? internallyTruncateText(val)
: val ?? null;
},
Filter: DefaultColumnFilter,
disableSortBy: false,
sortType: colsAreNumTypes.get(colName)
? "alphanumeric"
: "caseInsensitive",
};
}
);
return _unshiftHostname(columnConfigs);
};
export default generateColumnConfigsFromRows;