Skip to content

Commit

Permalink
fix: update entire messages array safely
Browse files Browse the repository at this point in the history
  • Loading branch information
aamikus committed Dec 11, 2023
1 parent 46be71c commit 2b0118d
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/components/ChatbotMessage/ChatbotMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,15 @@ const ChatbotMessage = ({
if (delay) defaultDisableTime += delay;

timeoutId = setTimeout(() => {
const newMessages = [...messages];
let message = newMessages.find((message) => message.id === id);
const newMessages = [...messages].map(message => {
if (message.id === id) {
return {...message, loading: false, delay: undefined};
}

if (!message) return;
message = {
...message,
loading: false,
delay: undefined,
}

setState((state: any) => {
const freshMessages = state.messages;
const messageIdx = freshMessages.findIndex(
(message: any) => message.id === id
);
freshMessages[messageIdx] = message;

return { ...state, messages: freshMessages };
return message;
});

setState((state: any) => ({...state, messages: newMessages}));
}, defaultDisableTime);
};

Expand Down

0 comments on commit 2b0118d

Please sign in to comment.