Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 5x 5x 5x 5x 5x | /**
* @file useDashboardMetrics.ts
* @module api/analytics/hooks
*
* @summary
* Dashboard KPI metrics hook for inventory overview.
* Provides real-time counts of inventory items, suppliers, and low-stock alerts.
*
* @enterprise
* - Caches KPI data for 2 minutes to balance freshness and performance
* - Conditional fetching support for performance optimization
* - Graceful fallbacks on error
* - Comprehensive TypeDoc documentation
*/
import { useQuery } from '@tanstack/react-query';
import { getItemCount, getSupplierCount, getLowStockCount } from '../index';
/**
* Hook to load dashboard KPI metrics (inventory count, supplier count, low stock count).
* Caches results for 2 minutes.
*
* @param enabled - Whether to fetch (defaults to true, typically tied to page visibility)
* @returns React Query result with KPI metrics object
*
* @enterprise
* - Only fetches when enabled (performance optimization)
* - 2-minute cache reduces backend load
* - Automatically handles loading and error states
* - Returns graceful null values on error (no breaking)
*
* @example
* ```typescript
* const { data: metrics, isLoading } = useDashboardMetrics();
*
* return (
* <>
* <StatCard title="Items" value={metrics?.itemCount} loading={isLoading} />
* <StatCard title="Suppliers" value={metrics?.supplierCount} loading={isLoading} />
* <StatCard title="Low Stock" value={metrics?.lowStockCount} loading={isLoading} />
* </>
* );
* ```
*/
export function useDashboardMetrics(enabled: boolean = true) {
return useQuery({
queryKey: ['analytics', 'dashboard-metrics'],
queryFn: async () => {
const [inventoryCount, suppliersCount, lowStockCount] = await Promise.all([
getItemCount(),
getSupplierCount(),
getLowStockCount(),
]);
return {
inventoryCount,
suppliersCount,
lowStockCount,
};
},
enabled,
staleTime: 2 * 60_000, // 2 minutes
gcTime: 10 * 60_000, // 10 minutes (formerly cacheTime)
});
}
|