Files
pn-new-crm/src/components/dashboard/kpi-cards.tsx
Matt ee2da8f67e feat(currency): centralise money formatting + curated currency picker
New `src/lib/utils/currency.ts` is the single source of truth for
display formatting (`formatCurrency`) and the supported-currency
catalog (`SUPPORTED_CURRENCIES`, 10 codes covering the marina market).

New shared components:
- `<CurrencyInput>` — number input with leading symbol prefix and
  decimal inputMode, raw number value out via onChange.
- `<CurrencySelect>` — Select dropdown over `SUPPORTED_CURRENCIES`
  with symbol + code + label per row, replaces the free-text 3-letter
  inputs that let reps type "EURO" or "$$$" into a 3-char ISO column.

Threaded through every money input + display:
- Forms: berth (price/currency), expense (amount/currency), invoice
  (currency Select + line-items unit-price + step-3 review totals).
- Reads: berth-card / berth-columns / invoice-card / expense-card /
  dashboard KPIs / dashboard revenue-forecast / portal-invoices page.
  Each had its own ad-hoc `Intl.NumberFormat` wrapper with slightly
  different fallbacks; collapsed onto the shared helper.

`InvoiceLineItems` gained a `currency` prop so the unit-price input
prefix and the subtotal use the parent invoice's currency rather than
hard-coded `en-US` formatting.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 04:24:46 +02:00

105 lines
2.9 KiB
TypeScript

'use client';
import { useQuery } from '@tanstack/react-query';
import { apiFetch } from '@/lib/api/client';
import { useUIStore } from '@/stores/ui-store';
import { KPITile } from '@/components/ui/kpi-tile';
import { Skeleton } from '@/components/ui/skeleton';
import { formatCurrency } from '@/lib/utils/currency';
import { WidgetErrorBoundary } from './widget-error-boundary';
interface KpiData {
totalClients: number;
activeInterests: number;
pipelineValueUsd: number;
occupancyRate: number;
}
const formatUsd = (value: number) => formatCurrency(value, 'USD', { maxFractionDigits: 0 });
function formatPercent(value: number): string {
return `${value.toFixed(1)}%`;
}
function KpiTileSkeleton() {
return (
<div className="relative overflow-hidden rounded-xl border border-border bg-card p-3 shadow-sm sm:p-5">
<div className="absolute inset-x-0 top-0 h-1 bg-muted" aria-hidden />
<Skeleton className="h-3 w-20" />
<Skeleton className="mt-2 h-6 w-24 sm:mt-3 sm:h-7" />
</div>
);
}
export function KpiCards() {
// Keying on currentPortId ensures React Query treats a port-resolved fetch
// as a different query than the one that fires on first paint when the
// store hasn't yet hydrated. Without this, an early null-port fetch could
// cache an error and display "-" indefinitely until the staleTime expires.
const portId = useUIStore((s) => s.currentPortId);
const { data, isLoading, isError } = useQuery<KpiData>({
queryKey: ['dashboard', 'kpis', portId],
queryFn: () => apiFetch<KpiData>('/api/v1/dashboard/kpis'),
staleTime: 60_000,
retry: 2,
// Avoid running until we have a port id - gates against the early
// unauth/no-port window where the API would return zeroes/errors.
enabled: !!portId,
});
if (isLoading) {
return (
<>
<KpiTileSkeleton />
<KpiTileSkeleton />
<KpiTileSkeleton />
<KpiTileSkeleton />
</>
);
}
const kpis: Array<{
label: string;
value: string;
accent: 'brand' | 'success' | 'warning' | 'mint' | 'teal' | 'purple';
}> = [
{
label: 'Total Clients',
value: isError ? '-' : String(data?.totalClients ?? 0),
accent: 'brand',
},
{
label: 'Active Interests',
value: isError ? '-' : String(data?.activeInterests ?? 0),
accent: 'teal',
},
{
label: 'Pipeline Value',
value: isError ? '-' : formatUsd(data?.pipelineValueUsd ?? 0),
accent: 'success',
},
{
label: 'Occupancy Rate',
value: isError ? '-' : formatPercent(data?.occupancyRate ?? 0),
accent: 'purple',
},
];
return (
<>
{kpis.map(({ label, value, accent }) => (
<KPITile key={label} title={label} value={value} accent={accent} />
))}
</>
);
}
export function KpiCardsWithBoundary() {
return (
<WidgetErrorBoundary>
<KpiCards />
</WidgetErrorBoundary>
);
}