A local-first personal finance app
Restore budget table scroll position when navigating back from spent transactions (#8144)
clintharrisf6c38776750a
diff --git a/.gitignore b/.gitignoreindex a5a699e6ed..1dde8e7507 100644--- a/.gitignore+++ b/.gitignore@@ -75,6 +75,9 @@ bundle.mobile.js.map# Local Netlify folder.netlify+# Personal devcontainer config+.devcontainer/*.local+# build outputpackage.tgzdiff --git a/packages/desktop-client/e2e/budget.test.ts b/packages/desktop-client/e2e/budget.test.tsindex b16f255ef6..e88ce33bbe 100644--- a/packages/desktop-client/e2e/budget.test.ts+++ b/packages/desktop-client/e2e/budget.test.ts@@ -66,3 +66,64 @@ test.describe('Budget', () => {await page.getByRole('button', { name: 'Back' }).click();});});++test.describe('Budget scroll position', () => {+ let page: Page;+ let configurationPage: ConfigurationPage;+ let budgetPage: BudgetPage;++ test.beforeEach(async ({ browser }) => {+ page = await browser.newPage();+ configurationPage = new ConfigurationPage(page);++ await page.goto('/');+ budgetPage = await configurationPage.createTestFile();++ // Add enough categories to make the budget table scrollable.+ const categoryCount = 20;+ await page.evaluate(async count => {+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ const $send = (window as any).$send as (+ type: string,+ args?: unknown,+ ) => Promise<string>;+ const groupId = await $send('category-group-create', {+ name: 'Extra Categories',+ });+ for (let i = 1; i <= count; i++) {+ await $send('category-create', { name: `Category ${i}`, groupId });+ }+ // Invalidate the category query so React re-renders with the new categories.+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ await (window as any).__TANSTACK_QUERY_CLIENT__.invalidateQueries({+ queryKey: ['categories', 'lists'],+ });+ }, categoryCount);+ await page+ .getByText(`Category ${categoryCount}`, { exact: true })+ .waitFor({ state: 'visible' });++ await page.mouse.move(0, 0);+ });++ test.afterEach(async () => {+ await page?.close();+ });++ test('scroll position is restored when navigating back from spent transactions page', async () => {+ await budgetPage.scrollToBottom();+ const scrollTopBeforeViewingSpent = await budgetPage.getScrollTop();+ expect(scrollTopBeforeViewingSpent).toBeGreaterThan(0);++ // Click a spent-amount cell that is already visible at the current scroll position so the scroll does not change+ // before the handler captures it.+ await budgetPage.clickOnSpentAmountForLastVisibleRow();+ expect(page.url()).toContain('/accounts');++ await page.getByRole('button', { name: 'Back' }).click();+ await budgetPage.waitFor();++ const scrollTopAfterReturningFromSpent = await budgetPage.getScrollTop();+ expect(scrollTopAfterReturningFromSpent).toBe(scrollTopBeforeViewingSpent);+ });+});diff --git a/packages/desktop-client/e2e/page-models/budget-page.ts b/packages/desktop-client/e2e/page-models/budget-page.tsindex 9bf4ccd17e..3a78b9cdb7 100644--- a/packages/desktop-client/e2e/page-models/budget-page.ts+++ b/packages/desktop-client/e2e/page-models/budget-page.ts@@ -10,6 +10,7 @@ export class BudgetPage {readonly budgetTableTotals: Locator;readonly selectedMonthButton: Locator;readonly nextMonthButton: Locator;+ readonly budgetTableScrollContainer: Locator;constructor(page: Page) {this.page = page;@@ -19,6 +20,19 @@ export class BudgetPage {this.budgetTableTotals = this.budgetTable.getByTestId('budget-totals');this.selectedMonthButton = page.getByTestId('selected-budget-month');this.nextMonthButton = page.getByTitle('Next month');+ this.budgetTableScrollContainer = page.getByTestId(+ 'budget-table-scroll-container',+ );+ }++ async getScrollTop() {+ return this.budgetTableScrollContainer.evaluate(el => el.scrollTop);+ }++ async scrollToBottom() {+ await this.budgetTableScrollContainer.evaluate(el => {+ el.scrollTop = el.scrollHeight;+ });}/**@@ -166,6 +180,40 @@ export class BudgetPage {return new AccountPage(this.page);}+ async clickOnSpentAmountForLastVisibleRow() {+ // Click the last spent-amount cell currently visible in the scroll container+ // without triggering Playwright's auto-scroll-into-view, so the scroll+ // position is not changed before the click handler captures it.+ const clicked = await this.page.evaluate(() => {+ const container = document.querySelector(+ '[data-testid="budget-table-scroll-container"]',+ );+ if (!container) {+ throw new Error('Budget scroll container not found');+ }+ const containerRect = container.getBoundingClientRect();+ const cells = container.querySelectorAll<HTMLElement>(+ '[data-testid="category-month-spent"]',+ );+ for (const cell of [...cells].reverse()) {+ const rect = cell.getBoundingClientRect();+ if (+ rect.top >= containerRect.top &&+ rect.bottom <= containerRect.bottom+ ) {+ cell.click();+ return true;+ }+ }+ return false;+ });++ if (!clicked) {+ throw new Error('No visible spent-amount cell found to click');+ }+ return new AccountPage(this.page);+ }+async transferAllBalance(fromIdx: number, toIdx: number) {const toName = await this.getCategoryNameForRow(toIdx);if (!toName) {diff --git a/packages/desktop-client/e2e/page-models/configuration-page.ts b/packages/desktop-client/e2e/page-models/configuration-page.tsindex 989a757337..c475a46219 100644--- a/packages/desktop-client/e2e/page-models/configuration-page.ts+++ b/packages/desktop-client/e2e/page-models/configuration-page.ts@@ -15,7 +15,9 @@ export class ConfigurationPage {}async createTestFile() {- await this.page.getByRole('button', { name: 'Create test file' }).click();+ await this.page+ .getByRole('button', { name: 'Create test file', exact: true })+ .click();const budgetPage = new BudgetPage(this.page);// Wait for the budget page to be fully mounted before returning so// callers don't race the virtualized budget-table's layout step.diff --git a/packages/desktop-client/src/components/budget/BudgetTable.tsx b/packages/desktop-client/src/components/budget/BudgetTable.tsxindex 859d555125..f81877996c 100644--- a/packages/desktop-client/src/components/budget/BudgetTable.tsx+++ b/packages/desktop-client/src/components/budget/BudgetTable.tsx@@ -1,4 +1,4 @@-import React, { useMemo, useState } from 'react';+import React, { useLayoutEffect, useMemo, useRef, useState } from 'react';import type { KeyboardEvent } from 'react';import { styles } from '@actual-app/components/styles';@@ -90,6 +90,18 @@ export function BudgetTable(props: BudgetTableProps) {null,);+ const scrollContainerRef = useRef<HTMLDivElement>(null);++ useLayoutEffect(() => {+ const savedScrollPosition = sessionStorage.getItem(+ 'budget-scroll-position',+ );+ if (savedScrollPosition != null && scrollContainerRef.current) {+ scrollContainerRef.current.scrollTop = Number(savedScrollPosition);+ sessionStorage.removeItem('budget-scroll-position');+ }+ }, []);+const onEditMonth = (id: string, month: string) => {setEditing(id ? { id, cell: month } : null);};@@ -221,6 +233,16 @@ export function BudgetTable(props: BudgetTableProps) {onCollapse(categoryGroups.map(g => g.id));};+ const _onShowActivity = (id: string, month?: string) => {+ if (scrollContainerRef.current) {+ sessionStorage.setItem(+ 'budget-scroll-position',+ String(scrollContainerRef.current.scrollTop),+ );+ }+ onShowActivity(id, month);+ };+const schedulesQuery = useMemo(() => q('schedules').select('*'), []);return (@@ -273,6 +295,8 @@ export function BudgetTable(props: BudgetTableProps) {collapseAllCategories={collapseAllCategories}/><View+ ref={scrollContainerRef}+ data-testid="budget-table-scroll-container"style={{overflowY: 'scroll',overflowAnchor: 'none',@@ -300,7 +324,7 @@ export function BudgetTable(props: BudgetTableProps) {onReorderCategory={_onReorderCategory}onReorderGroup={_onReorderGroup}onBudgetAction={onBudgetAction}- onShowActivity={onShowActivity}+ onShowActivity={_onShowActivity}onApplyBudgetTemplatesInGroup={onApplyBudgetTemplatesInGroup}onSortCategories={onSortCategories}/>diff --git a/upcoming-release-notes/7952.md b/upcoming-release-notes/7952.mdnew file mode 100644index 0000000000..82063d2df2--- /dev/null+++ b/upcoming-release-notes/7952.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [clintharris]+---++Restore budget table scroll position when navigating back from the spent transactions view.