From 4028e5dfa3cb99e0634a6ea113dcabc3d2977674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Falc=C3=B3n?= Date: Wed, 3 Jun 2026 19:00:46 +0200 Subject: [PATCH] refactor(js): use shared formatMonthFromYearMonth in dashboard (#482) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What `use-dashboard-data.ts` had a private `formatMonth()` duplicating `utils/date.ts` `formatMonthFromYearMonth()`. Removed the local copy; dashboard now uses the shared util. **Tiny visual change:** non-current-year labels on dashboard net-worth chart render `Jan '26` instead of `Jan 26` — now consistent with cashflow-trend and account-balance charts which already use the shared util. ## Stats - **-14 / +2 lines** ## Checks - `bun run test` — 154 passed - `bun run lint` — clean (1 pre-existing warning) Part of duplication-removal series (#475–#481). --- resources/js/hooks/use-dashboard-data.ts | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/resources/js/hooks/use-dashboard-data.ts b/resources/js/hooks/use-dashboard-data.ts index 132152a9..9cf7b6cc 100644 --- a/resources/js/hooks/use-dashboard-data.ts +++ b/resources/js/hooks/use-dashboard-data.ts @@ -2,6 +2,7 @@ import { useLocale } from '@/hooks/use-locale'; import { getAccountSign } from '@/lib/chart-calculations'; import { Account, AccountType, Bank } from '@/types/account'; import { Category } from '@/types/category'; +import { formatMonthFromYearMonth } from '@/utils/date'; import { format, subDays, subMonths } from 'date-fns'; import { useCallback, useEffect, useState } from 'react'; @@ -66,7 +67,7 @@ export function deriveAccountMetrics( return Object.values(accounts).map((account) => { const investedKey = account.id + '_invested'; const history = data.map((point) => ({ - date: formatMonth(point.month as string, locale), + date: formatMonthFromYearMonth(point.month as string, locale), value: typeof point[account.id] === 'number' ? getAccountSign(account.type) * @@ -100,20 +101,6 @@ export function deriveAccountMetrics( }); } -function formatMonth(yearMonth: string, locale = 'en-US'): string { - const [year, month] = yearMonth.split('-'); - const date = new Date(parseInt(year), parseInt(month) - 1); - - const isCurrentYear = date.getFullYear() === new Date().getFullYear(); - - return date.toLocaleDateString( - locale, - isCurrentYear - ? { month: 'short' } - : { year: '2-digit', month: 'short' }, - ); -} - export function useDashboardData(): DashboardData & { refetch: () => void } { const locale = useLocale(); const [data, setData] = useState>({