Skip to content

Commit

Permalink
Fix: Prevent repeated API calls in searchMessages after typing stops (R…
Browse files Browse the repository at this point in the history
…ocketChat#655)

* Fix: Prevent repeated API calls in searchMessages after typing stops

* Implement Rate Limiter

* Revised debounce
  • Loading branch information
SinghaAnirban005 authored Nov 28, 2024
1 parent 75f7760 commit 8ba15a5
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/react/src/views/MessageAggregators/SearchMessages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useContext, useEffect } from 'react';
import React, { useState, useContext, useEffect, useCallback } from 'react';
import debounce from 'lodash/debounce';
import { useComponentOverrides } from '@embeddedchat/ui-elements';
import RCContext from '../../context/RCInstance';
Expand All @@ -15,14 +15,17 @@ const SearchMessages = () => {
setText(e.target.value);
};

const searchMessages = async () => {
const searchMessages = useCallback(async () => {
const { messages } = await RCInstance.getSearchMessages(text);
setMessageList(messages);
};
}, [text, RCInstance]);

const debouncedSearch = debounce(async () => {
await searchMessages();
}, 500);
const debouncedSearch = useCallback(
debounce(async () => {
await searchMessages();
}, 500),
[searchMessages]
);

useEffect(() => {
if (!text.trim()) {
Expand Down

0 comments on commit 8ba15a5

Please sign in to comment.