Skip to content

A local-first personal finance app

Display/save limit/refill templates (#6693)

Julian Dominguez-Schatz3201819df943
diff --git a/packages/desktop-client/src/components/alerts.tsx b/packages/desktop-client/src/components/alerts.tsx
index bdd5673b4b..726f4802ba 100644
--- a/packages/desktop-client/src/components/alerts.tsx
+++ b/packages/desktop-client/src/components/alerts.tsx
@@ -15,6 +15,7 @@ type AlertProps = {
color?: string;
backgroundColor?: string;
style?: CSSProperties;
+ iconStyle?: CSSProperties;
children?: ReactNode;
};
@@ -23,6 +24,7 @@ const Alert = ({
color,
backgroundColor,
style,
+ iconStyle,
children,
}: AlertProps) => {
return (
@@ -48,21 +50,29 @@ const Alert = ({
alignSelf: 'stretch',
flexShrink: 0,
marginRight: 5,
+ ...iconStyle,
}}
>
<Icon width={13} style={{ marginTop: 2 }} />
</View>
- <Text style={{ zIndex: 1, lineHeight: 1.5 }}>{children}</Text>
+ <Text style={{ width: '100%', zIndex: 1, lineHeight: 1.5 }}>
+ {children}
+ </Text>
</View>
);
};
type ScopedAlertProps = {
style?: CSSProperties;
+ iconStyle?: CSSProperties;
children?: ReactNode;
};
-export const Information = ({ style, children }: ScopedAlertProps) => {
+export const Information = ({
+ style,
+ iconStyle,
+ children,
+}: ScopedAlertProps) => {
return (
<Alert
icon={SvgInformationOutline}
@@ -73,32 +83,35 @@ export const Information = ({ style, children }: ScopedAlertProps) => {
padding: 5,
...style,
}}
+ iconStyle={iconStyle}
>
{children}
</Alert>
);
};
-export const Warning = ({ style, children }: ScopedAlertProps) => {
+export const Warning = ({ style, iconStyle, children }: ScopedAlertProps) => {
return (
<Alert
icon={SvgExclamationOutline}
color={theme.warningText}
backgroundColor={theme.warningBackground}
style={style}
+ iconStyle={iconStyle}
>
{children}
</Alert>
);
};
-export const Error = ({ style, children }: ScopedAlertProps) => {
+export const Error = ({ style, iconStyle, children }: ScopedAlertProps) => {
return (
<Alert
icon={SvgExclamationOutline}
color={theme.errorTextDarker}
backgroundColor={theme.errorBackground}
style={style}
+ iconStyle={iconStyle}
>
{children}
</Alert>
diff --git a/packages/desktop-client/src/components/budget/goals/editor/RefillAutomation.tsx b/packages/desktop-client/src/components/budget/goals/editor/RefillAutomation.tsx
index 0b1df5d7dd..483493efd8 100644
--- a/packages/desktop-client/src/components/budget/goals/editor/RefillAutomation.tsx
+++ b/packages/desktop-client/src/components/budget/goals/editor/RefillAutomation.tsx
@@ -2,7 +2,6 @@ import { Trans, useTranslation } from 'react-i18next';
import { Button } from '@actual-app/components/button';
import { SpaceBetween } from '@actual-app/components/space-between';
-import { Text } from '@actual-app/components/text';
import { View } from '@actual-app/components/view';
import { Warning } from '@desktop-client/components/alerts';
@@ -19,13 +18,17 @@ export function RefillAutomation({
const { t } = useTranslation();
return (
- <SpaceBetween direction="vertical" gap={10} style={{ marginTop: 10 }}>
- <Text>
- <Trans>Uses the balance limit automation for this category.</Trans>
- </Text>
+ <SpaceBetween align="center" gap={10} style={{ marginTop: 10 }}>
{!hasLimitAutomation && (
- <Warning>
- <SpaceBetween gap={10} align="center" style={{ flexWrap: 'wrap' }}>
+ <Warning
+ style={{ width: '100%', alignItems: 'center' }}
+ iconStyle={{ alignSelf: 'unset', paddingTop: 0, marginTop: -2 }}
+ >
+ <SpaceBetween
+ gap={10}
+ align="center"
+ style={{ width: '100%', justifyContent: 'space-between' }}
+ >
<View>
<Trans>
Add a balance limit automation to set the refill target.
diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal.test.ts b/packages/desktop-client/src/components/modals/BudgetAutomationsModal.test.ts
new file mode 100644
index 0000000000..cb0e2b1f86
--- /dev/null
+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal.test.ts
@@ -0,0 +1,125 @@
+import type { Template } from 'loot-core/types/models/templates';
+
+import { migrateTemplatesToAutomations } from './BudgetAutomationsModal';
+
+describe('migrateTemplatesToAutomations', () => {
+ it('preserves simple templates that have no limit and no monthly amount', () => {
+ const simpleTemplate = {
+ type: 'simple',
+ directive: 'template',
+ priority: 5,
+ } satisfies Template;
+
+ const result = migrateTemplatesToAutomations([simpleTemplate]);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].displayType).toBe('week');
+ expect(result[0].template).toEqual(simpleTemplate);
+ expect(result[0].id).toMatch(/^automation-/);
+ });
+
+ it('expands a simple template with limit into limit and refill entries', () => {
+ const simpleTemplate = {
+ type: 'simple',
+ directive: 'template',
+ priority: 7,
+ limit: {
+ amount: 120,
+ hold: true,
+ period: 'monthly',
+ start: '2026-02-01',
+ },
+ } satisfies Template;
+
+ const result = migrateTemplatesToAutomations([simpleTemplate]);
+
+ expect(result).toHaveLength(2);
+ expect(result[0].displayType).toBe('limit');
+ expect(result[0].template).toEqual({
+ type: 'limit',
+ amount: 120,
+ hold: true,
+ period: 'monthly',
+ start: '2026-02-01',
+ directive: 'template',
+ priority: null,
+ });
+ expect(result[1].displayType).toBe('refill');
+ expect(result[1].template).toEqual({
+ type: 'refill',
+ directive: 'template',
+ priority: 7,
+ });
+ });
+
+ it('expands a simple template with monthly amount into one periodic entry', () => {
+ const simpleTemplate = {
+ type: 'simple',
+ directive: 'template',
+ priority: 3,
+ monthly: 45,
+ } satisfies Template;
+
+ const result = migrateTemplatesToAutomations([simpleTemplate]);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].displayType).toBe('week');
+ expect(result[0].template).toMatchObject({
+ type: 'periodic',
+ amount: 45,
+ period: {
+ period: 'month',
+ amount: 1,
+ },
+ directive: 'template',
+ priority: 3,
+ });
+ expect(result[0].template).toMatchObject({
+ starting: expect.any(String),
+ });
+ });
+
+ it('expands a simple template with both limit and monthly into three entries in order', () => {
+ const simpleTemplate = {
+ type: 'simple',
+ directive: 'template',
+ priority: 11,
+ monthly: 20,
+ limit: {
+ amount: 200,
+ hold: false,
+ period: 'weekly',
+ },
+ } satisfies Template;
+
+ const result = migrateTemplatesToAutomations([simpleTemplate]);
+
+ expect(result).toHaveLength(3);
+ expect(result.map(entry => entry.displayType)).toEqual([
+ 'limit',
+ 'refill',
+ 'week',
+ ]);
+ expect(result[2].template).toMatchObject({
+ type: 'periodic',
+ amount: 20,
+ directive: 'template',
+ priority: 11,
+ });
+ });
+
+ it('creates a single entry for non-simple templates', () => {
+ const scheduleTemplate = {
+ type: 'schedule',
+ directive: 'template',
+ priority: 1,
+ name: 'rent',
+ } satisfies Template;
+
+ const result = migrateTemplatesToAutomations([scheduleTemplate]);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].displayType).toBe('schedule');
+ expect(result[0].template).toEqual(scheduleTemplate);
+ });
+});
diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx b/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx
index 58c727ce93..a52431b9af 100644
--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx
+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx
@@ -10,6 +10,7 @@ import { View } from '@actual-app/components/view';
import uniqueId from 'lodash/uniqueId';
import { send } from 'loot-core/platform/client/connection';
+import { dayFromDate, firstDayOfMonth } from 'loot-core/shared/months';
import { q } from 'loot-core/shared/query';
import type {
CategoryGroupEntity,
@@ -19,6 +20,7 @@ import type { Template } from 'loot-core/types/models/templates';
import { Warning } from '@desktop-client/components/alerts';
import { BudgetAutomation } from '@desktop-client/components/budget/goals/BudgetAutomation';
+import type { DisplayTemplateType } from '@desktop-client/components/budget/goals/constants';
import { DEFAULT_PRIORITY } from '@desktop-client/components/budget/goals/reducer';
import { useBudgetAutomationCategories } from '@desktop-client/components/budget/goals/useBudgetAutomationCategories';
import { Link } from '@desktop-client/components/common/Link';
@@ -34,6 +36,120 @@ import { useSchedules } from '@desktop-client/hooks/useSchedules';
import { pushModal } from '@desktop-client/modals/modalsSlice';
import { useDispatch } from '@desktop-client/redux';
+type AutomationEntry = {
+ id: string;
+ template: Template;
+ displayType: DisplayTemplateType;
+};
+
+function getDisplayTypeFromTemplate(template: Template): DisplayTemplateType {
+ switch (template.type) {
+ case 'percentage':
+ return 'percentage';
+ case 'schedule':
+ return 'schedule';
+ case 'periodic':
+ case 'simple':
+ return 'week';
+ case 'limit':
+ return 'limit';
+ case 'refill':
+ return 'refill';
+ case 'average':
+ case 'copy':
+ return 'historical';
+ default:
+ return 'week';
+ }
+}
+
+function createAutomationEntry(
+ template: Template,
+ displayType: DisplayTemplateType,
+): AutomationEntry {
+ return {
+ id: uniqueId('automation-'),
+ template,
+ displayType,
+ };
+}
+
+export function migrateTemplatesToAutomations(
+ templates: Template[],
+): AutomationEntry[] {
+ const entries: AutomationEntry[] = [];
+
+ templates.forEach(template => {
+ // Expand simple templates into limit, refill, and/or periodic templates
+ if (template.type === 'simple') {
+ let hasExpandedTemplate = false;
+
+ if (template.limit) {
+ hasExpandedTemplate = true;
+ entries.push(
+ createAutomationEntry(
+ {
+ type: 'limit',
+ amount: template.limit.amount,
+ hold: template.limit.hold,
+ period: template.limit.period,
+ start: template.limit.start,
+ directive: 'template',
+ priority: null,
+ },
+ 'limit',
+ ),
+ );
+ entries.push(
+ createAutomationEntry(
+ {
+ type: 'refill',
+ directive: 'template',
+ priority: template.priority,
+ },
+ 'refill',
+ ),
+ );
+ }
+ // If it has a monthly amount, create a periodic template
+ if (template.monthly != null && template.monthly !== 0) {
+ hasExpandedTemplate = true;
+ entries.push(
+ createAutomationEntry(
+ {
+ type: 'periodic',
+ amount: template.monthly,
+ period: {
+ period: 'month',
+ amount: 1,
+ },
+ starting: dayFromDate(firstDayOfMonth(new Date())),
+ directive: 'template',
+ priority: template.priority,
+ },
+ 'week',
+ ),
+ );
+ }
+
+ if (!hasExpandedTemplate) {
+ entries.push(
+ createAutomationEntry(template, getDisplayTypeFromTemplate(template)),
+ );
+ }
+
+ return;
+ }
+
+ // For all other template types, create a single entry
+ entries.push(
+ createAutomationEntry(template, getDisplayTypeFromTemplate(template)),
+ );
+ });
+
+ return entries;
+}
+
function BudgetAutomationList({
automations,
setAutomations,
@@ -41,49 +157,69 @@ function BudgetAutomationList({
categories,
style,
}: {
- automations: Template[];
- setAutomations: (fn: (prev: Template[]) => Template[]) => void;
+ automations: AutomationEntry[];
+ setAutomations: (fn: (prev: AutomationEntry[]) => AutomationEntry[]) => void;
schedules: readonly ScheduleEntity[];
categories: CategoryGroupEntity[];
style?: CSSProperties;
}) {
- const [automationIds, setAutomationIds] = useState(() => {
- // automations don't have ids, so we need to generate them
- return automations.map(() => uniqueId('automation-'));
- });
-
const onAdd = () => {
- const newId = uniqueId('automation-');
- setAutomationIds(prevIds => [...prevIds, newId]);
setAutomations(prev => [
...prev,
- {
- type: 'simple',
- monthly: 5,
- directive: 'template',
- priority: DEFAULT_PRIORITY,
- },
+ createAutomationEntry(
+ {
+ type: 'periodic',
+ amount: 500,
+ period: {
+ period: 'month',
+ amount: 1,
+ },
+ starting: dayFromDate(firstDayOfMonth(new Date())),
+ directive: 'template',
+ priority: DEFAULT_PRIORITY,
+ },
+ 'week',
+ ),
+ ]);
+ };
+ const onAddLimit = () => {
+ setAutomations(prev => [
+ ...prev,
+ createAutomationEntry(
+ {
+ directive: 'template',
+ type: 'limit',
+ amount: 500,
+ period: 'monthly',
+ hold: false,
+ priority: null,
+ },
+ 'limit',
+ ),
]);
};
const onDelete = (index: number) => () => {
setAutomations(prev => [...prev.slice(0, index), ...prev.slice(index + 1)]);
- setAutomationIds(prev => [
- ...prev.slice(0, index),
- ...prev.slice(index + 1),
- ]);
};
const onSave = useCallback(
- (index: number) => (template: Template) => {
- setAutomations(prev =>
- prev.map((oldAutomation, mapIndex) =>
- mapIndex === index ? template : oldAutomation,
- ),
- );
- },
+ (index: number) =>
+ (template: Template, displayType: DisplayTemplateType) => {
+ setAutomations(prev =>
+ prev.map((oldAutomation, mapIndex) =>
+ mapIndex === index
+ ? { ...oldAutomation, template, displayType }
+ : oldAutomation,
+ ),
+ );
+ },
[setAutomations],
);
+ const hasLimitAutomation = automations.some(
+ automation => automation.displayType === 'limit',
+ );
+
return (
<SpaceBetween
direction="vertical"
@@ -97,12 +233,16 @@ function BudgetAutomationList({
>
{automations.map((automation, index) => (
<BudgetAutomation
- key={automationIds[index]}
+ key={automation.id}
onSave={onSave(index)}
onDelete={onDelete(index)}
- template={automation}
+ template={automation.template}
categories={categories}
schedules={schedules}
+ hasLimitAutomation={hasLimitAutomation}
+ onAddLimitAutomation={
+ automation.displayType === 'refill' ? onAddLimit : undefined
+ }
readOnlyStyle={{
color: theme.pillText,
backgroundColor: theme.pillBackground,
@@ -181,12 +321,20 @@ export function BudgetAutomationsModal({ categoryId }: { categoryId: string }) {
const { t } = useTranslation();
const dispatch = useDispatch();
- const [automations, setAutomations] = useState<Record<string, Template[]>>(
- {},
- );
+ const [automations, setAutomations] = useState<
+ Record<string, AutomationEntry[]>
+ >({});
+ const onLoaded = useCallback((result: Record<string, Template[]>) => {
+ const next: Record<string, AutomationEntry[]> = {};
+ for (const [id, templates] of Object.entries(result)) {
+ next[id] = migrateTemplatesToAutomations(templates);
+ }
+ setAutomations(next);
+ }, []);
+
const { loading } = useBudgetAutomations({
categoryId,
- onLoaded: setAutomations,
+ onLoaded,
});
const schedulesQuery = useMemo(() => q('schedules').select('*'), []);
@@ -205,11 +353,12 @@ export function BudgetAutomationsModal({ categoryId }: { categoryId: string }) {
return;
}
+ const templates = automations[categoryId].map(({ template }) => template);
await send('budget/set-category-automations', {
categoriesWithTemplates: [
{
id: categoryId,
- templates: automations[categoryId],
+ templates,
},
],
source: 'ui',
@@ -252,13 +401,18 @@ export function BudgetAutomationsModal({ categoryId }: { categoryId: string }) {
<AnimatedLoading style={{ width: 20, height: 20 }} />
</View>
) : (
- <SpaceBetween align="stretch" direction="vertical">
+ <SpaceBetween align="stretch" direction="vertical" wrap={false}>
{needsMigration && (
- <BudgetAutomationMigrationWarning categoryId={categoryId} />
+ <BudgetAutomationMigrationWarning
+ categoryId={categoryId}
+ style={{ flexShrink: 0 }}
+ />
)}
<BudgetAutomationList
automations={automations[categoryId] || []}
- setAutomations={(cb: (prev: Template[]) => Template[]) => {
+ setAutomations={(
+ cb: (prev: AutomationEntry[]) => AutomationEntry[],
+ ) => {
setAutomations(prev => ({
...prev,
[categoryId]: cb(prev[categoryId] || []),
@@ -286,7 +440,10 @@ export function BudgetAutomationsModal({ categoryId }: { categoryId: string }) {
pushModal({
modal: {
name: 'category-automations-unmigrate',
- options: { categoryId, templates },
+ options: {
+ categoryId,
+ templates: templates.map(({ template }) => template),
+ },
},
}),
);
diff --git a/upcoming-release-notes/6693.md b/upcoming-release-notes/6693.md
new file mode 100644
index 0000000000..1f7a61b53c
--- /dev/null
+++ b/upcoming-release-notes/6693.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [jfdoming]
+---
+
+Display/save refill templates