react-chatbot-kit icon indicating copy to clipboard operation
react-chatbot-kit copied to clipboard

[BUG] Chat history is not being saved

Open smac89 opened this issue 3 years ago • 12 comments
trafficstars

react-chatbot-kit version: 2.0.0

I am trying to save the chat history, but the saveMessages callback is not being called. I have traced the problem to be caused by this line in the code:

https://github.com/FredrikOseberg/react-chatbot-kit/blob/6913773873836ace7a1495a4ec09f6dbd642190b/src/components/Chat/Chat.tsx#L83-L85

As you can see, the useEffect is has a dependency which is set to a useRef value. The problem is that refs are not considered part of the state, so any changes made to them is not tracked. This will ultimately lead to this callback not working as expected: https://github.com/FredrikOseberg/react-chatbot-kit/blob/6913773873836ace7a1495a4ec09f6dbd642190b/src/hooks/useChatbot.ts#L89-L98

See this stackoverflow question.

I could be wrong, but this was just my observation after not being able to save history and looking through the logic used in the code.

smac89 avatar Dec 01 '21 21:12 smac89

react-chatbot-kit version: 2.0.0

I am trying to save the chat history, but the saveMessages callback is not being called. I have traced the problem to be caused by this line in the code:

https://github.com/FredrikOseberg/react-chatbot-kit/blob/6913773873836ace7a1495a4ec09f6dbd642190b/src/components/Chat/Chat.tsx#L83-L85

As you can see, the useEffect is has a dependency which is set to a useRef value. The problem is that refs are not considered part of the state, so any changes made to them is not tracked. This will ultimately lead to this callback not working as expected:

https://github.com/FredrikOseberg/react-chatbot-kit/blob/6913773873836ace7a1495a4ec09f6dbd642190b/src/hooks/useChatbot.ts#L89-L98

See this stackoverflow question.

I could be wrong, but this was just my observation after not being able to save history and looking through the logic used in the code.

The saveMessages function is set to trigger when the component unmounts from the DOM. How are you trying to save the messages?

FredrikOseberg avatar Dec 02 '21 21:12 FredrikOseberg

@FredrikOseberg that's exactly how I am doing it. I use mui components, and I am wrapping the chatbot container within a Fade component:

<Fade mountOnEnter unmountOnExit in={isChatbotShowing}>
  <Chatbot saveMessages={handleSaveMessageHistory} />
</Fade>

As you can see, when the chatbot is not being shown, it is unmounted due to the props passed to Fade, however I notice that the handleSaveMessageHistory method is never called.

smac89 avatar Dec 03 '21 15:12 smac89

Ok cool. I will try to reproduce and see if this is the cause.

fre. 3. des. 2021 kl. 16:25 skrev smac89 @.***>:

@FredrikOseberg https://github.com/FredrikOseberg that's exactly how I am doing it. I use mui https://mui.com/ components, and I am wrapping the chatbot container within a Fade https://mui.com/api/fade/#main-content component:

As you can see, when the chatbot is not being shown, it is unmounted due to the props passed to Fade, however I notice that the handleSaveMessageHistory method is never called.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/FredrikOseberg/react-chatbot-kit/issues/94#issuecomment-985608661, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD2WIPSORXAJP7Z46U2HNNLUPDONTANCNFSM5JFPRGUA .

FredrikOseberg avatar Dec 03 '21 15:12 FredrikOseberg

is this fixed? i seem to be running into the same issue. using this example exactly and nothing actually seems to be saved. when i toggle it off and on, everything in the chat window is always reset.

steverethink avatar Jan 28 '23 16:01 steverethink

Hey @smac89, did you find any workaround for that issue?

bushev avatar Mar 19 '23 14:03 bushev

Is this fixed?

ToJen avatar May 08 '23 19:05 ToJen

@FredrikOseberg has this error been resolved? Because im facing a similar issue with saving messages.

Z3-N0 avatar Nov 27 '23 06:11 Z3-N0

Hey @smac89, did you find any workaround for that issue?

No, unfortunately not. We decided to go a different approach and deployed a pre-built chatbot rather than building one.

smac89 avatar Nov 28 '23 17:11 smac89

@Z3-N0 This should be fixed in #166, can you try version 2.2.1?

FredrikOseberg avatar Nov 28 '23 21:11 FredrikOseberg

I'm facing a similar issue with loading history but have the opposite problem. I can see the data stored in localstorage, but no messages are loaded when the bot is rendered. It always returns an empty array. It is definitely looking for the correct storage key, I am not sure what the issue could be otherwise. This includes initialMessages - even with therunInitialMessagesWithHistory boolean set to true there are no initial messages shown, regardless of what is found in localstorage. Is this the expected behavior?

KristenLeach avatar Jan 15 '24 22:01 KristenLeach

Facing the same issue still on 2.2.1

karzunn avatar Mar 13 '24 17:03 karzunn