refactor: add useFormatters hook to reduce boilerplate across 14 components
Created useFormatters() combining useWeightUnit + useCurrency + formatWeight/formatPrice into a single hook returning weight(grams) and price(cents) bound functions plus raw unit and currency values. Updated all 14 consumer files to use the new hook, removing the repeated 4-import + 2-hook-call pattern from each. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { Link } from "@tanstack/react-router";
|
||||
import { useAuth, useLogout } from "../hooks/useAuth";
|
||||
import { useFormatters } from "../hooks/useFormatters";
|
||||
import { useUpdateSetting } from "../hooks/useSettings";
|
||||
import { useTotals } from "../hooks/useTotals";
|
||||
import { useWeightUnit } from "../hooks/useWeightUnit";
|
||||
import { formatPrice, formatWeight, type WeightUnit } from "../lib/formatters";
|
||||
import type { WeightUnit } from "../lib/formatters";
|
||||
import { LucideIcon } from "../lib/iconData";
|
||||
|
||||
const UNITS: WeightUnit[] = ["g", "oz", "lb", "kg"];
|
||||
@@ -23,7 +23,7 @@ export function TotalsBar({
|
||||
const { data: auth } = useAuth();
|
||||
const logout = useLogout();
|
||||
const isAuthenticated = !!auth?.user;
|
||||
const unit = useWeightUnit();
|
||||
const { weight, price, unit } = useFormatters();
|
||||
const updateSetting = useUpdateSetting();
|
||||
|
||||
// When no stats provided, use global totals (backward compatible)
|
||||
@@ -34,14 +34,14 @@ export function TotalsBar({
|
||||
{ label: "items", value: String(data.global.itemCount) },
|
||||
{
|
||||
label: "total",
|
||||
value: formatWeight(data.global.totalWeight, unit),
|
||||
value: weight(data.global.totalWeight),
|
||||
},
|
||||
{ label: "spent", value: formatPrice(data.global.totalCost) },
|
||||
{ label: "spent", value: price(data.global.totalCost) },
|
||||
]
|
||||
: [
|
||||
{ label: "items", value: "0" },
|
||||
{ label: "total", value: formatWeight(null, unit) },
|
||||
{ label: "spent", value: formatPrice(null) },
|
||||
{ label: "total", value: weight(null) },
|
||||
{ label: "spent", value: price(null) },
|
||||
]);
|
||||
|
||||
const titleContent = (
|
||||
|
||||
Reference in New Issue
Block a user