Skip to content

Commit

Permalink
Merge pull request #172 from actocracy/fix/immutable-update-state-mes…
Browse files Browse the repository at this point in the history
…sage

fix: update message object from state immutably
  • Loading branch information
FredrikOseberg authored Dec 16, 2023
2 parents a6e82e7 + 2b0118d commit 998ff70
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/components/ChatbotMessage/ChatbotMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,15 @@ const ChatbotMessage = ({
if (delay) defaultDisableTime += delay;

timeoutId = setTimeout(() => {
const newMessages = [...messages];
const 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.loading = false;
message.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 998ff70

Please sign in to comment.