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 (
); } // A failed request is not an empty inventory: reporting a 403 or a 500 as // "you have not assigned any" tells the user they have not done something // they have. if (isError) { return (

{t('stats.suppliers.error')}

); } if (!data || data.length === 0) { return

{t('stats.suppliers.empty')}

; } return (
{data.map((row) => ( ))}
{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)}
); }