FredrikOseberg / react-chatbot-kit

MIT License
297 stars 139 forks source link

[BUG] Chat history is not being saved #94

Open smac89 opened 2 years ago

smac89 commented 2 years ago
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.

FredrikOseberg commented 2 years ago
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?

smac89 commented 2 years ago

@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.

FredrikOseberg commented 2 years ago

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 .

steverethink commented 1 year ago

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.

bushev commented 1 year ago

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

ToJen commented 1 year ago

Is this fixed?

Z3-N0 commented 7 months ago

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

smac89 commented 7 months ago

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.

FredrikOseberg commented 7 months ago

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

KristenLeach commented 5 months ago

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?

karzunn commented 3 months ago

Facing the same issue still on 2.2.1