Open-source personal finance manager. Self-hosted, privacy-first.
feat(frontend): add refresh button to dashboard, accounts, rules and categories (#1248)
Tássio Noronha3fbcbe5ff2ca
diff --git a/frontend/src/components/page-refresh-button.test.tsx b/frontend/src/components/page-refresh-button.test.tsxnew file mode 100644index 00000000..ae8be598--- /dev/null+++ b/frontend/src/components/page-refresh-button.test.tsx@@ -0,0 +1,41 @@+import { screen, waitFor } from '@testing-library/react'+import { describe, expect, it, vi } from 'vitest'++import { PageRefreshButton } from '@/components/page-refresh-button'+import { renderWithProviders } from '@/test/utils'++describe('PageRefreshButton', () => {+ it('refetches when clicked', async () => {+ const onRefresh = vi.fn()++ const { user } = renderWithProviders(<PageRefreshButton onRefresh={onRefresh} />)++ const button = screen.getByRole('button', { name: 'Refresh' })+ await user.click(button)++ expect(onRefresh).toHaveBeenCalledTimes(1)+ })++ it('spins and disables while a refetch is in flight', () => {+ renderWithProviders(+ <PageRefreshButton onRefresh={() => {}} refreshing />,+ )++ const button = screen.getByRole('button', { name: 'Refresh' })+ expect(button).toBeDisabled()+ expect(button.querySelector('.animate-spin')).not.toBeNull()+ })++ it('stays busy until the refetch it started settles', async () => {+ let settle: () => void = () => {}+ const onRefresh = vi.fn(() => new Promise<void>((resolve) => { settle = resolve }))++ const { user } = renderWithProviders(<PageRefreshButton onRefresh={onRefresh} />)+ const button = screen.getByRole('button', { name: 'Refresh' })+ await user.click(button)++ expect(button).toBeDisabled()+ settle()+ await waitFor(() => expect(button).toBeEnabled())+ })+})diff --git a/frontend/src/components/page-refresh-button.tsx b/frontend/src/components/page-refresh-button.tsxnew file mode 100644index 00000000..2a8b3032--- /dev/null+++ b/frontend/src/components/page-refresh-button.tsx@@ -0,0 +1,52 @@+import { useState } from 'react'+import { RefreshCw } from 'lucide-react'+import { useTranslation } from 'react-i18next'++import { Button } from '@/components/ui/button'++export interface PageRefreshButtonProps {+ /**+ * Refetch the page's data (usually `queryClient.refetchQueries`). When it+ * returns a promise, the button stays busy until every refetch settles.+ */+ onRefresh: () => unknown+ /** Spin the icon and disable the button while a refetch is in flight. */+ refreshing?: boolean+}++/**+ * Icon-only "refresh" control for a page header. Mirrors the transactions+ * page's refresh button so every page reads the same word and behaves the+ * same: it spins and disables while a refetch is in flight.+ */+export function PageRefreshButton({ onRefresh, refreshing = false }: PageRefreshButtonProps) {+ const { t } = useTranslation()+ const label = t('transactions.refresh')+ // `refreshing` follows the page's main query; a page can show other queries+ // that finish later, so the button also waits for its own refetch promise.+ const [pending, setPending] = useState(false)+ const busy = refreshing || pending++ const handleClick = () => {+ const result = onRefresh()+ if (result instanceof Promise) {+ setPending(true)+ result.finally(() => setPending(false)).catch(() => {})+ }+ }++ return (+ <Button+ variant="outline"+ size="icon"+ className="!size-9"+ aria-label={label}+ title={label}+ data-testid="page-refresh-button"+ disabled={busy}+ onClick={handleClick}+ >+ <RefreshCw size={16} className={busy ? 'animate-spin' : ''} />+ </Button>+ )+}diff --git a/frontend/src/pages/accounts.tsx b/frontend/src/pages/accounts.tsxindex a704b642..8b62703d 100644--- a/frontend/src/pages/accounts.tsx+++ b/frontend/src/pages/accounts.tsx@@ -30,6 +30,7 @@ import { getAccountTypeConfig } from '@/lib/account-type-config'import { AccountPageActions } from '@/components/account-page-actions'import { AccountRowActions } from '@/components/account-row-actions'import { PageHeader } from '@/components/page-header'+import { PageRefreshButton } from '@/components/page-refresh-button'import { BankConnectDialog } from '@/components/bank-connect-dialog'import { ConnectorSelectDialog, type Provider } from '@/components/connector-select-dialog'import { OAuthConnectDialog } from '@/components/oauth-connect-dialog'@@ -89,7 +90,7 @@ export default function AccountsPage() {const [reconnectItemId, setReconnectItemId] = useState<string | null>(null)const [tokenReconnectConnection, setTokenReconnectConnection] = useState<BankConnection | null>(null)- const { data: accountsList, isLoading: accountsLoading } = useQuery({+ const { data: accountsList, isLoading: accountsLoading, isFetching: accountsFetching } = useQuery({queryKey: ['accounts'],queryFn: () => accounts.list(),})@@ -246,12 +247,18 @@ export default function AccountsPage() {section={t('accounts.title')}title={t('accounts.title')}action={- <AccountPageActions- canWrite={canWrite}- onAddAccount={() => { setEditingAccount(null); setDialogOpen(true) }}- onConnectBank={() => setConnectorSelectOpen(true)}- onOpenCollections={() => navigate('/collections')}- />+ <div className="flex items-center gap-2">+ <PageRefreshButton+ onRefresh={() => queryClient.refetchQueries({ type: 'active' })}+ refreshing={accountsFetching}+ />+ <AccountPageActions+ canWrite={canWrite}+ onAddAccount={() => { setEditingAccount(null); setDialogOpen(true) }}+ onConnectBank={() => setConnectorSelectOpen(true)}+ onOpenCollections={() => navigate('/collections')}+ />+ </div>}/>diff --git a/frontend/src/pages/categories.tsx b/frontend/src/pages/categories.tsxindex d10a6407..0dd34ae5 100644--- a/frontend/src/pages/categories.tsx+++ b/frontend/src/pages/categories.tsx@@ -20,6 +20,7 @@ import {import type { Category, CategoryGroup, CategoryUsage } from '@/types'import { Pencil, Trash2, Plus, ChevronDown, ChevronRight, ChevronsUpDown, Eye, EyeOff } from 'lucide-react'import { PageHeader } from '@/components/page-header'+import { PageRefreshButton } from '@/components/page-refresh-button'import { invalidateCategoryQueries } from '@/lib/invalidate-queries'import { CategoryIcon } from '@/components/category-icon'import { CategorySelect } from '@/components/category-select'@@ -77,7 +78,7 @@ export default function CategoriesPage() {queryFn: groupsApi.listIncludingHidden,})- const { data: categoriesList } = useQuery({+ const { data: categoriesList, isFetching: categoriesFetching } = useQuery({queryKey: ['categories', 'management'],queryFn: categoriesApi.listIncludingHidden,})@@ -314,7 +315,16 @@ export default function CategoriesPage() {return (<div>- <PageHeader section={t('categories.title')} title={t('categories.title')} />+ <PageHeader+ section={t('categories.title')}+ title={t('categories.title')}+ action={+ <PageRefreshButton+ onRefresh={() => queryClient.refetchQueries({ type: 'active' })}+ refreshing={categoriesFetching}+ />+ }+ /><p className="mb-4 text-sm text-muted-foreground">{t('categories.hiddenScopeDescription')}diff --git a/frontend/src/pages/dashboard.test.tsx b/frontend/src/pages/dashboard.test.tsxindex c2238a20..21c560d1 100644--- a/frontend/src/pages/dashboard.test.tsx+++ b/frontend/src/pages/dashboard.test.tsx@@ -184,3 +184,22 @@ describe('Dashboard available balance without an FX rate', () => {expect(screen.queryByText(t('transactions.fxFallbackBanner'))).not.toBeInTheDocument()})})++describe('Dashboard refresh button', () => {+ it('refetches page data when the refresh button is clicked', async () => {+ const { user, queryClient } = renderWithProviders(+ <TooltipProvider delayDuration={0}>+ <DashboardPage />+ </TooltipProvider>,+ )++ const refetch = vi.spyOn(queryClient, 'refetchQueries')++ const button = screen.getByRole('button', { name: 'Refresh' })+ await user.click(button)++ expect(refetch).toHaveBeenCalledTimes(1)+ // Only the queries on screen: a refresh must not refetch every page visited earlier.+ expect(refetch).toHaveBeenCalledWith({ type: 'active' })+ })+})diff --git a/frontend/src/pages/dashboard.tsx b/frontend/src/pages/dashboard.tsxindex 4e8c67e0..b4a95367 100644--- a/frontend/src/pages/dashboard.tsx+++ b/frontend/src/pages/dashboard.tsx@@ -42,6 +42,7 @@ import { CheckCircle2, CalendarIcon, Clock, Paperclip, Target, ArrowUpDown, Helpimport { Link, useNavigate, useSearchParams } from 'react-router-dom'import { ICON_MAP } from '@/lib/category-icons'import { PageHeader } from '@/components/page-header'+import { PageRefreshButton } from '@/components/page-refresh-button'import { CategoryIcon } from '@/components/category-icon'import { AccountIcon } from '@/components/account-icon'import { TransactionDrillDown, type DrillDownFilter } from '@/components/transaction-drill-down'@@ -187,7 +188,7 @@ export default function DashboardPage() {// silently falling back to "all accounts".const noAccounts = activeAccountIds !== null && activeAccountIds.length === 0- const { data: summary, isLoading: summaryLoading } = useQuery({+ const { data: summary, isLoading: summaryLoading, isFetching: summaryFetching } = useQuery({queryKey: ['dashboard', 'summary', selectedMonth, activeAccountIds, activeWalletIds],queryFn: () => dashboard.summary(monthParam, undefined, acctIds, walletIds),})@@ -660,6 +661,10 @@ export default function DashboardPage() {className="h-8 w-8 flex items-center justify-center rounded-lg border border-border bg-card text-muted-foreground hover:border-border hover:text-foreground transition-all text-base"onClick={() => handleMonthChange(shiftMonth(selectedMonth, 1))}>›</button>+ <PageRefreshButton+ onRefresh={() => queryClient.refetchQueries({ type: 'active' })}+ refreshing={summaryFetching}+ /></div>}/>diff --git a/frontend/src/pages/rules.tsx b/frontend/src/pages/rules.tsxindex 8b2816db..d8738280 100644--- a/frontend/src/pages/rules.tsx+++ b/frontend/src/pages/rules.tsx@@ -21,6 +21,7 @@ import { normalizeRuleMatchValue, ruleSearchText } from '@/lib/rule-match-utils'import { Trash2, Plus, RefreshCw, Package, Check, ArrowUpDown, ArrowUp, ArrowDown, Download, Upload, Search, Power } from 'lucide-react'import { cn } from '@/lib/utils'import { PageHeader } from '@/components/page-header'+import { PageRefreshButton } from '@/components/page-refresh-button'import { useWorkspace } from '@/contexts/workspace-context'import { RuleDialog } from '@/components/rule-dialog'import { ReconciliationRules } from '@/components/reconciliation-rules'@@ -190,7 +191,7 @@ export default function RulesPage() {setDialogOpen(true)}- const { data: rulesList } = useQuery({+ const { data: rulesList, isFetching: rulesFetching } = useQuery({queryKey: ['rules'],queryFn: rulesApi.list,})@@ -442,7 +443,16 @@ export default function RulesPage() {return (<div>- <PageHeader section={t('rules.section')} title={t('nav.rules')} />+ <PageHeader+ section={t('rules.section')}+ title={t('nav.rules')}+ action={+ <PageRefreshButton+ onRefresh={() => queryClient.refetchQueries({ type: 'active' })}+ refreshing={rulesFetching}+ />+ }+ /><div className="mb-4"><Segmented