A local-first personal finance app
sidebar pt. 5: account search (#8914)
Matt Fiddamand789e41540d7
diff --git a/packages/desktop-client/src/components/sidebar/redesign/AccountSearchField.tsx b/packages/desktop-client/src/components/sidebar/redesign/AccountSearchField.tsxnew file mode 100644index 0000000000..c603332ab6--- /dev/null+++ b/packages/desktop-client/src/components/sidebar/redesign/AccountSearchField.tsx@@ -0,0 +1,59 @@+import { useTranslation } from 'react-i18next';++import { Button } from '@actual-app/components/button';+import { SvgRemove } from '@actual-app/components/icons/v2';+import { InitialFocus } from '@actual-app/components/initial-focus';+import { Input } from '@actual-app/components/input';+import { theme } from '@actual-app/components/theme';+import { spacing } from '@actual-app/components/tokens';+import { View } from '@actual-app/components/view';++type AccountSearchFieldProps = {+ value: string;+ onChange: (value: string) => void;+ onClose: () => void;+};++export function AccountSearchField({+ value,+ onChange,+ onClose,+}: AccountSearchFieldProps) {+ const { t } = useTranslation();++ return (+ <View+ style={{+ flexDirection: 'row',+ alignItems: 'center',+ gap: spacing.xs,+ marginBottom: spacing.xs,+ }}+ >+ <InitialFocus>+ <Input+ value={value}+ aria-label={t('Find account')}+ placeholder={t('Find account')}+ onChangeValue={onChange}+ onEscape={onClose}+ style={{+ flex: 1,+ fontSize: 12,+ backgroundColor: theme.sidebarControlBackground,+ border: '1px solid transparent',+ color: theme.sidebarItemText,+ }}+ />+ </InitialFocus>+ <Button+ variant="bare"+ aria-label={t('Close search')}+ onPress={onClose}+ style={{ color: theme.sidebarTextSubdued, padding: spacing.xxs }}+ >+ <SvgRemove width={9} height={9} />+ </Button>+ </View>+ );+}diff --git a/packages/desktop-client/src/components/sidebar/redesign/AccountsHeaderRow.tsx b/packages/desktop-client/src/components/sidebar/redesign/AccountsHeaderRow.tsxindex e85f1332fc..f3a62c94ec 100644--- a/packages/desktop-client/src/components/sidebar/redesign/AccountsHeaderRow.tsx+++ b/packages/desktop-client/src/components/sidebar/redesign/AccountsHeaderRow.tsx@@ -5,6 +5,7 @@ import {SvgCheveronDownUp,SvgCheveronUpDown,} from '@actual-app/components/icons/v1';+import { SvgSearchAlternate } from '@actual-app/components/icons/v2';import { theme } from '@actual-app/components/theme';import { spacing } from '@actual-app/components/tokens';import { View } from '@actual-app/components/view';@@ -20,11 +21,17 @@ import { SidebarIconButton } from './SidebarIconButton';type AccountsHeaderRowProps = {allOpen: boolean;onToggleAll: () => void;+ isToggleAllDisabled: boolean;+ isSearchOpen: boolean;+ onToggleSearch: () => void;};export function AccountsHeaderRow({allOpen,onToggleAll,+ isToggleAllDisabled,+ isSearchOpen,+ onToggleSearch,}: AccountsHeaderRowProps) {const { t } = useTranslation();const dispatch = useDispatch();@@ -60,8 +67,15 @@ export function AccountsHeaderRow({<SidebarIconButtonIcon={allOpen ? SvgCheveronDownUp : SvgCheveronUpDown}label={allOpen ? t('Collapse all groups') : t('Expand all groups')}+ isDisabled={isToggleAllDisabled}onPress={onToggleAll}/>+ <SidebarIconButton+ Icon={SvgSearchAlternate}+ label={t('Find account')}+ isToggledOn={isSearchOpen}+ onPress={onToggleSearch}+ /><SidebarIconButtonIcon={SvgAdd}label={t('Add account')}diff --git a/packages/desktop-client/src/components/sidebar/redesign/AccountsSection.tsx b/packages/desktop-client/src/components/sidebar/redesign/AccountsSection.tsxindex 63ab329dce..a9603820e6 100644--- a/packages/desktop-client/src/components/sidebar/redesign/AccountsSection.tsx+++ b/packages/desktop-client/src/components/sidebar/redesign/AccountsSection.tsx@@ -1,3 +1,4 @@+import { useState } from 'react';import { useTranslation } from 'react-i18next';import { spacing } from '@actual-app/components/tokens';@@ -5,16 +6,32 @@ import { View } from '@actual-app/components/view';import * as bindings from '#spreadsheet/bindings';+import { AccountSearchField } from './AccountSearchField';import { AccountsHeaderRow } from './AccountsHeaderRow';import { ClosedSection } from './ClosedSection';import { SideGroup } from './SideGroup';-import { useSidebarAccountTree } from './useSidebarAccountTree';+import {+ filterSidebarTree,+ useSidebarAccountTree,+} from './useSidebarAccountTree';import { bucketKey, useSidebarCollapseState } from './useSidebarCollapseState';export function AccountsSection() {const { t } = useTranslation();const tree = useSidebarAccountTree();- const collapse = useSidebarCollapseState({ tree, isSearching: false });+ const [isSearchOpen, setIsSearchOpen] = useState(false);+ const [query, setQuery] = useState('');+ const trimmedQuery = query.trim().toLowerCase();+ const isSearching = trimmedQuery !== '';++ const collapse = useSidebarCollapseState({ tree, isSearching });++ const onToggleSearch = () => {+ setIsSearchOpen(!isSearchOpen);+ setQuery('');+ };++ const visibleTree = filterSidebarTree(tree, trimmedQuery);const showSyncDot = [...tree.onBudget.buckets.map(b => b.accounts).flat(),@@ -34,13 +51,23 @@ export function AccountsSection() {<AccountsHeaderRowallOpen={collapse.allOpen}onToggleAll={collapse.toggleAll}+ isToggleAllDisabled={isSearching}+ isSearchOpen={isSearchOpen}+ onToggleSearch={onToggleSearch}/>+ {isSearchOpen && (+ <AccountSearchField+ value={query}+ onChange={setQuery}+ onClose={onToggleSearch}+ />+ )}{tree.onBudget.buckets.length > 0 && (<SideGrouplabel={t('On budget')}side="on"showSyncDot={showSyncDot}- sideData={tree.onBudget}+ sideData={visibleTree.onBudget}totalBinding={bindings.onBudgetAccountBalance()}balanceTestId="sidebar-on-budget-balance"isOpen={collapse.isOpen('onbudget')}@@ -54,7 +81,7 @@ export function AccountsSection() {label={t('Off budget')}side="off"showSyncDot={showSyncDot}- sideData={tree.offBudget}+ sideData={visibleTree.offBudget}totalBinding={bindings.offBudgetAccountBalance()}balanceTestId="sidebar-off-budget-balance"isOpen={collapse.isOpen('offbudget')}@@ -64,7 +91,7 @@ export function AccountsSection() {/>)}<ClosedSection- accounts={tree.closed}+ accounts={visibleTree.closed}isOpen={collapse.isOpen('closed')}onToggle={() => collapse.toggle('closed')}/>diff --git a/packages/desktop-client/src/components/sidebar/redesign/useSidebarAccountTree.test.ts b/packages/desktop-client/src/components/sidebar/redesign/useSidebarAccountTree.test.tsindex 64d4a30651..d0272ba3db 100644--- a/packages/desktop-client/src/components/sidebar/redesign/useSidebarAccountTree.test.ts+++ b/packages/desktop-client/src/components/sidebar/redesign/useSidebarAccountTree.test.ts@@ -5,7 +5,12 @@ import type {} from '@actual-app/core/types/models';import { describe, expect, it } from 'vitest';-import { buildAccountSide, getEffectiveGroupId } from './useSidebarAccountTree';+import {+ buildAccountSide,+ filterSidebarTree,+ getEffectiveGroupId,+} from './useSidebarAccountTree';+import type { SidebarAccountTree } from './useSidebarAccountTree';function makeAccount(name: string,@@ -82,3 +87,64 @@ describe('buildAccountSide', () => {expect(side.failedCount).toBe(1);});});++describe('filterSidebarTree', () => {+ const groups = [makeGroup('g1', 'Savings')];+ const tree: SidebarAccountTree = {+ onBudget: buildAccountSide(+ [+ makeAccount('Checking'),+ makeAccount('Premium Saver', { account_group_id: 'g1' }),+ ],+ groups,+ ),+ offBudget: buildAccountSide([makeAccount('House')], groups),+ closed: [makeAccount('Old Checking', { closed: 1 })],+ };++ it('returns the tree unchanged for an empty query', () => {+ expect(filterSidebarTree(tree, '')).toBe(tree);+ });++ it('filters rows but keeps side counts for the full side', () => {+ const filtered = filterSidebarTree(tree, 'saver');++ expect(+ filtered.onBudget.buckets.flatMap(bucket =>+ bucket.accounts.map(account => account.name),+ ),+ ).toEqual(['Premium Saver']);+ expect(filtered.onBudget.accountCount).toBe(2);+ expect(filtered.offBudget.buckets).toEqual([]);+ expect(filtered.closed).toEqual([]);+ });++ it('matches closed accounts too', () => {+ const filtered = filterSidebarTree(tree, 'old');+ expect(filtered.closed.map(account => account.name)).toEqual([+ 'Old Checking',+ ]);+ });++ it('matches every account in a group whose name matches', () => {+ const filtered = filterSidebarTree(tree, 'savings');++ expect(filtered.onBudget.buckets.map(bucket => bucket.group?.id)).toEqual([+ 'g1',+ ]);+ expect(filtered.onBudget.buckets[0].accounts.map(a => a.name)).toEqual([+ 'Premium Saver',+ ]);+ });++ it('matches a query spanning the group and account names', () => {+ const filtered = filterSidebarTree(tree, 'savings prem');++ expect(filtered.onBudget.buckets[0].accounts.map(a => a.name)).toEqual([+ 'Premium Saver',+ ]);+ expect(filterSidebarTree(tree, 'checking prem').onBudget.buckets).toEqual(+ [],+ );+ });+});diff --git a/packages/desktop-client/src/components/sidebar/redesign/useSidebarAccountTree.ts b/packages/desktop-client/src/components/sidebar/redesign/useSidebarAccountTree.tsindex 59e3f6c934..5dde6147b1 100644--- a/packages/desktop-client/src/components/sidebar/redesign/useSidebarAccountTree.ts+++ b/packages/desktop-client/src/components/sidebar/redesign/useSidebarAccountTree.ts@@ -75,6 +75,38 @@ export function buildAccountSide(};}+function filterSide(side: SidebarAccountSide, query: string) {+ return {+ ...side,+ buckets: side.buckets+ .map(bucket => ({+ ...bucket,+ accounts: bucket.accounts.filter(account =>+ `${bucket.group?.name ?? ''} ${account.name}`+ .toLowerCase()+ .includes(query),+ ),+ }))+ .filter(bucket => bucket.accounts.length > 0),+ };+}++export function filterSidebarTree(+ tree: SidebarAccountTree,+ query: string,+): SidebarAccountTree {+ if (!query) {+ return tree;+ }+ return {+ onBudget: filterSide(tree.onBudget, query),+ offBudget: filterSide(tree.offBudget, query),+ closed: tree.closed.filter(account =>+ account.name.toLowerCase().includes(query),+ ),+ };+}+export function useSidebarAccountTree(): SidebarAccountTree {const { data: groups = [] } = useAccountGroups();const { data: onBudgetAccounts = [] } = useOnBudgetAccounts();diff --git a/upcoming-release-notes/add-new-sidebar-search.md b/upcoming-release-notes/add-new-sidebar-search.mdnew file mode 100644index 0000000000..5962e216f6--- /dev/null+++ b/upcoming-release-notes/add-new-sidebar-search.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [matt-fidd]+---++Add account search to the experimental new sidebar