-
Notifications
You must be signed in to change notification settings - Fork 21
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
Store more Assistant state in ChatContext
to uncouple it from component tree
#788
Store more Assistant state in ChatContext
to uncouple it from component tree
#788
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code LGTM and works as described 👍
One minor note:
When switching tabs, to a previous conversation, there's a scrolling behavior that I don't think it happened before.
I believe it was a side effect from us moving the state.
Screen.Recording.2025-01-10.at.18.57.26.mov
Nice catch, @bcotrim 👍 That turned out to be a surprisingly tricky issue to debug. Apparently, React's We could also drop |
if ( previousMessagesLength.current === 0 || previousSiteId.current !== siteId ) { | ||
if ( isInitialRenderRef.current ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a key
prop specifically for ContentTabAssistant
in src/components/site-content-tabs.tsx
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That turned out to be a surprisingly tricky issue to debug. Apparently, React's StrictMode intentionally runs effect hooks twice when components are initialized as a way of surfacing bugs in the application. In our case with a hook that scrolls the viewport, their reasoning doesn't really apply, and I recommend that we accept that the dev and prod environments behave differently.
Thank you for the detailed explanation—TIL! I agree that the best approach is to accept this behavior in development for now. My only suggestion would be to add this information as a code comment to save future us (or another developer) some research time.
Makes sense 👍 I've added an explanation and a link to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works as expected.
Related issues
ChatInputContext
withChatContext
#786Proposed Changes
If a user switches tabs in Studio while waiting for a response from Assistant, the response is never registered. This happens because the relevant state is stored at the bottom of the component tree—not in a context provider (and the component tree happens to be configured in a way where the state resets when users switch tabs).
This PR moves several pieces of state from the
useAssistant
anduseAssistantApi
hooks toChatContext
to mitigate this problem.Testing Instructions
Pre-merge Checklist