diff --git a/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx b/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx index a82efae05c..f46c4d7921 100644 --- a/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx +++ b/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx @@ -1352,15 +1352,19 @@ const ManageHostsPage = ({ }`} > {`${count} host${count === 1 ? "" : "s"}`} - + {count ? ( + + ) : ( + <> + )} ); }, [isHostCountLoading, filteredHostCount]); diff --git a/frontend/services/entities/hosts.ts b/frontend/services/entities/hosts.ts index c4d3e54e90..3980368447 100644 --- a/frontend/services/entities/hosts.ts +++ b/frontend/services/entities/hosts.ts @@ -23,6 +23,21 @@ export interface ILoadHostsOptions { visibleColumns?: string; } +export interface IExportHostsOptions { + sortBy: ISortOption[]; + page?: number; + perPage?: number; + selectedLabels?: string[]; + globalFilter?: string; + teamId?: number; + policyId?: number; + policyResponse?: string; + softwareId?: number; + device_mapping?: boolean; + columns?: string; + visibleColumns?: string; +} + export type ILoadHostDetailsExtension = "device_mapping" | "macadmins"; export default { @@ -53,63 +68,56 @@ export default { }, }); }, - exportHosts: (options: ILoadHostsOptions | undefined) => { - const { HOSTS_REPORT, LABEL_HOSTS } = endpoints; + exportHosts: (options: IExportHostsOptions) => { + const { HOSTS_REPORT } = endpoints; + const sortBy = options.sortBy; const selectedLabels = options?.selectedLabels || []; const globalFilter = options?.globalFilter || ""; - const sortBy = options?.sortBy || []; const teamId = options?.teamId || null; const policyId = options?.policyId || null; - const policyResponse = options?.policyResponse || null; + const policyResponse = options?.policyResponse || "passing"; const softwareId = options?.softwareId || null; const visibleColumns = options?.visibleColumns || null; - let orderKeyParam = ""; - let orderDirection = ""; - if (sortBy.length !== 0) { - const sortItem = sortBy[0]; - orderKeyParam += `&order_key=${sortItem.key}`; - orderDirection = `&order_direction=${sortItem.direction}`; + if (!sortBy.length) { + throw Error("sortBy is a required field."); } - let searchQuery = ""; + const orderKeyParam = `?order_key=${sortBy[0].key}`; + const orderDirection = `&order_direction=${sortBy[0].direction}`; + + let path = `${HOSTS_REPORT}${orderKeyParam}${orderDirection}`; + if (globalFilter !== "") { - searchQuery = `&query=${globalFilter}`; + path += `&query=${globalFilter}`; } - - let path = ""; const labelPrefix = "labels/"; // Handle multiple filters - const label = selectedLabels.find((f) => f.includes(labelPrefix)); - const status = selectedLabels.find((f) => !f.includes(labelPrefix)); - const isValidStatus = - status === "new" || status === "online" || status === "offline"; - if (label) { - const lid = label.substr(labelPrefix.length); - path = `${LABEL_HOSTS( - parseInt(lid, 10) - )}?${searchQuery}${orderKeyParam}${orderDirection}`; + const label = selectedLabels.find((f) => f.includes(labelPrefix)) || ""; + const status = selectedLabels.find((f) => !f.includes(labelPrefix)) || ""; + const statusFilterList = ["new", "online", "offline"]; + const isStatusFilter = statusFilterList.includes(status); - // connect status if applicable - if (status && isValidStatus) { - path += `&status=${status}`; - } - } else if (status && isValidStatus) { - path = `${HOSTS_REPORT}?&status=${status}${searchQuery}${orderKeyParam}${orderDirection}`; - } else { - path = `${HOSTS_REPORT}?${searchQuery}${orderKeyParam}${orderDirection}`; + if (isStatusFilter) { + path += `&status=${status}`; } if (teamId) { path += `&team_id=${teamId}`; } + // Label OR policy_id OR software_id are valid filters. + if (label) { + const lid = label.substr(labelPrefix.length); + path += `&label_id=${parseInt(lid, 10)}`; + } + if (!label && policyId) { path += `&policy_id=${policyId}`; - path += `&policy_response=${policyResponse || "passing"}`; // TODO: confirm whether there should be a default if there is an id but no response sepcified + path += `&policy_response=${policyResponse}`; } - // TODO: consider how to check for mutually exclusive scenarios with label, policy and software + if (!label && !policyId && softwareId) { path += `&software_id=${softwareId}`; }