Files
GearBox/src/client/lib/formatters.ts
Jean-Luc Makiola 9647f5759d feat: redesign weight summary legend and add currency selector
Redesign WeightSummaryCard stats from a disconnected 4-column grid to a
compact legend-style list with color dots, percentages, and a divider
before the total row. Switch chart and legend colors to a neutral gray
palette.

Add a currency selector to settings (USD, EUR, GBP, JPY, CAD, AUD) that
changes the displayed symbol across the app. This is visual only — no
value conversion is performed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-16 20:33:07 +01:00

46 lines
1.0 KiB
TypeScript

export type WeightUnit = "g" | "oz" | "lb" | "kg";
const GRAMS_PER_OZ = 28.3495;
const GRAMS_PER_LB = 453.592;
const GRAMS_PER_KG = 1000;
export function formatWeight(
grams: number | null | undefined,
unit: WeightUnit = "g",
): string {
if (grams == null) return "--";
switch (unit) {
case "g":
return `${Math.round(grams)}g`;
case "oz":
return `${(grams / GRAMS_PER_OZ).toFixed(1)} oz`;
case "lb":
return `${(grams / GRAMS_PER_LB).toFixed(2)} lb`;
case "kg":
return `${(grams / GRAMS_PER_KG).toFixed(2)} kg`;
}
}
export type Currency = "USD" | "EUR" | "GBP" | "JPY" | "CAD" | "AUD";
const CURRENCY_SYMBOLS: Record<Currency, string> = {
USD: "$",
EUR: "€",
GBP: "£",
JPY: "¥",
CAD: "CA$",
AUD: "A$",
};
export function formatPrice(
cents: number | null | undefined,
currency: Currency = "USD",
): string {
if (cents == null) return "--";
const symbol = CURRENCY_SYMBOLS[currency];
if (currency === "JPY") {
return `${symbol}${Math.round(cents / 100)}`;
}
return `${symbol}${(cents / 100).toFixed(2)}`;
}