import { useQuery } from '@tanstack/react-query'; import { useTranslation } from 'react-i18next'; import { AlertTriangle, Loader2 } from 'lucide-react'; import { api } from '../api/client'; // Consumption, cost and stock grouped by the purchase-source supplier // (#2988) — "how much did we run through supplier X" straight from the // recorded usage history, archived spools included. interface SupplierStatsProps { currency: string; // The dashboard timeframe, the same one every other widget on that page // honours. It scopes consumption and cost; stock is point-in-time. dateFrom?: string; dateTo?: string; } function formatGrams(g: number): string { if (Math.abs(g) >= 1000) return `${(g / 1000).toFixed(2)} kg`; return `${Math.round(g)} g`; } export function SupplierStats({ currency, dateFrom, dateTo }: SupplierStatsProps) { const { t } = useTranslation(); const { data, isLoading, isError } = useQuery({ queryKey: ['supplier-stats', dateFrom ?? 'all', dateTo ?? 'all'], queryFn: () => api.getSupplierStats(dateFrom, dateTo), }); if (isLoading) { return (
{t('stats.suppliers.empty')}
; } return (| {t('stats.suppliers.supplier')} | {t('stats.suppliers.spools')} | {t('stats.suppliers.remaining')} | {t('stats.suppliers.consumed')} | {t('stats.suppliers.cost')} |
|---|---|---|---|---|
| {row.supplier_name} | {row.spool_count} | {formatGrams(row.remaining_g)} | {formatGrams(row.consumed_g)} | {currency} {row.cost.toFixed(2)} |