fleet/frontend/pages/SoftwarePage/SoftwareVulnerabilities/SoftwareVulnerabilitiesTable/SoftwareVulnerabilitiesTable.tsx
Gabriel Hernandez 4c99ebebaf
UI updates to software page to support added software feature. (#18731)
relates to #18328

make updates to the software titles page to support new add software
feature. this includes.

**Change of page description**


![image](https://github.com/fleetdm/fleet/assets/1153709/e90a2149-54c4-41f0-a1ec-12ebc4619d6c)

**new install status column and change order of `Type` and `verison`
columns**


![image](https://github.com/fleetdm/fleet/assets/1153709/662841fd-2f9e-489c-adc3-fbf1442228b2)

**adding new dropdown filter option and conditionally showing it for
titles and versions tables**


![image](https://github.com/fleetdm/fleet/assets/1153709/8e81680e-d733-4d63-94b6-b4441cb708e3)

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Manual QA for all new/changed functionality
2024-05-06 13:49:49 +01:00

277 lines
7.6 KiB
TypeScript

/** software/vulnerabilities Vulnerabilities tab > Table */
import React, { useCallback, useContext, useMemo } from "react";
import { InjectedRouter } from "react-router";
import { Row } from "react-table";
import PATHS from "router/paths";
import { AppContext } from "context/app";
import {
GITHUB_NEW_ISSUE_LINK,
EXPLOITED_VULNERABILITIES_DROPDOWN_OPTIONS,
} from "utilities/constants";
// @ts-ignore
import Dropdown from "components/forms/fields/Dropdown";
import CustomLink from "components/CustomLink";
import TableContainer from "components/TableContainer";
import LastUpdatedText from "components/LastUpdatedText";
import { ITableQueryData } from "components/TableContainer/TableContainer";
import EmptySoftwareTable from "pages/SoftwarePage/components/EmptySoftwareTable";
import { IVulnerabilitiesResponse } from "services/entities/vulnerabilities";
import { buildQueryStringFromParams } from "utilities/url";
import { getNextLocationPath } from "utilities/helpers";
import generateTableConfig from "./VulnerabilitiesTableConfig";
const baseClass = "software-vulnerabilities-table";
interface IRowProps extends Row {
original: {
cve?: string;
};
}
interface ISoftwareVulnerabilitiesTableProps {
router: InjectedRouter;
isSoftwareEnabled: boolean;
data?: IVulnerabilitiesResponse;
query?: string;
perPage: number;
orderDirection: "asc" | "desc";
orderKey: string;
showExploitedVulnerabilitiesOnly: boolean;
currentPage: number;
teamId?: number;
isLoading: boolean;
}
const SoftwareVulnerabilitiesTable = ({
router,
isSoftwareEnabled,
data,
query,
perPage,
orderDirection,
orderKey,
showExploitedVulnerabilitiesOnly,
currentPage,
teamId,
isLoading,
}: ISoftwareVulnerabilitiesTableProps) => {
const { isPremiumTier, isSandboxMode, noSandboxHosts } = useContext(
AppContext
);
const determineQueryParamChange = useCallback(
(newTableQuery: ITableQueryData) => {
const changedEntry = Object.entries(newTableQuery).find(([key, val]) => {
switch (key) {
case "sortDirection":
return val !== orderDirection;
case "sortHeader":
return val !== orderKey;
case "pageIndex":
return val !== currentPage;
case "searchQuery":
return val !== query;
case "exploit":
return val !== showExploitedVulnerabilitiesOnly.toString();
default:
return false;
}
});
return changedEntry?.[0] ?? "";
},
[
currentPage,
orderDirection,
orderKey,
query,
showExploitedVulnerabilitiesOnly,
]
);
const generateNewQueryParams = useCallback(
(newTableQuery: ITableQueryData, changedParam: string) => {
return {
team_id: teamId,
exploit: showExploitedVulnerabilitiesOnly.toString(),
query: newTableQuery.searchQuery,
order_direction: newTableQuery.sortDirection,
order_key: newTableQuery.sortHeader,
page: changedParam === "pageIndex" ? newTableQuery.pageIndex : 0,
};
},
[teamId, showExploitedVulnerabilitiesOnly]
);
const onQueryChange = useCallback(
(newTableQuery: ITableQueryData) => {
// we want to determine which query param has changed in order to
// reset the page index to 0 if any other param has changed.
const changedParam = determineQueryParamChange(newTableQuery);
// if nothing has changed, don't update the route. this can happen when
// this handler is called on the inital render.
if (changedParam === "") return;
const newRoute = getNextLocationPath({
pathPrefix: PATHS.SOFTWARE_VULNERABILITIES,
routeTemplate: "",
queryParams: generateNewQueryParams(newTableQuery, changedParam),
});
router.replace(newRoute);
},
[determineQueryParamChange, generateNewQueryParams, router]
);
// determines if a user be able to search in the table
const searchable =
isSoftwareEnabled &&
(!!data?.vulnerabilities ||
query !== "" ||
showExploitedVulnerabilitiesOnly);
const vulnerabilitiesTableHeaders = useMemo(() => {
if (!data) return [];
return generateTableConfig(
isPremiumTier,
isSandboxMode,
router,
{
includeName: true,
includeVulnerabilities: true,
includeIcon: true,
},
teamId
);
}, [data, router, teamId]);
const handleExploitedVulnFilterDropdownChange = (
isFilterExploited: boolean
) => {
router.replace(
getNextLocationPath({
pathPrefix: PATHS.SOFTWARE_VULNERABILITIES,
routeTemplate: "",
queryParams: {
query,
team_id: teamId,
order_direction: orderDirection,
order_key: orderKey,
exploit: isFilterExploited.toString(),
page: 0, // resets page index
},
})
);
};
const handleRowSelect = (row: IRowProps) => {
const hostsByVulnerabilityParams = {
vulnerability: row.original.cve,
team_id: teamId,
};
const path = `${PATHS.MANAGE_HOSTS}?${buildQueryStringFromParams(
hostsByVulnerabilityParams
)}`;
router.push(path);
};
const getItemsCountText = () => {
const count = data?.count;
if (!data?.vulnerabilities || !count) return "";
return count === 1 ? `${count} item` : `${count} items`;
};
const getLastUpdatedText = () => {
if (!data?.vulnerabilities || !data?.counts_updated_at) return "";
return (
<LastUpdatedText
lastUpdatedAt={data.counts_updated_at}
whatToRetrieve="vulnerabilities"
/>
);
};
const renderVulnerabilityCount = () => {
const itemText = getItemsCountText();
const lastUpdatedText = getLastUpdatedText();
if (!itemText) return null;
return (
<div className={`${baseClass}__count`}>
<span>{itemText}</span>
{lastUpdatedText}
</div>
);
};
const renderTableFooter = () => {
return (
<div>
Seeing unexpected software or vulnerabilities?{" "}
<CustomLink
url={GITHUB_NEW_ISSUE_LINK}
text="File an issue on GitHub"
newTab
/>
</div>
);
};
const renderExploitedVulnerabilitiesDropdown = () => {
return (
<Dropdown
value={showExploitedVulnerabilitiesOnly}
className={`${baseClass}__exploited-vulnerabilities-dropdown`}
options={EXPLOITED_VULNERABILITIES_DROPDOWN_OPTIONS}
searchable={false}
onChange={handleExploitedVulnFilterDropdownChange}
tableFilterDropdown
/>
);
};
return (
<div className={baseClass}>
<TableContainer
columnConfigs={vulnerabilitiesTableHeaders}
data={data?.vulnerabilities ?? []}
isLoading={isLoading}
resultsTitle={"items"}
emptyComponent={() => (
<EmptySoftwareTable isSoftwareDisabled={!isSoftwareEnabled} />
)}
defaultSortHeader={orderKey}
defaultSortDirection={orderDirection}
defaultPageIndex={currentPage}
manualSortBy
pageSize={perPage}
showMarkAllPages={false}
isAllPagesSelected={false}
disableNextPage={!data?.meta.has_next_results}
searchable={searchable}
searchQueryColumn="vulnerability"
inputPlaceHolder="Search by CVE"
onQueryChange={onQueryChange}
customControl={
searchable ? renderExploitedVulnerabilitiesDropdown : undefined
}
renderCount={renderVulnerabilityCount}
renderFooter={renderTableFooter}
disableMultiRowSelect
onSelectSingleRow={handleRowSelect}
/>
</div>
);
};
export default SoftwareVulnerabilitiesTable;