diff --git a/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-1-chromium-linux.png b/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-1-chromium-linux.png index f823329c3fbef774736c4948dc67e91fa0d18735..a6bf93a8ad9155a83e5c7184f0cb8bf0a4c8d3c2 100644 Binary files a/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-1-chromium-linux.png and b/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-1-chromium-linux.png differ diff --git a/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-2-chromium-linux.png b/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-2-chromium-linux.png index 5aaedcb03a69d0dccadb095306b86b33dd5f1ee7..77e03e79be6d838b06f7df845d453ae255318410 100644 Binary files a/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-2-chromium-linux.png and b/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-2-chromium-linux.png differ diff --git a/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-3-chromium-linux.png b/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-3-chromium-linux.png index 28cbfa7506a6ae78b2dc11466b90a9b79d94c7ae..75cd2a5fd873710191de011d2c977c0c85542b05 100644 Binary files a/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-3-chromium-linux.png and b/packages/desktop-client/e2e/budget.test.js-snapshots/Budget-transfer-funds-to-another-category-3-chromium-linux.png differ diff --git a/packages/desktop-client/src/components/budget/report/ReportComponents.tsx b/packages/desktop-client/src/components/budget/report/ReportComponents.tsx index d6ae0650bc21ef4ae15da84cd8d82991c1c6b3d6..2d8157df8bb5e61c961b103f80239cf6327171d6 100644 --- a/packages/desktop-client/src/components/budget/report/ReportComponents.tsx +++ b/packages/desktop-client/src/components/budget/report/ReportComponents.tsx @@ -204,7 +204,6 @@ export const CategoryMonth = memo(function CategoryMonth({ onShowActivity, }: CategoryMonthProps) { const [menuOpen, setMenuOpen] = useState(false); - const [hover, setHover] = useState(false); const triggerRef = useRef(null); const [balanceMenuOpen, setBalanceMenuOpen] = useState(false); @@ -240,16 +239,14 @@ export const CategoryMonth = memo(function CategoryMonth({ flex: 1, flexDirection: 'row', }} - onMouseOverCapture={() => setHover(true)} - onMouseLeave={() => { - setHover(false); - }} > - {!editing && (hover || menuOpen) && ( + {!editing && ( <View style={{ + flexDirection: 'row', flexShrink: 0, paddingLeft: 3, + alignItems: 'center', justifyContent: 'center', borderTopWidth: 1, borderBottomWidth: 1, diff --git a/packages/desktop-client/src/components/budget/rollover/BalanceMovementMenu.tsx b/packages/desktop-client/src/components/budget/rollover/BalanceMovementMenu.tsx index 042dc81f533371ddb4255e7a49d79af318f5ad02..672a25521c928714438f9524750b1555e97ce4a6 100644 --- a/packages/desktop-client/src/components/budget/rollover/BalanceMovementMenu.tsx +++ b/packages/desktop-client/src/components/budget/rollover/BalanceMovementMenu.tsx @@ -1,6 +1,15 @@ -import React, { useState } from 'react'; +import React, { useCallback, useMemo, useState } from 'react'; +import { runQuery } from 'loot-core/client/query-helpers'; +import { send } from 'loot-core/platform/client/fetch'; +import { q } from 'loot-core/shared/query'; import { rolloverBudget } from 'loot-core/src/client/queries'; +import * as monthUtils from 'loot-core/src/shared/months'; +import { groupById, integerToCurrency } from 'loot-core/src/shared/util'; +import { type CategoryEntity } from 'loot-core/types/models'; +import { type WithRequired } from 'loot-core/types/util'; + +import { useCategories } from '../../../hooks/useCategories'; import { BalanceMenu } from './BalanceMenu'; import { CoverMenu } from './CoverMenu'; @@ -25,6 +34,8 @@ export function BalanceMovementMenu({ ); const [menu, setMenu] = useState('menu'); + const { addBudgetTransferNotes } = useBudgetTransferNotes({ month }); + return ( <> {menu === 'menu' && ( @@ -53,6 +64,11 @@ export function BalanceMovementMenu({ from: categoryId, to: toCategoryId, }); + addBudgetTransferNotes({ + fromCategoryId: categoryId, + toCategoryId, + amount, + }); }} /> )} @@ -72,3 +88,50 @@ export function BalanceMovementMenu({ </> ); } + +const useBudgetTransferNotes = ({ month }: { month: string }) => { + const { list: categories } = useCategories(); + const categoriesById = useMemo(() => { + return groupById(categories as WithRequired<CategoryEntity, 'id'>[]); + }, [categories]); + + const getNotes = async (id: string) => { + const { data: notes } = await runQuery( + q('notes').filter({ id }).select('note'), + ); + return (notes && notes[0]?.note) ?? ''; + }; + + const addNewLine = (notes?: string) => `${notes}${notes && '\n'}`; + + const addBudgetTransferNotes = useCallback( + async ({ + fromCategoryId, + toCategoryId, + amount, + }: { + fromCategoryId: Required<CategoryEntity['id']>; + toCategoryId: Required<CategoryEntity['id']>; + amount: number; + }) => { + const displayAmount = integerToCurrency(amount); + + const monthBudgetNotesId = `budget-${month}`; + const existingMonthBudgetNotes = addNewLine( + await getNotes(monthBudgetNotesId), + ); + + const displayDay = monthUtils.format(monthUtils.currentDate(), 'MMMM dd'); + const fromCategoryName = categoriesById[fromCategoryId || ''].name; + const toCategoryName = categoriesById[toCategoryId || ''].name; + + await send('notes-save', { + id: monthBudgetNotesId, + note: `${existingMonthBudgetNotes}- Reassigned ${displayAmount} from ${fromCategoryName} to ${toCategoryName} on ${displayDay}`, + }); + }, + [categoriesById, month], + ); + + return { addBudgetTransferNotes }; +}; diff --git a/packages/desktop-client/src/components/budget/rollover/RolloverComponents.tsx b/packages/desktop-client/src/components/budget/rollover/RolloverComponents.tsx index ab107196717983dc0719a8b435481b7536db0558..f4638d934f84be6d15f841922156f64e47d57720 100644 --- a/packages/desktop-client/src/components/budget/rollover/RolloverComponents.tsx +++ b/packages/desktop-client/src/components/budget/rollover/RolloverComponents.tsx @@ -196,7 +196,6 @@ export const ExpenseCategoryMonth = memo(function ExpenseCategoryMonth({ const balanceMenuTriggerRef = useRef(null); const [budgetMenuOpen, setBudgetMenuOpen] = useState(false); const [balanceMenuOpen, setBalanceMenuOpen] = useState(false); - const [hover, setHover] = useState(false); const onMenuAction = (...args: Parameters<typeof onBudgetAction>) => { onBudgetAction(...args); @@ -227,16 +226,14 @@ export const ExpenseCategoryMonth = memo(function ExpenseCategoryMonth({ flex: 1, flexDirection: 'row', }} - onMouseOverCapture={() => setHover(true)} - onMouseLeave={() => { - setHover(false); - }} > - {!editing && (hover || budgetMenuOpen) ? ( + {!editing && ( <View style={{ + flexDirection: 'row', flexShrink: 1, paddingLeft: 3, + alignItems: 'center', justifyContent: 'center', borderTopWidth: 1, borderBottomWidth: 1, @@ -302,7 +299,7 @@ export const ExpenseCategoryMonth = memo(function ExpenseCategoryMonth({ /> </Popover> </View> - ) : null} + )} <RolloverSheetCell name="budget" exposed={editing} diff --git a/packages/loot-core/src/types/util.d.ts b/packages/loot-core/src/types/util.d.ts index 3a7d6881c27475793158452f407e3daf8513bb9d..fd0c0345d831dfd2c3d9a0c440e6a484738230ab 100644 --- a/packages/loot-core/src/types/util.d.ts +++ b/packages/loot-core/src/types/util.d.ts @@ -7,3 +7,5 @@ export type StripNever<T> = { export type EverythingButIdOptional<T> = { id: T['id'] } & Partial< Omit<T, 'id'> >; + +export type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>; diff --git a/upcoming-release-notes/3119.md b/upcoming-release-notes/3119.md new file mode 100644 index 0000000000000000000000000000000000000000..72945925ce6679c3c3278be77fd6f3d6ccc181b7 --- /dev/null +++ b/upcoming-release-notes/3119.md @@ -0,0 +1,6 @@ +--- +category: Enhancements +authors: [joel-jeremy] +--- + +Auto notes in month notes when reassigning budgets.