Skip to content

A local-first personal finance app

Auto notes when transferring budgets (#3119)

Joel Jeremy Marquez61bffa3d31e9
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 f823329c3f..a6bf93a8ad 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 5aaedcb03a..77e03e79be 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 28cbfa7506..75cd2a5fd8 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 d6ae0650bc..2d8157df8b 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 042dc81f53..672a25521c 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 ab10719671..f4638d934f 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 3a7d6881c2..fd0c0345d8 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 0000000000..72945925ce
--- /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.