Skip to content

A local-first personal finance app

fix potentially inconsistent state updates (#7523)

Matt Fiddamandc8694cc3a21
diff --git a/packages/desktop-client/src/components/accounts/Account.tsx b/packages/desktop-client/src/components/accounts/Account.tsx
index 8b02b0a2d6..c70ed5b1b8 100644
--- a/packages/desktop-client/src/components/accounts/Account.tsx
+++ b/packages/desktop-client/src/components/accounts/Account.tsx
@@ -495,16 +495,18 @@ class AccountInternal extends PureComponent<
}
}
+ const balances = this.state.showBalances
+ ? await this.calculateBalances()
+ : null;
+ const filteredAmount = await this.getFilteredAmount();
this.setState(
{
transactions: data,
transactionsFiltered: isFiltered,
loading: false,
workingHard: false,
- balances: this.state.showBalances
- ? await this.calculateBalances()
- : null,
- filteredAmount: await this.getFilteredAmount(),
+ balances,
+ filteredAmount,
},
() => {
if (firstLoad) {
@@ -1025,10 +1027,10 @@ class AccountInternal extends PureComponent<
const lastReconciled = new Date().getTime().toString();
this.props.onUpdateAccount({ ...account, last_reconciled: lastReconciled });
- this.setState({
+ this.setState(state => ({
reconcileAmount: null,
- showCleared: this.state.prevShowCleared,
- });
+ showCleared: state.prevShowCleared,
+ }));
};
onCreateReconciliationTransaction = async (diff: number) => {
@@ -1046,9 +1048,9 @@ class AccountInternal extends PureComponent<
]);
// Optimistic UI: update the transaction list before sending the data to the database
- this.setState({
- transactions: [...reconciliationTransactions, ...this.state.transactions],
- });
+ this.setState(state => ({
+ transactions: [...reconciliationTransactions, ...state.transactions],
+ }));
// run rules on the reconciliation transaction
const ruledTransactions = await Promise.all(
@@ -1359,10 +1361,10 @@ class AccountInternal extends PureComponent<
};
onConditionsOpChange = (value: 'and' | 'or') => {
- this.setState({ filterConditionsOp: value });
- this.setState({
- filterId: { ...this.state.filterId, status: 'changed' } as SavedFilter,
- });
+ this.setState(state => ({
+ filterConditionsOp: value,
+ filterId: { ...state.filterId, status: 'changed' } as SavedFilter,
+ }));
void this.applyFilters([...this.state.filterConditions]);
if (this.state.search !== '') {
this.onSearch(this.state.search);
@@ -1384,7 +1386,9 @@ class AccountInternal extends PureComponent<
void this.applyFilters([...(savedFilter.conditions ?? [])]);
}
}
- this.setState({ filterId: { ...this.state.filterId, ...savedFilter } });
+ this.setState(state => ({
+ filterId: { ...state.filterId, ...savedFilter },
+ }));
};
onClearFilters = () => {
@@ -1405,12 +1409,12 @@ class AccountInternal extends PureComponent<
c === oldCondition ? updatedCondition : c,
),
);
- this.setState({
+ this.setState(state => ({
filterId: {
- ...this.state.filterId,
- status: this.state.filterId && 'changed',
+ ...state.filterId,
+ status: state.filterId && 'changed',
} as SavedFilter,
- });
+ }));
if (this.state.search !== '') {
this.onSearch(this.state.search);
}
@@ -1421,15 +1425,14 @@ class AccountInternal extends PureComponent<
this.state.filterConditions.filter(c => c !== condition),
);
if (this.state.filterConditions.length === 1) {
- this.setState({ filterId: undefined });
- this.setState({ filterConditionsOp: 'and' });
+ this.setState({ filterId: undefined, filterConditionsOp: 'and' });
} else {
- this.setState({
+ this.setState(state => ({
filterId: {
- ...this.state.filterId,
- status: this.state.filterId && 'changed',
+ ...state.filterId,
+ status: state.filterId && 'changed',
} as SavedFilter,
- });
+ }));
}
if (this.state.search !== '') {
this.onSearch(this.state.search);
@@ -1467,12 +1470,12 @@ class AccountInternal extends PureComponent<
return;
}
- this.setState({
+ this.setState(state => ({
filterId: {
- ...this.state.filterId,
- status: this.state.filterId && 'changed',
+ ...state.filterId,
+ status: state.filterId && 'changed',
} as SavedFilter,
- });
+ }));
void this.applyFilters([...filterConditions, condition]);
}
@@ -1672,25 +1675,25 @@ class AccountInternal extends PureComponent<
if (headerClicked === this.state.sort?.field) {
prevField = this.state.sort.prevField;
prevAscDesc = this.state.sort.prevAscDesc;
- this.setState({
+ this.setState(state => ({
sort: {
- ...this.state.sort,
+ ...state.sort,
ascDesc,
},
- });
+ }));
} else {
//if switching to new column then capture state
//of current sort column as prev
prevField = this.state.sort?.field;
prevAscDesc = this.state.sort?.ascDesc;
- this.setState({
+ this.setState(state => ({
sort: {
field: headerClicked,
ascDesc,
- prevField: this.state.sort?.field,
- prevAscDesc: this.state.sort?.ascDesc,
+ prevField: state.sort?.field,
+ prevAscDesc: state.sort?.ascDesc,
},
- });
+ }));
}
this.applySort(headerClicked, ascDesc, prevField, prevAscDesc);
diff --git a/upcoming-release-notes/7523.md b/upcoming-release-notes/7523.md
new file mode 100644
index 0000000000..b214913c85
--- /dev/null
+++ b/upcoming-release-notes/7523.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [matt-fidd]
+---
+
+Fix potentially inconsistent state updates