From 0e907402b82592cc7965827827151af631819903 Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Fri, 16 Sep 2022 12:19:07 -0300 Subject: [PATCH] Chore: Create useDebounce hook (#4470) * create useDebounce hook * fix types * apply useDebounce hook * migrate to use-debounce lib * remove useless useDebounce --- app/lib/methods/helpers/debounce.ts | 6 +++++ app/views/CannedResponsesListView/index.tsx | 15 +++++------ app/views/DiscussionsView/index.tsx | 30 +++++++++------------ package.json | 1 + yarn.lock | 5 ++++ 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/app/lib/methods/helpers/debounce.ts b/app/lib/methods/helpers/debounce.ts index edc7c5ac8..5061e322b 100644 --- a/app/lib/methods/helpers/debounce.ts +++ b/app/lib/methods/helpers/debounce.ts @@ -1,3 +1,5 @@ +import { useDebouncedCallback } from 'use-debounce'; + export function debounce(func: Function, wait?: number, immediate?: boolean) { let timeout: ReturnType | null; function _debounce(...args: any[]) { @@ -21,3 +23,7 @@ export function debounce(func: Function, wait?: number, immediate?: boolean) { _debounce.stop = () => clearTimeout(timeout!); return _debounce; } + +export function useDebounce(func: (...args: any) => any, wait?: number): (...args: any[]) => void { + return useDebouncedCallback(func, wait || 1000); +} diff --git a/app/views/CannedResponsesListView/index.tsx b/app/views/CannedResponsesListView/index.tsx index bd2acd611..d525a6fa3 100644 --- a/app/views/CannedResponsesListView/index.tsx +++ b/app/views/CannedResponsesListView/index.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState, useCallback } from 'react'; +import React, { useEffect, useState } from 'react'; import { FlatList } from 'react-native'; import { RouteProp } from '@react-navigation/native'; import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack'; @@ -24,7 +24,7 @@ import DropdownItemHeader from './Dropdown/DropdownItemHeader'; import styles from './styles'; import { ICannedResponse } from '../../definitions/ICannedResponse'; import { ChatsStackParamList } from '../../stacks/types'; -import { getRoomTitle, getUidDirectMessage, debounce } from '../../lib/methods/helpers'; +import { getRoomTitle, getUidDirectMessage, useDebounce } from '../../lib/methods/helpers'; import { Services } from '../../lib/services'; import { ILivechatDepartment } from '../../definitions/ILivechatDepartment'; import { useAppSelector } from '../../lib/hooks'; @@ -88,7 +88,7 @@ const CannedResponsesListView = ({ navigation, route }: ICannedResponsesListView } }; - const getDepartments = debounce(async () => { + const getDepartments = useDebounce(async () => { try { const res = await Services.getDepartments(); if (res.success) { @@ -187,12 +187,9 @@ const CannedResponsesListView = ({ navigation, route }: ICannedResponsesListView } }, [departments, cannedResponses]); - const searchCallback = useCallback( - debounce(async (text = '', department = '', depId = '') => { - await getListCannedResponse({ text, department, depId, debounced: true }); - }, 1000), - [] - ); // use debounce with useCallback https://stackoverflow.com/a/58594890 + const searchCallback = useDebounce(async (text = '', department = '', depId = '') => { + await getListCannedResponse({ text, department, depId, debounced: true }); + }, 1000); useEffect(() => { getRoomFromDb(); diff --git a/app/views/DiscussionsView/index.tsx b/app/views/DiscussionsView/index.tsx index 512f84e18..5785d3e82 100644 --- a/app/views/DiscussionsView/index.tsx +++ b/app/views/DiscussionsView/index.tsx @@ -10,7 +10,7 @@ import ActivityIndicator from '../../containers/ActivityIndicator'; import I18n from '../../i18n'; import StatusBar from '../../containers/StatusBar'; import log from '../../lib/methods/helpers/log'; -import { debounce, isIOS } from '../../lib/methods/helpers'; +import { isIOS, useDebounce } from '../../lib/methods/helpers'; import SafeAreaView from '../../containers/SafeAreaView'; import * as HeaderButton from '../../containers/HeaderButton'; import * as List from '../../containers/List'; @@ -81,10 +81,10 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re } }; - const onSearchChangeText = debounce(async (text: string) => { + const onSearchChangeText = useDebounce(async (text: string) => { setIsSearching(true); await load(text); - }, 300); + }, 500); const onCancelSearchPress = () => { setIsSearching(false); @@ -145,20 +145,16 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re navigation.setOptions(options); }, [navigation, isSearching]); - const onDiscussionPress = debounce( - (item: TThreadModel) => { - if (item.drid && item.t) { - navigation.push('RoomView', { - rid: item.drid, - prid: item.rid, - name: item.msg, - t - }); - } - }, - 1000, - true - ); + const onDiscussionPress = (item: TThreadModel) => { + if (item.drid && item.t) { + navigation.push('RoomView', { + rid: item.drid, + prid: item.rid, + name: item.msg, + t + }); + } + }; const renderItem = ({ item }: { item: IMessageFromServer }) => (