Skip to content

Commit

Permalink
Auto notes when transferring budgets (#3119)
Browse files Browse the repository at this point in the history
* Add expense category budget notes that resets every month + auto notes when transferring budgets

* Remove unused import

* Release notes

* VRT

* Fix typecheck error

* Rename

* Fix typecheck error

* Fix category name

* Update to notes to reassigned

* Append auto transfer notes to month notes

* Update release notes

* VRT

* VRT

* Fix lint error
  • Loading branch information
joel-jeremy authored Sep 6, 2024
1 parent fca1bcc commit 61bffa3
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 14 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -25,6 +34,8 @@ export function BalanceMovementMenu({
);
const [menu, setMenu] = useState('menu');

const { addBudgetTransferNotes } = useBudgetTransferNotes({ month });

return (
<>
{menu === 'menu' && (
Expand Down Expand Up @@ -53,6 +64,11 @@ export function BalanceMovementMenu({
from: categoryId,
to: toCategoryId,
});
addBudgetTransferNotes({
fromCategoryId: categoryId,
toCategoryId,
amount,
});
}}
/>
)}
Expand All @@ -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 };
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -302,7 +299,7 @@ export const ExpenseCategoryMonth = memo(function ExpenseCategoryMonth({
/>
</Popover>
</View>
) : null}
)}
<RolloverSheetCell
name="budget"
exposed={editing}
Expand Down
2 changes: 2 additions & 0 deletions packages/loot-core/src/types/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>>;
6 changes: 6 additions & 0 deletions upcoming-release-notes/3119.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [joel-jeremy]
---

Auto notes in month notes when reassigning budgets.

0 comments on commit 61bffa3

Please sign in to comment.