From 6a2455b5c78f0bdfc9cfe92dc09d140173664c61 Mon Sep 17 00:00:00 2001 From: rare-magma Date: Sat, 10 Aug 2024 18:14:24 +0200 Subject: [PATCH] fix: infinite loop in calculate button Signed-off-by: rare-magma --- .../CalculateButton/CalculateButton.tsx | 1 + src/hooks/useDB.ts | 28 +++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/components/CalculateButton/CalculateButton.tsx b/src/components/CalculateButton/CalculateButton.tsx index 9049f21..2080436 100644 --- a/src/components/CalculateButton/CalculateButton.tsx +++ b/src/components/CalculateButton/CalculateButton.tsx @@ -253,6 +253,7 @@ export function CalculateButton({ setTimeout(() => { inputRef.current?.focus(); }, 0); + getHistory(); }} > diff --git a/src/hooks/useDB.ts b/src/hooks/useDB.ts index 4983f22..e63e7a5 100644 --- a/src/hooks/useDB.ts +++ b/src/hooks/useDB.ts @@ -386,6 +386,7 @@ export function useDB() { return { filteredIncomes, filteredExpenses }; } + function renameBudget(event: React.ChangeEvent) { if (budget && event.target.value) { const newState = produce((draft) => { @@ -395,18 +396,21 @@ export function useDB() { } } - async function getCalcHist(id: string): Promise { - let item; - await calcHistDB - .getItem(id) - .then((i) => { - item = i; - }) - .catch((e: unknown) => { - throw e; - }); - return item ?? []; - } + const getCalcHist = useCallback( + async (id: string): Promise => { + let item; + await calcHistDB + .getItem(id) + .then((i) => { + item = i; + }) + .catch((e: unknown) => { + throw e; + }); + return item ?? []; + }, + [], + ); async function saveCalcHist(id: string, item: CalculationHistoryItem) { const calcHist = await getCalcHist(id);