A local-first personal finance app
Add tools to migrate/un-migrate to/from UI automations (#5624)
Julian Dominguez-Schatz338093836ba5
diff --git a/packages/desktop-client/src/components/Modals.tsx b/packages/desktop-client/src/components/Modals.tsxindex db6716d910..bf160590ec 100644--- a/packages/desktop-client/src/components/Modals.tsx+++ b/packages/desktop-client/src/components/Modals.tsx@@ -68,6 +68,7 @@ import { TrackingBudgetMonthMenuModal } from './modals/TrackingBudgetMonthMenuMoimport { TrackingBudgetSummaryModal } from './modals/TrackingBudgetSummaryModal';import { TransferModal } from './modals/TransferModal';import { TransferOwnership } from './modals/TransferOwnership';+import { UnmigrateBudgetAutomationsModal } from './modals/UnmigrateBudgetAutomationsModal';import { CategoryLearning } from './payees/CategoryLearning';import { DiscoverSchedules } from './schedules/DiscoverSchedules';import { PostsOfflineNotification } from './schedules/PostsOfflineNotification';@@ -106,6 +107,11 @@ export function Modals() {<BudgetAutomationsModal key={name} {...modal.options} />) : null;+ case 'category-automations-unmigrate':+ return budgetId ? (+ <UnmigrateBudgetAutomationsModal key={name} {...modal.options} />+ ) : null;+case 'keyboard-shortcuts':// don't show the hotkey help modal when a budget is not openreturn budgetId ? <KeyboardShortcutModal key={key} /> : null;diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx b/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsxindex 13171c637b..9b02f93ff7 100644--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx@@ -1,4 +1,4 @@-import { useMemo, useState } from 'react';+import { type CSSProperties, useMemo, useState } from 'react';import { Trans, useTranslation } from 'react-i18next';import { Button } from '@actual-app/components/button';@@ -16,27 +16,35 @@ import {} from 'loot-core/types/models';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 { 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';import {Modal,ModalCloseButton,ModalHeader,} from '@desktop-client/components/common/Modal';import { useBudgetAutomations } from '@desktop-client/hooks/useBudgetAutomations';+import { useCategory } from '@desktop-client/hooks/useCategory';+import { useNotes } from '@desktop-client/hooks/useNotes';import { useSchedules } from '@desktop-client/hooks/useSchedules';+import { pushModal } from '@desktop-client/modals/modalsSlice';+import { useDispatch } from '@desktop-client/redux';function BudgetAutomationList({automations,setAutomations,schedules,categories,+ style,}: {automations: Template[];setAutomations: (fn: (prev: Template[]) => Template[]) => void;schedules: readonly ScheduleEntity[];categories: CategoryGroupEntity[];+ style?: CSSProperties;}) {const [automationIds, setAutomationIds] = useState(() => {// automations don't have ids, so we need to generate them@@ -69,6 +77,7 @@ function BudgetAutomationList({spacing={4}style={{overflowY: 'scroll',+ ...style,}}>{automations.map((automation, index) => (@@ -95,8 +104,66 @@ function BudgetAutomationList({);}+function BudgetAutomationMigrationWarning({+ categoryId,+ style,+}: {+ categoryId: string;+ style?: CSSProperties;+}) {+ const notes = useNotes(categoryId);++ const templates = useMemo(() => {+ if (!notes) return null;+ const lines = notes.split('\n');+ return lines+ .flatMap(line => {+ if (line.trim().startsWith('#template')) return line;+ if (line.trim().startsWith('#goal')) return line;+ if (line.trim().startsWith('#cleanup')) return line;+ return [];+ })+ .join('\n');+ }, [notes]);++ if (!templates) return null;++ return (+ <Warning style={style}>+ <Stack style={{ minHeight: 'unset' }}>+ <View>+ <Trans>+ This category uses notes-based automations (formerly “budget+ templates”). We have automatically imported your existing+ automations below. Please review them for accuracy and hit save to+ complete the migration.+ </Trans>+ </View>+ <View>+ <Trans>+ Original templates:+ <View+ style={{+ whiteSpace: 'pre-wrap',+ fontFamily: 'monospace',+ marginTop: 4,+ padding: 12,+ borderRadius: 4,+ backgroundColor: 'rgba(255, 255, 255, 0.1)',+ }}+ >+ {templates}+ </View>+ </Trans>+ </View>+ </Stack>+ </Warning>+ );+}+export function BudgetAutomationsModal({ categoryId }: { categoryId: string }) {const { t } = useTranslation();+ const dispatch = useDispatch();const [automations, setAutomations] = useState<Record<string, Template[]>>({},@@ -112,6 +179,9 @@ export function BudgetAutomationsModal({ categoryId }: { categoryId: string }) {});const categories = useBudgetAutomationCategories();+ const currentCategory = useCategory(categoryId);++ const needsMigration = currentCategory?.template_settings?.source !== 'ui';const onSave = async (close: () => void) => {if (!automations[categoryId]) {@@ -141,7 +211,9 @@ export function BudgetAutomationsModal({ categoryId }: { categoryId: string }) {{({ state: { close } }) => (<Stack direction="column" style={{ height: '100%' }}><ModalHeader- title={t('Budget automations')}+ title={t('Budget automations: {{category}}', {+ category: currentCategory?.name,+ })}rightContent={<ModalCloseButton onPress={close} />}/>{loading ? (@@ -159,20 +231,49 @@ export function BudgetAutomationsModal({ categoryId }: { categoryId: string }) {<AnimatedLoading style={{ width: 20, height: 20 }} /></View>) : (- <BudgetAutomationList- automations={automations[categoryId] || []}- setAutomations={(cb: (prev: Template[]) => Template[]) => {- setAutomations(prev => ({- ...prev,- [categoryId]: cb(prev[categoryId] || []),- }));- }}- schedules={schedules}- categories={categories}- />+ <Stack>+ {needsMigration && (+ <BudgetAutomationMigrationWarning categoryId={categoryId} />+ )}+ <BudgetAutomationList+ automations={automations[categoryId] || []}+ setAutomations={(cb: (prev: Template[]) => Template[]) => {+ setAutomations(prev => ({+ ...prev,+ [categoryId]: cb(prev[categoryId] || []),+ }));+ }}+ schedules={schedules}+ categories={categories}+ />+ </Stack>)}<View style={{ flexGrow: 1 }} />- <Stack direction="row" justify="flex-end" style={{ marginTop: 20 }}>+ <Stack+ direction="row"+ justify="flex-end"+ align="center"+ style={{ marginTop: 20 }}+ >+ {!needsMigration && (+ <Link+ variant="text"+ onClick={() => {+ const templates = automations[categoryId] || [];+ dispatch(+ pushModal({+ modal: {+ name: 'category-automations-unmigrate',+ options: { categoryId, templates },+ },+ }),+ );+ }}+ >+ <Trans>Un-migrate</Trans>+ </Link>+ )}+ {/* <View style={{ flex: 1 }} /> */}<Button onPress={close}><Trans>Cancel</Trans></Button>diff --git a/packages/desktop-client/src/components/modals/UnmigrateBudgetAutomationsModal.tsx b/packages/desktop-client/src/components/modals/UnmigrateBudgetAutomationsModal.tsxnew file mode 100644index 0000000000..5cea1b3cbb--- /dev/null+++ b/packages/desktop-client/src/components/modals/UnmigrateBudgetAutomationsModal.tsx@@ -0,0 +1,187 @@+import React, { useEffect, useState } from 'react';+import { Trans, useTranslation } from 'react-i18next';++import { Button } from '@actual-app/components/button';+import { AnimatedLoading } from '@actual-app/components/icons/AnimatedLoading';+import { Stack } from '@actual-app/components/stack';+import { View } from '@actual-app/components/view';++import { send } from 'loot-core/platform/client/fetch';+import type { Template } from 'loot-core/types/models/templates';++import { Link } from '@desktop-client/components/common/Link';+import {+ Modal,+ ModalCloseButton,+ ModalHeader,+} from '@desktop-client/components/common/Modal';+import { Notes } from '@desktop-client/components/Notes';+import { useCategory } from '@desktop-client/hooks/useCategory';+import { useNotes } from '@desktop-client/hooks/useNotes';++export function UnmigrateBudgetAutomationsModal({+ categoryId,+ templates,+}: {+ categoryId: string;+ templates: Template[];+}) {+ const { t } = useTranslation();+ const category = useCategory(categoryId);+ const existingNotes = useNotes(categoryId) || '';+ const [editedNotes, setEditedNotes] = useState<string>('');++ const [rendered, setRendered] = useState<string | null>(null);+ const [saving, setSaving] = useState(false);++ useEffect(() => {+ let mounted = true;+ (async () => {+ try {+ const text: string = await send(+ 'budget/render-note-templates',+ templates,+ );+ if (mounted) setRendered(text);+ } catch (e) {+ if (mounted) setRendered('');+ }+ })();+ return () => {+ mounted = false;+ };+ }, [templates]);++ // Seed editable notes once templates rendered+ useEffect(() => {+ if (rendered !== null) {+ const base = existingNotes.trimEnd();+ if (!rendered) {+ setEditedNotes(base);+ return;+ }+ const existingLineSet = new Set(+ base+ .split('\n')+ .map(l => l.trim())+ .filter(l => l.length > 0),+ );+ const renderedLines = rendered+ .split('\n')+ .map(l => l.trimEnd())+ .filter(l => l.length > 0);+ const newLines: string[] = [];+ for (const line of renderedLines) {+ if (!existingLineSet.has(line.trim())) {+ newLines.push(line);+ }+ }+ if (newLines.length === 0) {+ setEditedNotes(base);+ } else {+ const needsNewline = base && !base.endsWith('\n') ? '\n' : '';+ setEditedNotes(+ base ++ needsNewline ++ '\nExport from automations UI:\n' ++ newLines.join('\n'),+ );+ }+ }+ }, [rendered, existingNotes]);++ async function onSave(close: () => void) {+ setSaving(true);+ await send('notes-save-undoable', { id: categoryId, note: editedNotes });+ await send('budget/set-category-automations', {+ categoriesWithTemplates: [{ id: categoryId, templates }],+ source: 'notes',+ });+ setSaving(false);+ close();+ }++ return (+ <Modal+ name="category-automations-unmigrate"+ containerProps={{+ style: { width: 850, height: 650, paddingBottom: 20 },+ }}+ >+ {({ state: { close } }) => (+ <Stack direction="column" style={{ height: '100%' }} spacing={3}>+ <ModalHeader+ title={t('Un-migrate automations: {{category}}', {+ category: category?.name,+ })}+ rightContent={<ModalCloseButton onPress={close} />}+ />+ {rendered === null ? (+ <View+ style={{+ flex: 1,+ alignItems: 'center',+ justifyContent: 'center',+ }}+ >+ <AnimatedLoading style={{ width: 20, height: 20 }} />+ </View>+ ) : (+ <Stack spacing={3} style={{ overflowY: 'auto', flex: 1 }}>+ <View style={{ display: 'inline-block', minHeight: 'unset' }}>+ <Trans>+ If the automation UI isn't working for you, you can+ temporarily switch back to notes-based automations. Please let+ us know your feedback about what's not working on the{' '}+ <Link+ variant="external"+ to="https://github.com/actualbudget/actual/issues/"+ >+ feedback issue+ </Link>+ .+ </Trans>+ </View>+ <View>+ <Trans>+ We have merged your existing automations with the notes for+ this category. Please review and edit as needed.+ </Trans>+ </View>+ <Notes+ notes={editedNotes}+ editable+ focused+ getStyle={() => ({+ flex: 1,+ borderRadius: 6,+ display: 'flex',+ flexDirection: 'column',+ resize: 'none',+ })}+ onChange={setEditedNotes}+ />+ </Stack>+ )}+ <Stack direction="row" justify="flex-end" spacing={2}>+ <Button onPress={() => close()}>+ <Trans>Cancel</Trans>+ </Button>+ <Button+ variant="primary"+ onPress={() => onSave(close)}+ isDisabled={saving}+ >+ {saving && (+ <AnimatedLoading+ style={{ width: 16, height: 16, marginRight: 6 }}+ />+ )}+ <Trans>Save notes & un-migrate</Trans>+ </Button>+ </Stack>+ </Stack>+ )}+ </Modal>+ );+}diff --git a/packages/desktop-client/src/hooks/useBudgetAutomations.ts b/packages/desktop-client/src/hooks/useBudgetAutomations.tsindex 532e67ada5..406ca1c918 100644--- a/packages/desktop-client/src/hooks/useBudgetAutomations.ts+++ b/packages/desktop-client/src/hooks/useBudgetAutomations.ts@@ -19,6 +19,10 @@ export function useBudgetAutomations({let mounted = true;async function fetchAutomations() {setLoading(true);++ // Always import notes first; the query will automatically ignore UI-based categories.+ await send('budget/store-note-templates');+const result = await send('budget/get-category-automations', categoryId);if (mounted) {setAutomations(result);diff --git a/packages/desktop-client/src/modals/modalsSlice.ts b/packages/desktop-client/src/modals/modalsSlice.tsindex 4d6dfc82f4..db83e8ea05 100644--- a/packages/desktop-client/src/modals/modalsSlice.ts+++ b/packages/desktop-client/src/modals/modalsSlice.ts@@ -16,6 +16,7 @@ import {type NewUserEntity,type NoteEntity,} from 'loot-core/types/models';+import { type Template } from 'loot-core/types/models/templates';import { resetApp, setAppState } from '@desktop-client/app/appSlice';import { type SelectLinkedAccountsModalProps } from '@desktop-client/components/modals/SelectLinkedAccountsModal';@@ -545,6 +546,13 @@ export type Modal =options: {categoryId: CategoryEntity['id'];};+ }+ | {+ name: 'category-automations-unmigrate';+ options: {+ categoryId: CategoryEntity['id'];+ templates: Template[];+ };};type OpenAccountCloseModalPayload = {diff --git a/packages/loot-core/src/server/budget/app.ts b/packages/loot-core/src/server/budget/app.tsindex 75b13c3522..ce2e220bcd 100644--- a/packages/loot-core/src/server/budget/app.ts+++ b/packages/loot-core/src/server/budget/app.ts@@ -16,6 +16,7 @@ import * as actions from './actions';import * as budget from './base';import * as cleanupActions from './cleanup-template';import * as goalActions from './goal-template';+import * as goalNoteActions from './template-notes';export interface BudgetHandlers {'budget/budget-amount': typeof actions.setBudget;@@ -56,6 +57,8 @@ export interface BudgetHandlers {'must-category-transfer': typeof isCategoryTransferRequired;'budget/get-category-automations': typeof goalActions.getTemplatesForCategory;'budget/set-category-automations': typeof goalActions.storeTemplates;+ 'budget/store-note-templates': typeof goalNoteActions.storeNoteTemplates;+ 'budget/render-note-templates': typeof goalNoteActions.unparse;}export const app = createApp<BudgetHandlers>();@@ -150,6 +153,11 @@ app.method('budget/set-category-automations',mutator(undoable(goalActions.storeTemplates)),);+app.method(+ 'budget/store-note-templates',+ mutator(goalNoteActions.storeNoteTemplates),+);+app.method('budget/render-note-templates', goalNoteActions.unparse);// Server must return AQL entities not the raw DB dataasync function getCategories() {diff --git a/packages/loot-core/src/server/budget/template-notes.test.ts b/packages/loot-core/src/server/budget/template-notes.test.tsindex e568dfda3b..9a1022df0e 100644--- a/packages/loot-core/src/server/budget/template-notes.test.ts+++ b/packages/loot-core/src/server/budget/template-notes.test.ts@@ -1,12 +1,18 @@+import type { Template } from '../../types/models/templates';import * as db from '../db';+import { parse } from './goal-template.pegjs';import {CategoryWithTemplateNote,getActiveSchedules,getCategoriesWithTemplateNotes,resetCategoryGoalDefsWithNoTemplates,} from './statements';-import { checkTemplateNotes, storeNoteTemplates } from './template-notes';+import {+ checkTemplateNotes,+ storeNoteTemplates,+ unparse,+} from './template-notes';vi.mock('../db');vi.mock('./statements');@@ -297,3 +303,54 @@ function mockSchedules(): db.DbSchedule[] {},];}++describe('unparse/parse round-trip', () => {+ const cases: string[] = [+ // simple+ '#template 10',+ '#template up to 50',+ '#template up to 25 per day hold',+ '#template up to 100 per week starting 2025-01-01',+ '#template-2 123.45',+ // schedule+ '#template schedule Rent',+ '#template schedule full Mortgage',+ '#template schedule Netflix [increase 10%]',+ '#template schedule full Groceries [decrease 5%]',+ // percentage+ '#template 50% of Utilities',+ '#template 75% of previous Dining Out',+ // periodic+ '#template 200 repeat every 2 months starting 2025-06-01',+ '#template 300 repeat every week starting 2025-01-07',+ '#template 400 repeat every year starting 2025-01-01 up to 50',+ // by / spend+ '#template 500 by 2025-12',+ '#template 600 by 2025-11 repeat every month',+ '#template 700 by 2025-10 repeat every 2 months',+ '#template 800 by 2025-09 repeat every year',+ '#template 900 by 2025-08 repeat every 3 years',+ '#template 1000 by 2025-07 spend from 2025-01 repeat every month',+ '#template 1100 by 2025-06 spend from 2025-02 repeat every 2 months',+ // remainder+ '#template remainder',+ '#template remainder 2',+ '#template remainder 3 up to 10',+ // average+ '#template average 6 months',+ '#template-5 average 12 months',+ // copy+ '#template copy from 3 months ago',+ '#template copy from 6 months ago',+ // goal+ '#goal 1234',+ ];++ it.each(cases)('round-trips: %s', async original => {+ const parsed: Template = parse(original);+ const serialized = await unparse([parsed]);+ const reparsed: Template = parse(serialized);++ expect(parsed).toEqual(reparsed);+ });+});diff --git a/packages/loot-core/src/server/budget/template-notes.ts b/packages/loot-core/src/server/budget/template-notes.tsindex 6487ab3e53..b7e0519572 100644--- a/packages/loot-core/src/server/budget/template-notes.ts+++ b/packages/loot-core/src/server/budget/template-notes.ts@@ -136,3 +136,155 @@ async function getCategoriesWithTemplates(): Promise<return templatesForCategory;}++export async function unparse(templates: Template[]): Promise<string> {+ return templates+ .flatMap(template => {+ if (template.type === 'error') {+ return [];+ }++ if (template.type === 'goal') {+ return `${GOAL_PREFIX} ${template.amount}`;+ }++ const prefix = template.priority+ ? `${TEMPLATE_PREFIX}-${template.priority}`+ : TEMPLATE_PREFIX;++ switch (template.type) {+ case 'simple': {+ // Simple template syntax: #template[-prio] simple [monthly N] [limit]+ let result = prefix;+ if (template.monthly != null) {+ result += ` ${template.monthly}`;+ }+ if (template.limit) {+ result += ` ${limitToString(template.limit)}`;+ }+ return result.trim();+ }+ case 'schedule': {+ // schedule syntax: #template[-prio] schedule <name> [full] [ [increase/decrease N%] ]+ let result = `${prefix} schedule`;+ if (template.full) {+ result += ' full';+ }+ result += ` ${template.name}`;+ if (template.adjustment !== undefined) {+ const adj = template.adjustment;+ const op = adj >= 0 ? 'increase' : 'decrease';+ const val = Math.abs(adj);+ result += ` [${op} ${val}%]`;+ }+ return result;+ }+ case 'percentage': {+ // #template[-prio] <percent>% of [previous ]<category>+ const prev = template.previous ? 'previous ' : '';+ return `${prefix} ${trimTrailingZeros(template.percent)}% of ${prev}${template.category}`.trim();+ }+ case 'periodic': {+ // #template[-prio] <amount> repeat every <n> <period>(s) starting <date> [limit]+ const periodPart = periodToString(template.period);+ let result = `${prefix} ${template.amount} repeat every ${periodPart} starting ${template.starting}`;+ if (template.limit) {+ result += ` ${limitToString(template.limit)}`;+ }+ return result;+ }+ case 'by':+ case 'spend': {+ // #template[-prio] <amount> by <month> [spend from <month>] [repeat every <...>]+ let result = `${prefix} ${template.amount} by ${template.month}`;+ if (template.type === 'spend' && template.from) {+ result += ` spend from ${template.from}`;+ }+ // repeat info+ if (template.annual !== undefined) {+ const repeatInfo = repeatToString(template.annual, template.repeat);+ if (repeatInfo) {+ result += ` repeat every ${repeatInfo}`;+ }+ }+ return result;+ }+ case 'remainder': {+ // #template remainder [weight] [limit]+ let result = `${prefix} remainder`;+ if (template.weight !== undefined && template.weight !== 1) {+ result += ` ${template.weight}`;+ }+ if (template.limit) {+ result += ` ${limitToString(template.limit)}`;+ }+ return result;+ }+ case 'average': {+ // #template average <numMonths> months+ return `${prefix} average ${template.numMonths} months`;+ }+ case 'copy': {+ // #template copy from <lookBack> months ago [limit]+ const result = `${prefix} copy from ${template.lookBack} months ago`;+ return result;+ }+ default:+ return [];+ }+ })+ .join('\n');+}++function limitToString(limit: {+ amount: number;+ hold: boolean;+ period?: 'daily' | 'weekly' | 'monthly';+ start?: string | undefined;+}): string {+ switch (limit.period) {+ case 'weekly': {+ // Needs start date per grammar+ const base = `up to ${limit.amount} per week starting ${limit.start}`;+ return limit.hold ? `${base} hold` : base;+ }+ case 'daily': {+ const base = `up to ${limit.amount} per day`;+ return limit.hold ? `${base} hold` : base;+ }+ case 'monthly':+ default: {+ const base = `up to ${limit.amount}`;+ return limit.hold ? `${base} hold` : base;+ }+ }+}++function periodToString(p: {+ period: 'day' | 'week' | 'month' | 'year';+ amount: number;+}): string {+ const { period, amount } = p;+ if (amount === 1) {+ return period; // singular+ }+ // pluralize simple+ return `${amount} ${period}s`;+}++function repeatToString(annual?: boolean, repeat?: number): string | null {+ if (annual === undefined) return null;+ if (annual) {+ if (!repeat || repeat === 1) return 'year';+ return `${repeat} years`;+ }+ // monthly+ if (!repeat || repeat === 1) return 'month';+ return `${repeat} months`;+}++function trimTrailingZeros(n: number): string {+ const s = n.toString();+ if (!s.includes('.')) return s;+ return s.replace(/\.0+$/, '').replace(/(\.[0-9]*[1-9])0+$/, '$1');+}diff --git a/packages/loot-core/src/server/notes/app.ts b/packages/loot-core/src/server/notes/app.tsindex a597cd9b70..f59eb028ff 100644--- a/packages/loot-core/src/server/notes/app.ts+++ b/packages/loot-core/src/server/notes/app.ts@@ -1,13 +1,17 @@import { NoteEntity } from '../../types/models';import { createApp } from '../app';import * as db from '../db';+import { mutator } from '../mutators';+import { undoable } from '../undo';export type NotesHandlers = {'notes-save': typeof updateNotes;+ 'notes-save-undoable': typeof updateNotes;};export const app = createApp<NotesHandlers>();app.method('notes-save', updateNotes);+app.method('notes-save-undoable', mutator(undoable(updateNotes)));async function updateNotes({ id, note }: NoteEntity) {await db.update('notes', { id, note });diff --git a/upcoming-release-notes/5624.md b/upcoming-release-notes/5624.mdnew file mode 100644index 0000000000..7a1444f8f7--- /dev/null+++ b/upcoming-release-notes/5624.md@@ -0,0 +1,6 @@+---+category: Features+authors: [jfdoming]+---++Add tools to migrate/un-migrate to/from UI automations