Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Prevent repeated API calls in searchMessages after typing stops #655

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading