From 35b64fcf7e87f48ddefbd23103287b00acd2c153 Mon Sep 17 00:00:00 2001 From: Paul Ochieng Levi Date: Wed, 20 May 2026 00:02:48 +0300 Subject: [PATCH 1/8] Enhance Analytics components with error handling and loading states; update hooks for improved data fetching --- .../components/AnalyticsDashboard.tsx | 7 +- .../analytics/components/QuickAccessCard.tsx | 113 ++++++++++++------ .../src/modules/analytics/hooks/index.ts | 57 +++++++-- .../src/modules/analytics/types/index.ts | 2 + .../src/shared/hooks/usePreferences.ts | 8 +- 5 files changed, 132 insertions(+), 55 deletions(-) diff --git a/src/platform/src/modules/analytics/components/AnalyticsDashboard.tsx b/src/platform/src/modules/analytics/components/AnalyticsDashboard.tsx index ab030f58d3..e24344be16 100644 --- a/src/platform/src/modules/analytics/components/AnalyticsDashboard.tsx +++ b/src/platform/src/modules/analytics/components/AnalyticsDashboard.tsx @@ -112,6 +112,7 @@ export const AnalyticsDashboard: React.FC = ({ siteCards, isLoading: siteCardsLoading, isRefreshing: siteCardsRefreshing, + error: siteCardsError, refetch: refreshSiteCards, } = useAnalyticsSiteCards({ selectedSiteIds, @@ -457,10 +458,12 @@ export const AnalyticsDashboard: React.FC = ({ sites={siteCards} onManageFavorites={handleManageFavorites} onRefresh={handleRefreshDashboard} - isRefreshing={isRefreshing} + isRefreshing={siteCardsRefreshing || isRefreshing} selectedPollutant={filters.pollutant} onCardClick={handleCardClick} - isLoading={siteCardsLoading || siteCardsRefreshing || isRefreshing} + isLoading={siteCardsLoading} + placeholderCount={selectedSites.length} + hasError={Boolean(siteCardsError)} showIcon={showIcons} onShowIconsChange={setShowIcons} infoLine={ diff --git a/src/platform/src/modules/analytics/components/QuickAccessCard.tsx b/src/platform/src/modules/analytics/components/QuickAccessCard.tsx index 3ad226eaa9..fa5df58e91 100644 --- a/src/platform/src/modules/analytics/components/QuickAccessCard.tsx +++ b/src/platform/src/modules/analytics/components/QuickAccessCard.tsx @@ -7,6 +7,7 @@ import { cn } from '@/shared/lib/utils'; import type { QuickAccessLocationsProps } from '../types'; import { AnalyticsCard } from './AnalyticsCard'; import { LoadingSpinner } from '@/shared/components/ui/loading-spinner'; +import { WarningBanner } from '@/shared/components/ui'; export const QuickAccessCard: React.FC = memo( ({ @@ -23,8 +24,18 @@ export const QuickAccessCard: React.FC = memo( onShowIconsChange, selectedPollutant, isLoading = false, + placeholderCount = 0, + hasError = false, onCardClick, }) => { + const visiblePlaceholderCount = Math.min(Math.max(placeholderCount, 1), 4); + const shouldShowSkeleton = + visiblePlaceholderCount > 0 && + (isLoading || (hasError && sites.length === 0)); + const shouldShowAddFavorite = shouldShowSkeleton + ? visiblePlaceholderCount < 4 + : sites.length < 4; + return (
= memo( onShowIconsChange={onShowIconsChange} /> + {isRefreshing && sites.length > 0 && !isLoading && ( +
+
+ + Refreshing latest readings... +
+
+ )} + + {hasError && ( + + )} + {warningBanner &&
{warningBanner}
} {/* Sites Grid */} -
-
- {/* Render site cards up to 4 to leave room for Add Location */} - {sites.slice(0, 4).map(site => ( - {})} - /> - ))} - - {/* Show Add Location button if less than 4 sites */} - {sites.length < 4 && ( - - )} -
+
+ {shouldShowSkeleton + ? Array.from({ length: visiblePlaceholderCount }).map( + (_, index) => ( +