Skip to content

A local-first personal finance app

Use strict typing in useSheetValue and fix bug where query is not being updated when changed (#3864)

Joel Jeremy Marquezc5411518c465
diff --git a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-1-chromium-linux.png b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-1-chromium-linux.png
index fbac8396fa..6d47cb90a5 100644
Binary files a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-1-chromium-linux.png and b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-2-chromium-linux.png b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-2-chromium-linux.png
index cd6cc7d544..33925a4d26 100644
Binary files a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-2-chromium-linux.png and b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-4-chromium-linux.png b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-4-chromium-linux.png
index 40a92b53ef..538c80c745 100644
Binary files a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-4-chromium-linux.png and b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-4-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-5-chromium-linux.png b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-5-chromium-linux.png
index 5badb70080..ac6583f5c4 100644
Binary files a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-5-chromium-linux.png and b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-5-chromium-linux.png differ
diff --git a/packages/desktop-client/src/components/accounts/Reconcile.tsx b/packages/desktop-client/src/components/accounts/Reconcile.tsx
index 77e0dc1459..e3136bea02 100644
--- a/packages/desktop-client/src/components/accounts/Reconcile.tsx
+++ b/packages/desktop-client/src/components/accounts/Reconcile.tsx
@@ -29,11 +29,13 @@ export function ReconcilingMessage({
onDone,
onCreateTransaction,
}: ReconcilingMessageProps) {
- const cleared = useSheetValue<'balance', `balance-query-${string}-cleared`>({
- name: (balanceQuery.name + '-cleared') as `balance-query-${string}-cleared`,
- value: 0,
- query: balanceQuery.query.filter({ cleared: true }),
- });
+ const cleared =
+ useSheetValue<'balance', `balance-query-${string}-cleared`>({
+ name: (balanceQuery.name +
+ '-cleared') as `balance-query-${string}-cleared`,
+ value: 0,
+ query: balanceQuery.query.filter({ cleared: true }),
+ }) ?? 0;
const format = useFormat();
const targetDiff = targetBalance - cleared;
diff --git a/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx b/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx
index 735982713b..1939b82e65 100644
--- a/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx
+++ b/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx
@@ -393,7 +393,7 @@ function CategoryItem({
>(balanceBinding);
const isToBeBudgetedItem = item.id === 'to-be-budgeted';
- const toBudget = useEnvelopeSheetValue(envelopeBudget.toBudget) ?? 0;
+ const toBudget = useEnvelopeSheetValue(envelopeBudget.toBudget);
return (
<div
@@ -429,10 +429,13 @@ function CategoryItem({
display: !showBalances ? 'none' : undefined,
marginLeft: 5,
flexShrink: 0,
- ...makeAmountFullStyle(isToBeBudgetedItem ? toBudget : balance, {
- positiveColor: theme.noticeTextMenu,
- negativeColor: theme.errorTextMenu,
- }),
+ ...makeAmountFullStyle(
+ (isToBeBudgetedItem ? toBudget : balance) || 0,
+ {
+ positiveColor: theme.noticeTextMenu,
+ negativeColor: theme.errorTextMenu,
+ },
+ ),
}}
>
{isToBeBudgetedItem
diff --git a/packages/desktop-client/src/components/budget/envelope/BalanceMenu.tsx b/packages/desktop-client/src/components/budget/envelope/BalanceMenu.tsx
index 83244cf4a9..f9762b5cc8 100644
--- a/packages/desktop-client/src/components/budget/envelope/BalanceMenu.tsx
+++ b/packages/desktop-client/src/components/budget/envelope/BalanceMenu.tsx
@@ -29,7 +29,8 @@ export function BalanceMenu({
const carryover = useEnvelopeSheetValue(
envelopeBudget.catCarryover(categoryId),
);
- const balance = useEnvelopeSheetValue(envelopeBudget.catBalance(categoryId));
+ const balance =
+ useEnvelopeSheetValue(envelopeBudget.catBalance(categoryId)) ?? 0;
return (
<Menu
diff --git a/packages/desktop-client/src/components/budget/envelope/BalanceMovementMenu.tsx b/packages/desktop-client/src/components/budget/envelope/BalanceMovementMenu.tsx
index f83ce9f2d6..398ae5f2d2 100644
--- a/packages/desktop-client/src/components/budget/envelope/BalanceMovementMenu.tsx
+++ b/packages/desktop-client/src/components/budget/envelope/BalanceMovementMenu.tsx
@@ -20,9 +20,9 @@ export function BalanceMovementMenu({
onBudgetAction,
onClose = () => {},
}: BalanceMovementMenuProps) {
- const catBalance = useEnvelopeSheetValue(
- envelopeBudget.catBalance(categoryId),
- );
+ const catBalance =
+ useEnvelopeSheetValue(envelopeBudget.catBalance(categoryId)) ?? 0;
+
const [menu, _setMenu] = useState('menu');
const ref = useRef<HTMLSpanElement>(null);
diff --git a/packages/desktop-client/src/components/budget/envelope/budgetsummary/ToBudgetMenu.tsx b/packages/desktop-client/src/components/budget/envelope/budgetsummary/ToBudgetMenu.tsx
index 28978d6a13..3dd9b4a685 100644
--- a/packages/desktop-client/src/components/budget/envelope/budgetsummary/ToBudgetMenu.tsx
+++ b/packages/desktop-client/src/components/budget/envelope/budgetsummary/ToBudgetMenu.tsx
@@ -25,7 +25,7 @@ export function ToBudgetMenu({
const { t } = useTranslation();
const toBudget = useEnvelopeSheetValue(envelopeBudget.toBudget) ?? 0;
- const forNextMonth = useEnvelopeSheetValue(envelopeBudget.forNextMonth);
+ const forNextMonth = useEnvelopeSheetValue(envelopeBudget.forNextMonth) ?? 0;
const items = [
...(toBudget > 0
? [
diff --git a/packages/desktop-client/src/components/spreadsheet/useSheetValue.ts b/packages/desktop-client/src/components/spreadsheet/useSheetValue.ts
index 62061fede0..692adeac56 100644
--- a/packages/desktop-client/src/components/spreadsheet/useSheetValue.ts
+++ b/packages/desktop-client/src/components/spreadsheet/useSheetValue.ts
@@ -1,6 +1,6 @@
-// @ts-strict-ignore
import { useState, useRef, useLayoutEffect } from 'react';
+import { type Query } from 'loot-core/shared/query';
import { useSpreadsheet } from 'loot-core/src/client/SpreadsheetProvider';
import { useSheetName } from './useSheetName';
@@ -12,13 +12,22 @@ import {
type Binding,
} from '.';
+type SheetValueResult<
+ SheetName extends SheetNames,
+ FieldName extends SheetFields<SheetName>,
+> = {
+ name: string;
+ value: Spreadsheets[SheetName][FieldName] | null;
+ query?: Query;
+};
+
export function useSheetValue<
SheetName extends SheetNames,
FieldName extends SheetFields<SheetName>,
>(
binding: Binding<SheetName, FieldName>,
- onChange?: (result) => void,
-): Spreadsheets[SheetName][FieldName] {
+ onChange?: (result: SheetValueResult<SheetName, FieldName>) => void,
+): SheetValueResult<SheetName, FieldName>['value'] {
const { sheetName, fullSheetName } = useSheetName(binding);
const bindingObj =
@@ -27,34 +36,37 @@ export function useSheetValue<
: binding;
const spreadsheet = useSpreadsheet();
- const [result, setResult] = useState({
+ const [result, setResult] = useState<SheetValueResult<SheetName, FieldName>>({
name: fullSheetName,
value: bindingObj.value === undefined ? null : bindingObj.value,
query: bindingObj.query,
});
const latestOnChange = useRef(onChange);
- const latestValue = useRef(result.value);
+ latestOnChange.current = onChange;
- useLayoutEffect(() => {
- latestOnChange.current = onChange;
- latestValue.current = result.value;
- });
+ const latestValue = useRef(result.value);
+ latestValue.current = result.value;
useLayoutEffect(() => {
if (bindingObj.query) {
spreadsheet.createQuery(sheetName, bindingObj.name, bindingObj.query);
}
- return spreadsheet.bind(sheetName, binding, null, newResult => {
- if (latestOnChange.current) {
- latestOnChange.current(newResult);
- }
+ return spreadsheet.bind(
+ sheetName,
+ binding,
+ null,
+ (newResult: SheetValueResult<SheetName, FieldName>) => {
+ if (latestOnChange.current) {
+ latestOnChange.current(newResult);
+ }
- if (newResult.value !== latestValue.current) {
- setResult(newResult);
- }
- });
- }, [sheetName, bindingObj.name]);
+ if (newResult.value !== latestValue.current) {
+ setResult(newResult);
+ }
+ },
+ );
+ }, [sheetName, bindingObj.name, bindingObj.query]);
return result.value;
}
diff --git a/upcoming-release-notes/3864.md b/upcoming-release-notes/3864.md
new file mode 100644
index 0000000000..837da8dc6a
--- /dev/null
+++ b/upcoming-release-notes/3864.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [joel-jeremy]
+---
+
+Use strict typing in useSheetValue and fix bug where query is not being updated when the query object changed