Skip to content

Commit cbac611

Browse files
authored
fix rerender issue with formula card (#6058)
* fix infinite rerender * note * remove React import
1 parent e83cfba commit cbac611

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

packages/desktop-client/src/components/reports/reports/Formula.tsx

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
useState,
33
useRef,
44
useCallback,
5+
useMemo,
56
Suspense,
67
lazy,
78
type ChangeEvent,
@@ -92,12 +93,8 @@ function FormulaInner({ widget }: FormulaInnerProps) {
9293
error,
9394
} = useFormulaExecution(formula, queriesRef.current, queriesVersion);
9495

95-
// Execute color formula with access to main result via named expression
96-
const { result: colorResult, error: colorError } = useFormulaExecution(
97-
colorFormula,
98-
queriesRef.current,
99-
queriesVersion,
100-
{
96+
const colorVariables = useMemo(
97+
() => ({
10198
RESULT: result ?? 0,
10299
...Object.entries(themeColors).reduce(
103100
(acc, [key, value]) => {
@@ -106,7 +103,14 @@ function FormulaInner({ widget }: FormulaInnerProps) {
106103
},
107104
{} as Record<string, string>,
108105
),
109-
},
106+
}),
107+
[result, themeColors],
108+
);
109+
const { result: colorResult, error: colorError } = useFormulaExecution(
110+
colorFormula,
111+
queriesRef.current,
112+
queriesVersion,
113+
colorVariables,
110114
);
111115

112116
const handleQueriesChange = useCallback(
@@ -387,16 +391,7 @@ function FormulaInner({ widget }: FormulaInnerProps) {
387391
<Suspense fallback={<div style={{ height: 32 }} />}>
388392
<FormulaEditor
389393
value={colorFormula}
390-
variables={{
391-
RESULT: result ?? 0,
392-
...Object.entries(themeColors).reduce(
393-
(acc, [key, value]) => {
394-
acc[`theme_${key}`] = value;
395-
return acc;
396-
},
397-
{} as Record<string, string>,
398-
),
399-
}}
394+
variables={colorVariables}
400395
onChange={setColorFormula}
401396
mode="query"
402397
queries={queriesRef.current}

packages/desktop-client/src/components/reports/reports/FormulaCard.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import { useState, useMemo } from 'react';
22
import { useTranslation } from 'react-i18next';
33

44
import { View } from '@actual-app/components/view';
@@ -42,12 +42,8 @@ export function FormulaCard({
4242
meta?.queriesVersion,
4343
);
4444

45-
// Execute color formula with access to main result via named expression
46-
const { result: colorResult, error: colorError } = useFormulaExecution(
47-
colorFormula,
48-
meta?.queries || {},
49-
meta?.queriesVersion,
50-
{
45+
const colorVariables = useMemo(
46+
() => ({
5147
RESULT: result ?? 0,
5248
...Object.entries(themeColors).reduce(
5349
(acc, [key, value]) => {
@@ -56,7 +52,14 @@ export function FormulaCard({
5652
},
5753
{} as Record<string, string>,
5854
),
59-
},
55+
}),
56+
[result, themeColors],
57+
);
58+
const { result: colorResult, error: colorError } = useFormulaExecution(
59+
colorFormula,
60+
meta?.queries || {},
61+
meta?.queriesVersion,
62+
colorVariables,
6063
);
6164

6265
// Determine the custom color from color formula result

upcoming-release-notes/6058.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
category: Bugfix
3+
authors: [matt-fidd]
4+
---
5+
6+
Fix high CPU usage when using experimental formula report

0 commit comments

Comments
 (0)