Skip to content

A local-first personal finance app

automation UI: fix various bugs (#8258)

Matt Fiddaman0ccdc8dde225
diff --git a/packages/desktop-client/src/components/budget/goals/ActiveEditor.tsx b/packages/desktop-client/src/components/budget/goals/ActiveEditor.tsx
index 306964ca85..e4815c9262 100644
--- a/packages/desktop-client/src/components/budget/goals/ActiveEditor.tsx
+++ b/packages/desktop-client/src/components/budget/goals/ActiveEditor.tsx
@@ -22,6 +22,7 @@ type ActiveEditorProps = {
categories: CategoryGroupEntity[];
hasLimitAutomation: boolean;
onAddLimitAutomation: () => void;
+ defaultWeeklyStart: string;
};
export function ActiveEditor({
@@ -31,10 +32,17 @@ export function ActiveEditor({
categories,
hasLimitAutomation,
onAddLimitAutomation,
+ defaultWeeklyStart,
}: ActiveEditorProps) {
switch (state.displayType) {
case 'limit':
- return <LimitAutomation template={state.template} dispatch={dispatch} />;
+ return (
+ <LimitAutomation
+ template={state.template}
+ dispatch={dispatch}
+ defaultWeeklyStart={defaultWeeklyStart}
+ />
+ );
case 'refill':
return (
<RefillAutomation
diff --git a/packages/desktop-client/src/components/budget/goals/CategoryAutomationButton.tsx b/packages/desktop-client/src/components/budget/goals/CategoryAutomationButton.tsx
index a6e8a1ce9b..d845f8c118 100644
--- a/packages/desktop-client/src/components/budget/goals/CategoryAutomationButton.tsx
+++ b/packages/desktop-client/src/components/budget/goals/CategoryAutomationButton.tsx
@@ -17,6 +17,7 @@ import {
emptyCleanupConfig,
} from '#components/budget/goals/cleanupModel';
import type { CleanupConfig } from '#components/budget/goals/cleanupModel';
+import { displayTemplateTypes } from '#components/budget/goals/constants';
import { MonthsContext } from '#components/budget/MonthsContext';
import { migrateTemplatesToAutomations } from '#components/modals/BudgetAutomationsModal/migrateTemplatesToAutomations';
import { useCategories } from '#hooks/useCategories';
@@ -102,6 +103,11 @@ export function CategoryAutomationButton({
}
const automations = getAutomationEntries(category.goal_def);
+ const sortedAutomations = [...automations].sort(
+ (a, b) =>
+ displayTemplateTypes.indexOf(a.displayType) -
+ displayTemplateTypes.indexOf(b.displayType),
+ );
const categoryNameMap: Record<string, string> = {};
for (const cat of categoriesData?.list ?? []) {
categoryNameMap[cat.id] = cat.name;
@@ -152,7 +158,7 @@ export function CategoryAutomationButton({
placement="bottom start"
content={
<View style={{ maxWidth: 320, padding: 10, gap: 10 }}>
- {automations.map(entry => (
+ {sortedAutomations.map(entry => (
<View key={entry.id} style={{ gap: 4 }}>
<Text style={{ display: 'block' }}>
<TemplateSentence
diff --git a/packages/desktop-client/src/components/budget/goals/editor/BySaveAutomation.tsx b/packages/desktop-client/src/components/budget/goals/editor/BySaveAutomation.tsx
index 8f86d547a8..faadaf21de 100644
--- a/packages/desktop-client/src/components/budget/goals/editor/BySaveAutomation.tsx
+++ b/packages/desktop-client/src/components/budget/goals/editor/BySaveAutomation.tsx
@@ -89,9 +89,19 @@ export const BySaveAutomation = ({
const committedRepeat = template.repeat ?? 1;
const [rawRepeat, setRawRepeat] = useState(String(committedRepeat));
+
useEffect(() => {
setRawRepeat(String(committedRepeat));
}, [committedRepeat]);
+
+ const onRepeatChange = (value: string) => {
+ setRawRepeat(value);
+ const parsed = Math.trunc(Number(value));
+ if (value.trim() !== '' && parsed >= 1 && parsed !== committedRepeat) {
+ dispatch(updateTemplate({ type: template.type, repeat: parsed }));
+ }
+ };
+
const commitRepeat = () => {
const parsed = Math.max(1, Math.trunc(Number(rawRepeat)) || 1);
setRawRepeat(String(parsed));
@@ -176,7 +186,7 @@ export const BySaveAutomation = ({
min={1}
step={1}
value={rawRepeat}
- onChangeValue={setRawRepeat}
+ onChangeValue={onRepeatChange}
onBlur={commitRepeat}
/>
</FormField>
diff --git a/packages/desktop-client/src/components/budget/goals/editor/FixedAutomation.tsx b/packages/desktop-client/src/components/budget/goals/editor/FixedAutomation.tsx
index 7be1164748..aca86db17a 100644
--- a/packages/desktop-client/src/components/budget/goals/editor/FixedAutomation.tsx
+++ b/packages/desktop-client/src/components/budget/goals/editor/FixedAutomation.tsx
@@ -47,6 +47,20 @@ export const FixedAutomation = ({
useEffect(() => {
setRawPeriodAmount(String(periodAmount));
}, [periodAmount]);
+
+ const onPeriodAmountChange = (value: string) => {
+ setRawPeriodAmount(value);
+ const parsed = Math.trunc(Number(value));
+ if (value.trim() !== '' && parsed >= 1 && parsed !== periodAmount) {
+ dispatch(
+ updateTemplate({
+ type: 'periodic',
+ period: { period: periodUnit, amount: parsed },
+ }),
+ );
+ }
+ };
+
const commitPeriodAmount = () => {
const parsed = Math.max(1, Math.trunc(Number(rawPeriodAmount)) || 1);
setRawPeriodAmount(String(parsed));
@@ -86,7 +100,7 @@ export const FixedAutomation = ({
min={1}
step={1}
value={rawPeriodAmount}
- onChangeValue={setRawPeriodAmount}
+ onChangeValue={onPeriodAmountChange}
onBlur={commitPeriodAmount}
/>
</FormField>
diff --git a/packages/desktop-client/src/components/budget/goals/editor/LimitAutomation.tsx b/packages/desktop-client/src/components/budget/goals/editor/LimitAutomation.tsx
index 113d58ef16..c460a5328a 100644
--- a/packages/desktop-client/src/components/budget/goals/editor/LimitAutomation.tsx
+++ b/packages/desktop-client/src/components/budget/goals/editor/LimitAutomation.tsx
@@ -5,8 +5,8 @@ import { SpaceBetween } from '@actual-app/components/space-between';
import { Text } from '@actual-app/components/text';
import { theme } from '@actual-app/components/theme';
import {
- currentDate,
dayFromDate,
+ firstDayOfMonth,
parseDate,
} from '@actual-app/core/shared/months';
import { amountToInteger, integerToAmount } from '@actual-app/core/shared/util';
@@ -27,11 +27,13 @@ import { useFormat } from '#hooks/useFormat';
type LimitAutomationProps = {
template: LimitTemplate;
dispatch: (action: Action) => void;
+ defaultWeeklyStart?: string;
};
export const LimitAutomation = ({
template,
dispatch,
+ defaultWeeklyStart,
}: LimitAutomationProps) => {
const { t } = useTranslation();
const format = useFormat();
@@ -42,8 +44,11 @@ export const LimitAutomation = ({
template.amount,
format.currency.decimalPlaces,
);
- const start = template.start;
- const dayOfWeek = start ? getDay(parseDate(start)) : 0;
+ const start =
+ template.start ??
+ defaultWeeklyStart ??
+ dayFromDate(firstDayOfMonth(new Date()));
+ const dayOfWeek = getDay(parseDate(start));
const hold = template.hold;
const selectButtonClassName = css({
@@ -63,7 +68,7 @@ export const LimitAutomation = ({
dispatch(
updateTemplate({
type: 'limit',
- start: dayFromDate(setDay(currentDate(), Number(value))),
+ start: dayFromDate(setDay(parseDate(start), Number(value))),
}),
)
}
@@ -100,7 +105,11 @@ export const LimitAutomation = ({
id="cadence-field"
value={period}
onChange={cadence =>
- dispatch(updateTemplate({ type: 'limit', period: cadence }))
+ dispatch(
+ cadence === 'weekly' && !template.start
+ ? updateTemplate({ type: 'limit', period: cadence, start })
+ : updateTemplate({ type: 'limit', period: cadence }),
+ )
}
options={[
['daily', t('Day')],
diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/AutomationEditorPane.tsx b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/AutomationEditorPane.tsx
index ae8f66c576..9301a913a0 100644
--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/AutomationEditorPane.tsx
+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/AutomationEditorPane.tsx
@@ -9,6 +9,7 @@ import { Text } from '@actual-app/components/text';
import { theme } from '@actual-app/components/theme';
import { tokens } from '@actual-app/components/tokens';
import { View } from '@actual-app/components/view';
+import { dayFromDate, firstDayOfMonth } from '@actual-app/core/shared/months';
import type {
CategoryGroupEntity,
ScheduleEntity,
@@ -69,6 +70,26 @@ const SINGLETON_TYPES: ReadonlySet<DisplayTemplateType> = new Set([
'remainder',
]);
+function getDefaultWeeklyStart(entries: AutomationEntry[]): string {
+ const starts: string[] = [];
+
+ for (const { template } of entries) {
+ if (template.type === 'periodic' && template.starting) {
+ starts.push(template.starting);
+ } else if (
+ (template.type === 'by' || template.type === 'spend') &&
+ template.month
+ ) {
+ starts.push(`${template.month}-01`);
+ }
+ }
+
+ const earliest =
+ starts.length > 0 ? starts.reduce((a, b) => (a < b ? a : b)) : null;
+
+ return earliest ?? dayFromDate(firstDayOfMonth(new Date()));
+}
+
type AutomationEditorPaneProps = {
entries: AutomationEntry[];
activeIdx: number;
@@ -97,6 +118,7 @@ export function AutomationEditorPane({
const activeError = automationErrors[activeIdx];
const state = active ? getInitialState(active.template) : null;
+ const defaultWeeklyStart = getDefaultWeeklyStart(entries);
const dispatch = (action: Parameters<typeof templateReducer>[1]) => {
setEntries(prev =>
@@ -284,6 +306,7 @@ export function AutomationEditorPane({
categories={categories}
hasLimitAutomation={hasLimitAutomation}
onAddLimitAutomation={onAddLimitAutomation}
+ defaultWeeklyStart={defaultWeeklyStart}
/>
</View>
</>
@@ -298,6 +321,7 @@ export function AutomationEditorPane({
categories={categories}
hasLimitAutomation={hasLimitAutomation}
onAddLimitAutomation={onAddLimitAutomation}
+ defaultWeeklyStart={defaultWeeklyStart}
/>
)}
diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsModal.test.ts b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsModal.test.ts
index 26ed93f8bf..dd587731d0 100644
--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsModal.test.ts
+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/BudgetAutomationsModal.test.ts
@@ -15,7 +15,7 @@ describe('migrateTemplatesToAutomations', () => {
expect(migrateTemplatesToAutomations([simpleTemplate])).toEqual([]);
});
- it('drops simple templates whose monthly amount is zero with no limit', () => {
+ it('migrates a standalone `#template 0` (no limit) into a $0 fixed entry', () => {
const simpleTemplate = {
type: 'simple',
directive: 'template',
@@ -23,7 +23,17 @@ describe('migrateTemplatesToAutomations', () => {
monthly: 0,
} satisfies Template;
- expect(migrateTemplatesToAutomations([simpleTemplate])).toEqual([]);
+ const result = migrateTemplatesToAutomations([simpleTemplate]);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].displayType).toBe('fixed');
+ expect(result[0].template).toMatchObject({
+ type: 'periodic',
+ amount: 0,
+ period: { period: 'month', amount: 1 },
+ directive: 'template',
+ priority: 5,
+ });
});
it('migrates a goal directive to a long-term goal entry', () => {
@@ -100,7 +110,7 @@ describe('migrateTemplatesToAutomations', () => {
});
});
- it('expands `#template 0 up to N` into limit + fixed-zero (not refill)', () => {
+ it('expands `#template 0 up to N` into a standalone limit (no refill, no $0 contribution)', () => {
const simpleTemplate = {
type: 'simple',
directive: 'template',
@@ -115,13 +125,12 @@ describe('migrateTemplatesToAutomations', () => {
const result = migrateTemplatesToAutomations([simpleTemplate]);
- expect(result).toHaveLength(2);
- expect(result.map(entry => entry.displayType)).toEqual(['limit', 'fixed']);
- expect(result[1].template).toMatchObject({
- type: 'periodic',
- amount: 0,
+ expect(result).toHaveLength(1);
+ expect(result.map(entry => entry.displayType)).toEqual(['limit']);
+ expect(result[0].template).toMatchObject({
+ type: 'limit',
+ amount: 1000,
directive: 'template',
- priority: 4,
});
});
@@ -153,6 +162,98 @@ describe('migrateTemplatesToAutomations', () => {
});
});
+ it('splits a periodic template with an `up to` limit into fixed + limit entries', () => {
+ // `#template 32.5 repeat every 1 days starting 2025-02-01 up to 2500`: the
+ // fixed editor can't show the inline cap, so the limit must become its own
+ // entry rather than vanishing and re-serialising as a duplicate `up to`.
+ const periodicTemplate = {
+ type: 'periodic',
+ directive: 'template',
+ priority: 2,
+ amount: 32.5,
+ period: { period: 'day', amount: 1 },
+ starting: '2025-02-01',
+ limit: {
+ amount: 2500,
+ hold: false,
+ period: 'monthly',
+ },
+ } satisfies Template;
+
+ const result = migrateTemplatesToAutomations([periodicTemplate]);
+
+ expect(result).toHaveLength(2);
+ expect(result.map(entry => entry.displayType)).toEqual(['fixed', 'limit']);
+ expect(result[0].template).toEqual({
+ type: 'periodic',
+ directive: 'template',
+ priority: 2,
+ amount: 32.5,
+ period: { period: 'day', amount: 1 },
+ starting: '2025-02-01',
+ });
+ expect(result[1].template).toEqual({
+ type: 'limit',
+ amount: 2500,
+ hold: false,
+ period: 'monthly',
+ start: undefined,
+ directive: 'template',
+ priority: null,
+ });
+ });
+
+ it('splits a remainder template with an `up to` limit into remainder + limit entries', () => {
+ const remainderTemplate = {
+ type: 'remainder',
+ directive: 'template',
+ priority: null,
+ weight: 2,
+ limit: {
+ amount: 500,
+ hold: true,
+ period: 'monthly',
+ },
+ } satisfies Template;
+
+ const result = migrateTemplatesToAutomations([remainderTemplate]);
+
+ expect(result).toHaveLength(2);
+ expect(result.map(entry => entry.displayType)).toEqual([
+ 'remainder',
+ 'limit',
+ ]);
+ expect(result[0].template).toEqual({
+ type: 'remainder',
+ directive: 'template',
+ priority: null,
+ weight: 2,
+ });
+ expect(result[1].template).toMatchObject({
+ type: 'limit',
+ amount: 500,
+ hold: true,
+ period: 'monthly',
+ });
+ });
+
+ it('leaves a periodic template without a limit as a single fixed entry', () => {
+ const periodicTemplate = {
+ type: 'periodic',
+ directive: 'template',
+ priority: 2,
+ amount: 10,
+ period: { period: 'month', amount: 1 },
+ starting: '2025-02-01',
+ } satisfies Template;
+
+ const result = migrateTemplatesToAutomations([periodicTemplate]);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].displayType).toBe('fixed');
+ expect(result[0].template).toEqual(periodicTemplate);
+ });
+
it('creates a single entry for non-simple templates', () => {
const scheduleTemplate = {
type: 'schedule',
diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/migrateTemplatesToAutomations.ts b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/migrateTemplatesToAutomations.ts
index 4757187abf..35e0065330 100644
--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal/migrateTemplatesToAutomations.ts
+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal/migrateTemplatesToAutomations.ts
@@ -47,8 +47,7 @@ export function migrateTemplatesToAutomations(
templates.forEach(template => {
if (template.type === 'simple') {
const monthly = template.monthly;
- const hasMonthly =
- monthly != null && (monthly !== 0 || template.limit != null);
+ const hasMonthly = monthly != null && monthly !== 0;
const { description } = template;
if (template.limit) {
@@ -70,7 +69,7 @@ export function migrateTemplatesToAutomations(
),
);
- if (!hasMonthly) {
+ if (monthly == null) {
entries.push(
createAutomationEntry(
{
@@ -84,12 +83,17 @@ export function migrateTemplatesToAutomations(
}
}
- if (hasMonthly) {
+ const contribution =
+ hasMonthly || (monthly === 0 && template.limit == null)
+ ? monthly
+ : null;
+
+ if (contribution != null) {
entries.push(
createAutomationEntry(
{
type: 'periodic',
- amount: monthly,
+ amount: contribution,
period: { period: 'month', amount: 1 },
starting: dayFromDate(firstDayOfMonth(new Date())),
directive: 'template',
@@ -107,6 +111,31 @@ export function migrateTemplatesToAutomations(
return;
}
+ if (
+ (template.type === 'periodic' || template.type === 'remainder') &&
+ template.limit
+ ) {
+ const { limit, ...base } = template;
+ entries.push(
+ createAutomationEntry(base, getDisplayTypeFromTemplate(base)),
+ );
+ entries.push(
+ createAutomationEntry(
+ {
+ type: 'limit',
+ amount: limit.amount,
+ hold: limit.hold,
+ period: limit.period,
+ start: limit.start,
+ directive: 'template',
+ priority: null,
+ },
+ 'limit',
+ ),
+ );
+ return;
+ }
+
entries.push(
createAutomationEntry(template, getDisplayTypeFromTemplate(template)),
);
diff --git a/upcoming-release-notes/budget-automation-feedback.md b/upcoming-release-notes/budget-automation-feedback.md
new file mode 100644
index 0000000000..e4e80392bc
--- /dev/null
+++ b/upcoming-release-notes/budget-automation-feedback.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [matt-fidd]
+---
+
+Fix various QOL and parsing bugs in the Automation UI