fleet/frontend/pages/SoftwarePage/SoftwareTitles/SoftwareTable/SoftwareTitlesTableConfig.tsx
Jacob Shandling 5ec1c2c4bf
Software page, details empty states when no hosts are affected (#16908)
## Addresses #16870 


![Screenshot-2024-02-15-at-50759PM(1)](https://github.com/fleetdm/fleet/assets/61553566/9ced47ea-6bbc-4ea6-b31f-26b3ea01a135)

## Checklist for submitter
- [x] Manual QA

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2024-02-16 13:50:25 -08:00

196 lines
5.4 KiB
TypeScript

import React from "react";
import { Column } from "react-table";
import { InjectedRouter } from "react-router";
import {
ISoftwareTitleVersion,
ISoftwareTitle,
formatSoftwareType,
} from "interfaces/software";
import PATHS from "router/paths";
import { buildQueryStringFromParams } from "utilities/url";
import HeaderCell from "components/TableContainer/DataTable/HeaderCell";
import TextCell from "components/TableContainer/DataTable/TextCell";
import LinkCell from "components/TableContainer/DataTable/LinkCell/LinkCell";
import ViewAllHostsLink from "components/ViewAllHostsLink";
import VersionCell from "../../components/VersionCell";
import VulnerabilitiesCell from "../../components/VulnerabilitiesCell";
import SoftwareIcon from "../../components/icons/SoftwareIcon";
// NOTE: cellProps come from react-table
// more info here https://react-table.tanstack.com/docs/api/useTable#cell-properties
interface ICellProps {
cell: {
value: number | string | ISoftwareTitleVersion[];
};
row: {
original: ISoftwareTitle;
};
}
interface IStringCellProps extends ICellProps {
cell: {
value: string;
};
}
interface IVersionCellProps extends ICellProps {
cell: {
value: ISoftwareTitleVersion[];
};
}
interface INumberCellProps extends ICellProps {
cell: {
value: number;
};
}
interface IVulnCellProps extends ICellProps {
cell: {
value: ISoftwareTitleVersion[];
};
}
interface IHeaderProps {
column: {
title: string;
isSortedDesc: boolean;
};
}
const getVulnerabilities = (versions: ISoftwareTitleVersion[]) => {
if (!versions) {
return [];
}
const vulnerabilities = versions.reduce((acc: string[], currentVersion) => {
if (
currentVersion.vulnerabilities &&
currentVersion.vulnerabilities.length !== 0
) {
acc.push(...currentVersion.vulnerabilities);
}
return acc;
}, []);
return vulnerabilities;
};
const generateTableHeaders = (
router: InjectedRouter,
teamId?: number
): Column[] => {
const softwareTableHeaders = [
{
title: "Name",
Header: (cellProps: IHeaderProps): JSX.Element => (
<HeaderCell
value={cellProps.column.title}
isSortedDesc={cellProps.column.isSortedDesc}
/>
),
disableSortBy: false,
accessor: "name",
Cell: (cellProps: IStringCellProps): JSX.Element => {
const { id, name, source } = cellProps.row.original;
const teamQueryParam = buildQueryStringFromParams({ team_id: teamId });
const softwareTitleDetailsPath = `${PATHS.SOFTWARE_TITLE_DETAILS(
id.toString()
)}?${teamQueryParam}`;
const onClickSoftware = (e: React.MouseEvent) => {
// Allows for button to be clickable in a clickable row
e.stopPropagation();
router?.push(softwareTitleDetailsPath);
};
return (
<LinkCell
path={softwareTitleDetailsPath}
customOnClick={onClickSoftware}
value={
<>
<SoftwareIcon name={name} source={source} />
<span className="software-name">{name}</span>
</>
}
/>
);
},
sortType: "caseInsensitive",
},
{
title: "Version",
Header: "Version",
disableSortBy: true,
accessor: "versions",
Cell: (cellProps: IVersionCellProps): JSX.Element => (
<VersionCell versions={cellProps.cell.value} />
),
},
{
title: "Type",
Header: "Type",
disableSortBy: true,
accessor: "source",
Cell: (cellProps: ICellProps): JSX.Element => (
<TextCell value={formatSoftwareType(cellProps.row.original)} />
),
},
// the "vulnerabilities" accessor is used but the data is actually coming
// from the version attribute. We do this as we already have a "versions"
// attribute used for the "Version" column and we cannot reuse. This is a
// limitation of react-table.
// With the versions data, we can sum up the vulnerabilities to get the
// total number of vulnerabilities for the software title
{
title: "Vulnerabilities",
Header: "Vulnerabilities",
disableSortBy: true,
accessor: "vulnerabilities",
Cell: (cellProps: IVulnCellProps): JSX.Element => {
const vulnerabilities = getVulnerabilities(
cellProps.row.original.versions ?? []
);
return <VulnerabilitiesCell vulnerabilities={vulnerabilities} />;
},
},
{
title: "Hosts",
Header: (cellProps: IHeaderProps): JSX.Element => (
<HeaderCell
value={cellProps.column.title}
disableSortBy={false}
isSortedDesc={cellProps.column.isSortedDesc}
/>
),
disableSortBy: false,
accessor: "hosts_count",
Cell: (cellProps: INumberCellProps): JSX.Element => (
<TextCell value={cellProps.cell.value} />
),
},
{
title: "",
Header: "",
accessor: "linkToFilteredHosts",
disableSortBy: true,
Cell: (cellProps: ICellProps) => {
return (
<ViewAllHostsLink
queryParams={{
software_title_id: cellProps.row.original.id,
team_id: teamId, // TODO: do we need team id here?
}}
className="software-link"
rowHover
/>
);
},
},
];
return softwareTableHeaders;
};
export default generateTableHeaders;