Skip to content

Commit

Permalink
Debounce the useTransactionsSearch search method
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-jeremy committed Oct 17, 2024
1 parent 0c4900d commit d98e133
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import React, {
} from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { useDebounceCallback } from 'usehooks-ts';

import {
collapseModals,
getPayees,
Expand Down Expand Up @@ -276,14 +274,12 @@ function TransactionListWithPreviews({
});
}, [dispatch, reloadTransactions]);

const { isSearching, search } = useTransactionsSearch({
const { isSearching, search: onSearch } = useTransactionsSearch({
updateQuery: setTransactionsQuery,
resetQuery: () => setTransactionsQuery(baseTransactionsQuery()),
dateFormat,
});

const onSearch = useDebounceCallback(search, 150);

const onOpenTransaction = useCallback(
(transaction: TransactionEntity) => {
if (!isPreviewId(transaction.id)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React, { useCallback, useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';

import { useDebounceCallback } from 'usehooks-ts';

import { getPayees } from 'loot-core/client/actions';
import {
useTransactions,
Expand Down Expand Up @@ -68,12 +66,11 @@ export function CategoryTransactions({ category, month }) {
});
}, [dispatch, reloadTransactions]);

const { search } = useTransactionsSearch({
const { search: onSearch } = useTransactionsSearch({
updateQuery: setTransactionsQuery,
resetQuery: () => setTransactionsQuery(baseTransactionsQuery()),
dateFormat,
});
const onSearch = useDebounceCallback(search, 150);

const onOpenTransaction = useCallback(
transaction => {
Expand Down
8 changes: 6 additions & 2 deletions packages/loot-core/src/client/data-hooks/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useEffect, useRef, useState, useMemo, useCallback } from 'react';

import debounce from 'lodash/debounce';

import { send } from '../../platform/client/fetch';
import { type Query } from '../../shared/query';
import { ungroupTransactions } from '../../shared/transactions';
Expand Down Expand Up @@ -178,6 +180,7 @@ type UseTransactionsSearchProps = {
updateQuery: (updateFn: (searchQuery: Query) => Query) => void;
resetQuery: () => void;
dateFormat: string;
delayMs?: number;
};

type UseTransactionsSearchResult = {
Expand All @@ -189,11 +192,12 @@ export function useTransactionsSearch({
updateQuery,
resetQuery,
dateFormat,
delayMs = 150,
}: UseTransactionsSearchProps): UseTransactionsSearchResult {
const [isSearching, setIsSearching] = useState(false);

const updateSearchQuery = useCallback(

Check warning on line 199 in packages/loot-core/src/client/data-hooks/transactions.ts

View workflow job for this annotation

GitHub Actions / lint

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead
(searchText: string) => {
debounce((searchText: string) => {
if (searchText === '') {
resetQuery();
setIsSearching(false);
Expand All @@ -203,7 +207,7 @@ export function useTransactionsSearch({
);
setIsSearching(true);
}
},
}, delayMs),
[dateFormat, resetQuery, updateQuery],
);

Expand Down

0 comments on commit d98e133

Please sign in to comment.