Fleet UI: Dashboard vuln bugginess (#28848)

This commit is contained in:
RachelElysia 2025-05-07 11:39:32 -04:00 committed by GitHub
parent 55f308663a
commit 37511be3a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -299,6 +299,7 @@ const DashboardPage = ({ router, location }: IDashboardProps): JSX.Element => {
? teams?.find((t) => t.id === currentTeamId)?.features
: config?.features;
const isSoftwareEnabled = !!featuresConfig?.enable_software_inventory;
const isViewingVulnerableSoftware = !!softwareNavTabIndex; // we can take the tab index as a boolean to represent the vulnerable flag
const SOFTWARE_DEFAULT_SORT_DIRECTION = "desc";
const SOFTWARE_DEFAULT_SORT_HEADER = "hosts_count";
@ -322,7 +323,7 @@ const DashboardPage = ({ router, location }: IDashboardProps): JSX.Element => {
orderDirection: SOFTWARE_DEFAULT_SORT_DIRECTION,
orderKey: SOFTWARE_DEFAULT_SORT_HEADER,
teamId: teamIdForApi,
vulnerable: !!softwareNavTabIndex, // we can take the tab index as a boolean to represent the vulnerable flag :)
vulnerable: isViewingVulnerableSoftware,
},
],
({ queryKey }) => softwareAPI.load(queryKey[0]),
@ -331,11 +332,9 @@ const DashboardPage = ({ router, location }: IDashboardProps): JSX.Element => {
keepPreviousData: true,
staleTime: 30000, // stale time can be adjusted if fresher data is desired based on software inventory interval
onSuccess: (data) => {
const hasSoftwareResults = data.software?.length > 0;
const viewingVulnSoftwareTab = softwareNavTabIndex === 1;
const hasSoftwareResults = !!data.software && data.software.length > 0;
// Prevent card from hiding if returns results or on-clicking vuln tab if software has no vulnerabilities
if (hasSoftwareResults || viewingVulnSoftwareTab) {
if (hasSoftwareResults) {
setSoftwareTitleDetail &&
setSoftwareTitleDetail(
<LastUpdatedText
@ -352,21 +351,23 @@ const DashboardPage = ({ router, location }: IDashboardProps): JSX.Element => {
/>
);
setShowSoftwareCard(true);
} else {
} else if (!isViewingVulnerableSoftware) {
setShowSoftwareCard(false);
}
// For vulnerable software with no results, the count query will handle showing the card.
},
}
);
// If no vulnerable software, !software?.software can return undefined
// Must check non-vuln software count > 0 to show software card iff API returning undefined
const { data: softwareCount } = useQuery<
ISoftwareCountResponse,
Error,
number,
ISoftwareCountQueryKey[]
>(
// If viewing vulnerable software and no results are returned,
// fetch the count of non-vulnerable software to decide if the card should be shown.
const shouldFetchSoftwareCount =
isSoftwareEnabled && // Needed to prevent race condition with isSoftwareFetching
!isSoftwareFetching &&
isViewingVulnerableSoftware &&
(!software?.software || software?.software.length === 0);
useQuery<ISoftwareCountResponse, Error, number, ISoftwareCountQueryKey[]>(
[
{
scope: "softwareCount",
@ -375,11 +376,14 @@ const DashboardPage = ({ router, location }: IDashboardProps): JSX.Element => {
],
({ queryKey }) => softwareAPI.getCount(queryKey[0]),
{
enabled: isRouteOk && !software?.software,
enabled: isRouteOk && shouldFetchSoftwareCount,
keepPreviousData: true,
refetchOnWindowFocus: false,
retry: 1,
select: (data) => data.count,
onSuccess: (count) => {
setShowSoftwareCard(!!count && count > 0);
},
}
);
@ -455,12 +459,6 @@ const DashboardPage = ({ router, location }: IDashboardProps): JSX.Element => {
counts_updated_at: munkiCountsUpdatedAt,
} = macAdminsData || {};
useEffect(() => {
softwareCount && softwareCount > 0
? setShowSoftwareCard(true)
: setShowSoftwareCard(false);
}, [softwareCount]);
// Sets selected platform label id for links to filtered manage host page
useEffect(() => {
if (labels) {