Skip to content

Commit

Permalink
Debounce refreshFee calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepinho committed Jan 10, 2024
1 parent ef3c9bd commit 31e2ace
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions apps/webapp/src/components/send/send-form/use-refresh-fee.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { useEffect } from 'react';
import { useCallback, useEffect, useRef } from 'react';
import { sendSelector } from '../../../state/send';
import { useStore } from '../../../state';

const DEBOUNCE_MS = 500;

/**
* Refreshes the fee in the state when the amount, recipient, selection, or memo
* changes.
*/
export const useRefreshFee = () => {
const { amount, recipient, selection, refreshFee } = useStore(sendSelector);
const timeoutId = useRef<number | null>(null);

const debouncedRefreshFee = useCallback(() => {
if (timeoutId.current) {
window.clearTimeout(timeoutId.current);
timeoutId.current = null;
}

timeoutId.current = window.setTimeout(() => {
timeoutId.current = null;
void refreshFee();
}, DEBOUNCE_MS);
}, [refreshFee]);

useEffect(() => {
void refreshFee();
}, [amount, recipient, selection, refreshFee]);
useEffect(debouncedRefreshFee, [amount, recipient, selection, debouncedRefreshFee]);
};

0 comments on commit 31e2ace

Please sign in to comment.