A local-first personal finance app
[AI] feat(budget analysis report): show hidden categories (#8164)
tabedzki644256ddc1f5
diff --git a/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx b/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsxindex f7a7c6b572..eca613f27f 100644--- a/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx+++ b/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx@@ -1,4 +1,4 @@-import React, { useEffect, useMemo, useState } from 'react';+import React, { useEffect, useMemo, useRef, useState } from 'react';import { Trans, useTranslation } from 'react-i18next';import { useParams } from 'react-router';@@ -6,11 +6,10 @@ import { AlignedText } from '@actual-app/components/aligned-text';import { Block } from '@actual-app/components/block';import { Button } from '@actual-app/components/button';import { useResponsive } from '@actual-app/components/hooks/useResponsive';-import { SvgChart, SvgChartBar } from '@actual-app/components/icons/v1';+import { Menu } from '@actual-app/components/menu';import { Paragraph } from '@actual-app/components/paragraph';-import { Select } from '@actual-app/components/select';+import { Popover } from '@actual-app/components/popover';import { theme } from '@actual-app/components/theme';-import { Tooltip } from '@actual-app/components/tooltip';import { View } from '@actual-app/components/view';import { send } from '@actual-app/core/platform/client/connection';import * as monthUtils from '@actual-app/core/shared/months';@@ -44,10 +43,80 @@ import { addNotification } from '#notifications/notificationsSlice';import { useDispatch } from '#redux';import { useUpdateDashboardWidgetMutation } from '#reports/mutations';-type BalanceMode =- | 'balance-only'- | 'balance-and-categories'- | 'categories-only';+type OptionsButtonProps = {+ graphType: 'Line' | 'Bar';+ onToggleGraphType: () => void;+ showBalance: boolean;+ onToggleShowBalance: () => void;+ showCategories: boolean;+ onToggleShowCategories: () => void;+ showHiddenCategories: boolean;+ onToggleShowHiddenCategories: () => void;+};++function OptionsButton({+ graphType,+ onToggleGraphType,+ showBalance,+ onToggleShowBalance,+ showCategories,+ onToggleShowCategories,+ showHiddenCategories,+ onToggleShowHiddenCategories,+}: OptionsButtonProps) {+ const { t } = useTranslation();+ const triggerRef = useRef<HTMLButtonElement | null>(null);+ const [isOpen, setIsOpen] = useState(false);++ return (+ <>+ <Button ref={triggerRef} onPress={() => setIsOpen(true)}>+ <Trans>Options</Trans>+ </Button>+ <Popover+ triggerRef={triggerRef}+ placement="bottom end"+ isOpen={isOpen}+ onOpenChange={() => setIsOpen(false)}+ >+ <Menu+ onMenuSelect={item => {+ if (item === 'graph-type') onToggleGraphType();+ if (item === 'show-balance') onToggleShowBalance();+ if (item === 'show-categories') onToggleShowCategories();+ if (item === 'show-hidden-categories') {+ onToggleShowHiddenCategories();+ }+ }}+ items={[+ {+ name: 'graph-type',+ text: t('Bar chart'),+ toggle: graphType === 'Bar',+ },+ Menu.line,+ {+ name: 'show-balance',+ text: t('Show balance'),+ toggle: showBalance,+ },+ {+ name: 'show-categories',+ text: t('Show categories'),+ toggle: showCategories,+ },+ Menu.line,+ {+ name: 'show-hidden-categories',+ text: t('Show hidden categories'),+ toggle: showHiddenCategories,+ },+ ]}+ />+ </Popover>+ </>+ );+}export function BudgetAnalysis() {const params = useParams();@@ -99,12 +168,14 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {const [showBalance, setShowBalance] = useState(widget?.meta?.showBalance ?? true,);- const [balanceOnly, setBalanceOnly] = useState(- widget?.meta?.balanceOnly ?? false,+ const [showCategories, setShowCategories] = useState(+ !(widget?.meta?.balanceOnly ?? false),+ );+ const [showHiddenCategories, setShowHiddenCategories] = useState(+ widget?.meta?.showHiddenCategories ?? false,);const [latestTransaction, setLatestTransaction] = useState('');const [isConcise, setIsConcise] = useState(() => {- // Default to concise (monthly) view until we load the actual date rangereturn true;});@@ -192,8 +263,9 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {conditionsOp,startDate,endDate,+ showHiddenCategories,}),- [conditions, conditionsOp, startDate, endDate],+ [conditions, conditionsOp, startDate, endDate, showHiddenCategories],);const data = useReport('default', getGraphData);@@ -234,7 +306,8 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {},graphType,showBalance,- balanceOnly,+ balanceOnly: !showCategories,+ showHiddenCategories,},},},@@ -279,17 +352,6 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {});};- const balanceMode: BalanceMode = balanceOnly- ? 'balance-only'- : showBalance- ? 'balance-and-categories'- : 'categories-only';-- function onBalanceModeChange(newMode: BalanceMode) {- setBalanceOnly(newMode === 'balance-only');- setShowBalance(newMode !== 'categories-only');- }-return (<Pageheader={@@ -334,38 +396,21 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {onDeleteFilter={onDeleteFilter}onConditionsOpChange={onConditionsOpChange}filterInclude={['category', 'saved']}- inlineContent={- <Tooltip- content={- graphType === 'Line'- ? t('Switch to bar chart')- : t('Switch to line chart')- }- >- <Button- variant="bare"- onPress={() =>- setGraphType(graphType === 'Line' ? 'Bar' : 'Line')- }- >- {graphType === 'Line' ? (- <SvgChartBar style={{ width: 12, height: 12 }} />- ) : (- <SvgChart style={{ width: 12, height: 12 }} />- )}- </Button>- </Tooltip>- }><View style={{ flexDirection: 'row', gap: 10 }}>- <Select<BalanceMode>- value={balanceMode}- onChange={onBalanceModeChange}- options={[- ['balance-only', t('Balance only')],- ['balance-and-categories', t('Balance + Categories')],- ['categories-only', t('Categories only')],- ]}+ <OptionsButton+ graphType={graphType}+ onToggleGraphType={() =>+ setGraphType(graphType === 'Line' ? 'Bar' : 'Line')+ }+ showBalance={showBalance}+ onToggleShowBalance={() => setShowBalance(v => !v)}+ showCategories={showCategories}+ onToggleShowCategories={() => setShowCategories(v => !v)}+ showHiddenCategories={showHiddenCategories}+ onToggleShowHiddenCategories={() =>+ setShowHiddenCategories(v => !v)+ }/>{widget && (@@ -494,7 +539,7 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {data={data}graphType={graphType}showBalance={showBalance}- balanceOnly={balanceOnly}+ balanceOnly={!showCategories}isConcise={isConcise}/><View style={{ marginTop: 30 }}>@@ -520,8 +565,9 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {changes in a specific area.</Paragraph><Paragraph>- Hint: You can use the icon in the header to toggle between- line and bar chart views.+ Use the <strong>Options</strong> button to switch between+ line and bar chart, toggle balance and category series+ visibility, and include hidden budget categories.</Paragraph></Trans></View>diff --git a/packages/desktop-client/src/components/reports/reports/BudgetAnalysisCard.tsx b/packages/desktop-client/src/components/reports/reports/BudgetAnalysisCard.tsxindex e2768a5e16..ef0c7b39fb 100644--- a/packages/desktop-client/src/components/reports/reports/BudgetAnalysisCard.tsx+++ b/packages/desktop-client/src/components/reports/reports/BudgetAnalysisCard.tsx@@ -65,8 +65,15 @@ export function BudgetAnalysisCard({conditionsOp: meta?.conditionsOp,startDate,endDate,+ showHiddenCategories: meta?.showHiddenCategories ?? false,});- }, [meta?.conditions, meta?.conditionsOp, startDate, endDate]);+ }, [+ meta?.conditions,+ meta?.conditionsOp,+ meta?.showHiddenCategories,+ startDate,+ endDate,+ ]);const data = useReport('default', getGraphData);diff --git a/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-spreadsheet.test.ts b/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-spreadsheet.test.tsnew file mode 100644index 0000000000..edb218158b--- /dev/null+++ b/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-spreadsheet.test.ts@@ -0,0 +1,77 @@+import type { CategoryEntity } from '@actual-app/core/types/models';++import { isBaseCategory } from './budget-analysis-spreadsheet';++const makeCategory = (+ overrides: Partial<CategoryEntity> & Pick<CategoryEntity, 'id' | 'name'>,+): CategoryEntity => ({+ is_income: false,+ hidden: false,+ group: 'group1',+ ...overrides,+});++const visibleExpense = makeCategory({ id: 'c1', name: 'Groceries' });+const hiddenExpense = makeCategory({+ id: 'c2',+ name: 'Car Fund',+ hidden: true,+});+const incomeCategory = makeCategory({+ id: 'c3',+ name: 'Salary',+ is_income: true,+});+const hiddenIncome = makeCategory({+ id: 'c4',+ name: 'Hidden Income',+ is_income: true,+ hidden: true,+});++const all = [visibleExpense, hiddenExpense, incomeCategory, hiddenIncome];++function filterBaseCategories(+ categories: CategoryEntity[],+ showHiddenCategories: boolean,+): CategoryEntity[] {+ return categories.filter(cat => isBaseCategory(cat, showHiddenCategories));+}++describe('createBudgetAnalysisSpreadsheet', () => {+ describe('hidden category filtering', () => {+ it('excludes hidden categories when showHiddenCategories is false', () => {+ const result = filterBaseCategories(all, false);+ expect(result).toContain(visibleExpense);+ expect(result).not.toContain(hiddenExpense);+ });++ it('includes hidden expense categories when showHiddenCategories is true', () => {+ const result = filterBaseCategories(all, true);+ expect(result).toContain(visibleExpense);+ expect(result).toContain(hiddenExpense);+ });++ it('always excludes income categories regardless of showHiddenCategories', () => {+ const resultFalse = filterBaseCategories(all, false);+ const resultTrue = filterBaseCategories(all, true);+ expect(resultFalse).not.toContain(incomeCategory);+ expect(resultFalse).not.toContain(hiddenIncome);+ expect(resultTrue).not.toContain(incomeCategory);+ expect(resultTrue).not.toContain(hiddenIncome);+ });++ it('returns only visible expense categories by default', () => {+ const result = filterBaseCategories(all, false);+ expect(result).toHaveLength(1);+ expect(result[0]).toBe(visibleExpense);+ });++ it('returns all expense categories when flag is true', () => {+ const result = filterBaseCategories(all, true);+ expect(result).toHaveLength(2);+ expect(result).toContain(visibleExpense);+ expect(result).toContain(hiddenExpense);+ });+ });+});diff --git a/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-spreadsheet.ts b/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-spreadsheet.tsindex d48e9c3cea..7541cda30b 100644--- a/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-spreadsheet.ts+++ b/packages/desktop-client/src/components/reports/spreadsheets/budget-analysis-spreadsheet.ts@@ -33,13 +33,22 @@ type createBudgetAnalysisSpreadsheetProps = {conditionsOp?: 'and' | 'or';startDate: string;endDate: string;+ showHiddenCategories?: boolean;};+export function isBaseCategory(+ cat: CategoryEntity,+ showHiddenCategories: boolean,+): boolean {+ return !cat.is_income && (showHiddenCategories || !cat.hidden);+}+export function createBudgetAnalysisSpreadsheet({conditions = [],conditionsOp = 'and',startDate,endDate,+ showHiddenCategories = false,}: createBudgetAnalysisSpreadsheetProps) {return async (spreadsheet: ReturnType<typeof useSpreadsheet>,@@ -64,9 +73,10 @@ export function createBudgetAnalysisSpreadsheet({(cond.field === 'category' || cond.field === 'category_group'),);- // Base set: expense categories only (exclude income and hidden)- const baseCategories = allCategories.filter(- (cat: CategoryEntity) => !cat.is_income && !cat.hidden,+ // Base set: expense categories only; hidden categories are included when+ // showHiddenCategories is true so historic data is not misrepresented.+ const baseCategories = allCategories.filter((cat: CategoryEntity) =>+ isBaseCategory(cat, showHiddenCategories),);let categoriesToInclude: CategoryEntity[];diff --git a/packages/loot-core/src/types/models/dashboard.ts b/packages/loot-core/src/types/models/dashboard.tsindex 3fee0ed68f..865e9be3b0 100644--- a/packages/loot-core/src/types/models/dashboard.ts+++ b/packages/loot-core/src/types/models/dashboard.ts@@ -95,6 +95,7 @@ export type BudgetAnalysisWidget = AbstractWidget<graphType?: 'Line' | 'Bar';showBalance?: boolean;balanceOnly?: boolean;+ showHiddenCategories?: boolean;} | null>;export type CustomReportWidget = AbstractWidget<diff --git a/upcoming-release-notes/ai-feat-budget-analysis-report-show-hidden-categories.md b/upcoming-release-notes/ai-feat-budget-analysis-report-show-hidden-categories.mdnew file mode 100644index 0000000000..31199968f0--- /dev/null+++ b/upcoming-release-notes/ai-feat-budget-analysis-report-show-hidden-categories.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [tabedzki]+---++Budget analysis report: add "show hidden categories" option, group options into dropdown