From 37511be3a5c84864eeb272b66b59aeb2e8cd8fe0 Mon Sep 17 00:00:00 2001 From: RachelElysia <71795832+RachelElysia@users.noreply.github.com> Date: Wed, 7 May 2025 11:39:32 -0400 Subject: [PATCH] Fleet UI: Dashboard vuln bugginess (#28848) --- .../pages/DashboardPage/DashboardPage.tsx | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/frontend/pages/DashboardPage/DashboardPage.tsx b/frontend/pages/DashboardPage/DashboardPage.tsx index 7273ffbdd4..d2cf3c2d0a 100644 --- a/frontend/pages/DashboardPage/DashboardPage.tsx +++ b/frontend/pages/DashboardPage/DashboardPage.tsx @@ -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( { /> ); 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( [ { 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) {