A local-first personal finance app
✨ Enhance: Formula feedbacks (#6413)
lelemm7fa1ff230e79
diff --git a/packages/desktop-client/src/components/formula/codeMirror-excelLanguage.tsx b/packages/desktop-client/src/components/formula/codeMirror-excelLanguage.tsxindex 008278ce00..6ed9d603d2 100644--- a/packages/desktop-client/src/components/formula/codeMirror-excelLanguage.tsx+++ b/packages/desktop-client/src/components/formula/codeMirror-excelLanguage.tsx@@ -1,5 +1,6 @@/* eslint-disable actual/typography */import React from 'react';+import { Trans } from 'react-i18next';import { styles } from '@actual-app/components/styles';import { theme } from '@actual-app/components/theme';@@ -15,7 +16,12 @@ import {type StreamParser,} from '@codemirror/language';import { type Extension } from '@codemirror/state';-import { hoverTooltip, type Tooltip, EditorView } from '@codemirror/view';+import {+ hoverTooltip,+ type Tooltip,+ EditorView,+ tooltips,+} from '@codemirror/view';import { tags } from '@lezer/highlight';import { t } from 'i18next';import { createRoot } from 'react-dom/client';@@ -38,7 +44,9 @@ function FunctionTooltip({<div style={{ fontWeight: 600, marginBottom: '4px' }}>{name}</div><div style={{ marginBottom: '8px' }}>{description}</div><div style={{ fontSize: '0.9em', opacity: 0.8 }}>- <div style={{ fontWeight: 500, marginBottom: '4px' }}>Parameters:</div>+ <div style={{ fontWeight: 500, marginBottom: '4px' }}>+ <Trans>Parameters:</Trans>+ </div>{parameters.map((p, i) => (<div key={i} style={{ marginBottom: '2px' }}>•{' '}@@ -416,7 +424,13 @@ function getFunctionCompletions(mode: FormulaMode): Completion[] {label: name,type: 'function',section,- info: `${func.description}\n\nParameters: ${func.parameters.map(p => p.name).join(', ')}\n\n${func.parameters.map(p => `- ${p.name}: ${p.description}`).join('\n')}`,+ info: [+ func.description,+ '',+ `${t('Parameters:')} ${func.parameters.map(p => p.name).join(', ')}`,+ '',+ func.parameters.map(p => `- ${p.name}: ${p.description}`).join('\n'),+ ].join('\n'),apply: `${name}()`,boost: 10, // Boost functions to appear higher});@@ -560,18 +574,7 @@ export function excelFormulaHover(mode: FormulaMode): Extension {create() {const dom = document.createElement('div');dom.className = 'cm-tooltip-hover';- Object.assign(dom.style, {- padding: '8px',- boxShadow:- '0 15px 30px 0 rgba(0,0,0,0.11), 0 5px 15px 0 rgba(0,0,0,0.08)',- borderWidth: '2px',- borderRadius: '4px',- borderStyle: 'solid',- borderColor: theme.tooltipBorder,- backgroundColor: theme.tooltipBackground,- color: theme.tooltipText,- overflow: 'auto',- });+ Object.assign(dom.style, styles.tooltip, { padding: '8px' });const root = createRoot(dom);root.render(<FunctionTooltip@@ -601,18 +604,7 @@ export function excelFormulaHover(mode: FormulaMode): Extension {create() {const dom = document.createElement('div');dom.className = 'cm-tooltip-hover';- Object.assign(dom.style, {- padding: '8px',- boxShadow:- '0 15px 30px 0 rgba(0,0,0,0.11), 0 5px 15px 0 rgba(0,0,0,0.08)',- borderWidth: '2px',- borderRadius: '4px',- borderStyle: 'solid',- borderColor: theme.tooltipBorder,- backgroundColor: theme.tooltipBackground,- color: theme.tooltipText,- overflow: 'auto',- });+ Object.assign(dom.style, styles.tooltip, { padding: '8px' });const root = createRoot(dom);const infoText = typeof field.info === 'string' ? field.info : '';root.render(<FieldTooltip label={field.label} info={infoText} />);@@ -634,6 +626,143 @@ export function excelFormulaHover(mode: FormulaMode): Extension {// Excel formula language supportexport const excelFormulaLanguage = StreamLanguage.define(excelFormulaParser);+const tooltipZIndexTheme = EditorView.baseTheme({+ '.cm-tooltip': {+ zIndex: '4000 !important',+ },+ '.cm-tooltip-autocomplete': {+ zIndex: '4000 !important',+ },+});++const tooltipPortalConfig =+ typeof document === 'undefined'+ ? tooltips({ position: 'fixed' })+ : tooltips({ position: 'fixed', parent: document.body });++const autocompletePopoverTheme = EditorView.baseTheme({+ // Wrapper: keep transparent; the list and info panel are the actual "cards".+ '.cm-tooltip.cm-tooltip-autocomplete': {+ backgroundColor: 'transparent',+ border: 'none',+ boxShadow: 'none',+ padding: 0,+ margin: '6px',+ overflow: 'visible',+ fontFamily: 'inherit',+ userSelect: 'none',+ },++ // The suggestion list card+ '.cm-tooltip.cm-tooltip-autocomplete > ul': {+ ...styles.shadowLarge,+ margin: 0,+ // Outer inset gutter for the whole list (so content never hugs the edges)+ padding: '12px',+ maxHeight: '260px',+ minWidth: '280px',+ listStyle: 'none',+ backgroundColor: theme.menuBackground,+ color: theme.menuItemText,+ borderRadius: '6px',+ overflow: 'hidden',+ overflowY: 'auto',+ },++ // Hide CodeMirror's leading icons (gear / f / etc.)+ '.cm-tooltip.cm-tooltip-autocomplete .cm-completionIcon': {+ display: 'none !important',+ width: 0,+ margin: 0,+ padding: 0,+ },+ '.cm-tooltip.cm-tooltip-autocomplete .cm-completionIcon::before': {+ display: 'none !important',+ },++ // Section headers+ '.cm-tooltip.cm-tooltip-autocomplete li.cm-completionSection': {+ padding: '6px 10px 4px',+ fontSize: '11px',+ lineHeight: '1em',+ textTransform: 'uppercase',+ letterSpacing: '0.03em',+ color: theme.menuItemTextHeader,+ opacity: 0.9,+ marginTop: '8px',+ },+ '.cm-tooltip.cm-tooltip-autocomplete li.cm-completionSection:first-child': {+ marginTop: 0,+ },++ // Completion rows+ '.cm-tooltip.cm-tooltip-autocomplete li.cm-completionItem': {+ padding: '8px 10px',+ lineHeight: '1.2',+ cursor: 'default',+ display: 'flex',+ alignItems: 'baseline',+ gap: '8px',+ borderRadius: '4px',+ },++ '.cm-tooltip.cm-tooltip-autocomplete > ul > completion-section, .cm-tooltip.cm-tooltip-autocomplete li.cm-completionSection':+ {+ padding: '0 !important',+ position: 'sticky',+ top: 0,+ backgroundColor: theme.menuBackground,+ opacity: '1 !important',+ zIndex: 1,+ },++ '.cm-tooltip.cm-tooltip-autocomplete li.cm-completionItem[aria-selected]': {+ backgroundColor: theme.menuItemBackgroundHover,+ color: theme.menuItemTextHover,+ },++ // Matched text within a label+ '.cm-tooltip.cm-tooltip-autocomplete .cm-completionMatchedText': {+ fontWeight: '600',+ textDecoration: 'underline',+ textUnderlineOffset: '2px',+ },++ // Label + detail formatting+ '.cm-tooltip.cm-tooltip-autocomplete .cm-completionLabel': {+ fontSize: '13px',+ flex: '1 1 auto',+ overflow: 'hidden',+ textOverflow: 'ellipsis',+ whiteSpace: 'nowrap',+ },+ '.cm-tooltip.cm-tooltip-autocomplete .cm-completionDetail': {+ fontSize: '12px',+ color: theme.menuKeybindingText,+ marginLeft: 'auto',+ whiteSpace: 'nowrap',+ },+ '.cm-tooltip.cm-tooltip-autocomplete li.cm-completionItem[aria-selected] .cm-completionDetail':+ {+ color: theme.menuItemTextHover,+ },++ // Docs panel for selected completion+ '.cm-tooltip.cm-tooltip-autocomplete .cm-completionInfo': {+ backgroundColor: theme.menuBackground,+ color: theme.pageText,+ padding: '12px 14px',+ maxWidth: '360px',+ whiteSpace: 'pre-wrap',+ fontSize: '12px',+ lineHeight: '1.4',+ ...styles.shadowLarge,+ border: 'none',+ borderRadius: '6px',+ marginLeft: '8px',+ },+});+// Custom theme for categorized function highlighting (light)const functionCategoryTheme = EditorView.baseTheme({'&light .cm-keyword': { color: '#0066CC' }, // Math functions in blue@@ -691,5 +820,8 @@ export function excelFormulaExtension(excelFormulaHover(mode),isDark ? excelFormulaDarkHighlighting : excelFormulaHighlighting,isDark ? functionCategoryThemeDark : functionCategoryTheme,+ tooltipZIndexTheme,+ tooltipPortalConfig,+ autocompletePopoverTheme,];}diff --git a/packages/desktop-client/src/components/reports/reports/FormulaCard.tsx b/packages/desktop-client/src/components/reports/reports/FormulaCard.tsxindex da9ba31032..5dfe584a96 100644--- a/packages/desktop-client/src/components/reports/reports/FormulaCard.tsx+++ b/packages/desktop-client/src/components/reports/reports/FormulaCard.tsx@@ -90,7 +90,6 @@ export function FormulaCard({onRemove();break;default:- console.warn(`Unrecognized menu selection: ${item}`);break;}}}diff --git a/packages/desktop-client/src/components/rules/RuleEditor.tsx b/packages/desktop-client/src/components/rules/RuleEditor.tsxindex 346f8cf228..302c095383 100644--- a/packages/desktop-client/src/components/rules/RuleEditor.tsx+++ b/packages/desktop-client/src/components/rules/RuleEditor.tsx@@ -1336,6 +1336,8 @@ export function RuleEditor({overflow: 'auto',maxHeight: 'calc(100% - 300px)',minHeight: 100,+ position: 'relative',+ zIndex: 2,}}><View style={{ flexShrink: 0 }}>@@ -1485,7 +1487,14 @@ export function RuleEditor({</View><SelectedProvider instance={selectedInst}>- <View style={{ padding: '20px', flex: 1 }}>+ <View+ style={{+ padding: '20px',+ flex: 1,+ position: 'relative',+ zIndex: 1,+ }}+ ><SpaceBetweengap={5}style={{diff --git a/packages/loot-core/src/server/dashboard/app.ts b/packages/loot-core/src/server/dashboard/app.tsindex e821239bd8..386d8a225d 100644--- a/packages/loot-core/src/server/dashboard/app.ts+++ b/packages/loot-core/src/server/dashboard/app.ts@@ -81,6 +81,7 @@ const exportModel = {'markdown-card','summary-card','calendar-card',+ 'formula-card',].includes(widget.type)) {throw new ValidationError(diff --git a/packages/loot-core/src/server/rules/action.ts b/packages/loot-core/src/server/rules/action.tsindex f9cf87d3e5..a7b2acaa71 100644--- a/packages/loot-core/src/server/rules/action.ts+++ b/packages/loot-core/src/server/rules/action.ts@@ -3,6 +3,8 @@ import * as dateFns from 'date-fns';import * as Handlebars from 'handlebars';import { HyperFormula } from 'hyperformula';+import { amountToInteger } from 'loot-core/shared/util';+import { logger } from '../../platform/server/log';import { parseDate, format, currentDay } from '../../shared/months';import { FIELD_TYPES } from '../../shared/rules';@@ -298,6 +300,10 @@ export class Action {throw new Error(`Formula error: ${cellValue.message}`);}+ if (typeof cellValue === 'number') {+ return amountToInteger(Math.round(cellValue * 100) / 100);+ }+return cellValue;} catch (err) {logger.error('Formula execution error:', err);diff --git a/packages/loot-core/src/server/rules/formula-action.test.ts b/packages/loot-core/src/server/rules/formula-action.test.tsindex 0ae255b338..fb776f02ce 100644--- a/packages/loot-core/src/server/rules/formula-action.test.ts+++ b/packages/loot-core/src/server/rules/formula-action.test.ts@@ -10,7 +10,7 @@ describe('Formula-based rule actions', () => {const transaction: Partial<TransactionForRules> = { amount: 500 };const result = action.executeFormulaSync('=100 + 200', transaction);- expect(result).toBe(300);+ expect(result).toBe(30000);});it('should use transaction field variables', () => {@@ -18,7 +18,7 @@ describe('Formula-based rule actions', () => {const transaction = { amount: 5000 };const result = action.executeFormulaSync('=amount / 100', transaction);- expect(result).toBe(50);+ expect(result).toBe(5000);});it('should support IF function with transaction fields', () => {@@ -108,7 +108,7 @@ describe('Formula-based rule actions', () => {};const result = action.executeFormulaSync('=balance * 2', transaction);- expect(result).toBe(3000);+ expect(result).toBe(300000);});it('should execute formula and convert to number type', () => {@@ -119,7 +119,7 @@ describe('Formula-based rule actions', () => {const transaction = { amount: 100 };action.exec(transaction);- expect(transaction.amount).toBe(750);+ expect(transaction.amount).toBe(75000);});it('should execute formula and convert to string type', () => {@@ -170,6 +170,6 @@ describe('Formula-based rule actions', () => {action.exec(transaction);// Should convert number to string- expect(transaction.notes).toBe('750');+ expect(transaction.notes).toBe('75000');});});diff --git a/upcoming-release-notes/6413.md b/upcoming-release-notes/6413.mdnew file mode 100644index 0000000000..92a317af99--- /dev/null+++ b/upcoming-release-notes/6413.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [lelemm]+---++Enhance formula feedback functionality for improved user experience and interaction.