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

in WebChat.tsx, in the onNewMessage event handler, what was the plan for interacting with useState values (both getting and setting)? #492

Open
MisterRichards opened this issue Jun 20, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@MisterRichards
Copy link

MisterRichards commented Jun 20, 2024

Is your feature request related to a problem? Please describe.
While trying to respond to certain message values that will trigger custom state for our version of WebChat, the onNewMessage event handler isn't able to read ANY of the values setup with useState in WebChat.tsx.

Describe the solution you'd like
Having wracked my brain over these coals for the past 2 days, besides someone else anticipating the need, I'm at a loss. Everything I have tried so far has failed. I can call the set functions inside of a window.setTimeout with a 50 millisecond interval, and those do work, but the need, in this case, is to read the values inside the event, not just set them. All the useState values show up inside the event with their initial values. This also includes the state values from the store, I tried it both ways, they all show only the initial values.

Describe alternatives you've considered
First, I attempted to pull the onNewMessage function out into a property, that I would then reset ever render of the chat control in an attempt to keep the reference current. I even attempted to wrap the onNewMessage function in a useRef, and then tweaked the onNewMessage function in the OmnichannelSDK (like so)
image

and then manually re-set the ref on every re-render of the WebChat component and still no joy.

Additional context
This problem sounds simple. At least, that's what my boss says. Copilot and ChatGPT seem to disagree with him, however.

@MisterRichards MisterRichards added the enhancement New feature or request label Jun 20, 2024
@MisterRichards
Copy link
Author

OK. it's terribly yucky... but here is what I did:

    // we maintain separate instances of the store for each chat component and pass them in as properties
    const { state, dispatch } = useContext(props.Store);  
    if (!window.ourChatStates) {
        window.ourChatStates = {};
    }
    window.ourChatStates[props.id] = state;

We have a custom type for our window object, and I added a dictionary to hold the state bag (a dictionary because we have multiple instances of the chat control in our app, distinguished by an id property). Each time through the function, the current state is then being written into the dictionary, and from there, the event handler is able to access the current values.

Given the disconnected nature of the underlying SDK, and the tendency of react to re-render itself, I don't know that I see a better option, at this point. I have to go home and take a shower. I feel dirty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant