-
Notifications
You must be signed in to change notification settings - Fork 237
chore(compass-assistant): add auto-scroll to bottom with entry points and message sending COMPASS-9887 #7374
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React, { useCallback, useEffect, useContext } from 'react'; | ||
import React, { useCallback, useEffect, useContext, useRef } from 'react'; | ||
import type { AssistantMessage } from '../compass-assistant-provider'; | ||
import { AssistantActionsContext } from '../compass-assistant-provider'; | ||
import type { Chat } from '../@ai-sdk/react/chat-react'; | ||
|
@@ -203,6 +203,11 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({ | |
}) => { | ||
const track = useTelemetry(); | ||
const darkMode = useDarkMode(); | ||
const messagesContainerRef = useRef<HTMLDivElement>(null); | ||
const previousLastMessageId = useRef<string | undefined>(undefined); | ||
const lastMessageId = chat.messages[chat.messages.length - 1]?.id; | ||
const lastMessageIsUser = | ||
chat.messages[chat.messages.length - 1]?.role === 'user'; | ||
|
||
const { ensureOptInAndSend } = useContext(AssistantActionsContext); | ||
const { messages, status, error, clearError, setMessages } = useChat({ | ||
|
@@ -214,6 +219,26 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({ | |
}, | ||
}); | ||
|
||
const scrollToBottom = useCallback(() => { | ||
if (messagesContainerRef.current) { | ||
// Since the container uses flexDirection: 'column-reverse', | ||
// scrolling to the bottom means setting scrollTop to 0 | ||
messagesContainerRef.current.scrollTop = 0; | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
if ( | ||
lastMessageId && | ||
previousLastMessageId.current !== undefined && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to check if there was a message and one before that? If not, what's the harm in scrolling to the bottom if the list is and was empty? If there's a reason we should probably spell it all out in a comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just wanted to avoid unnecessary behavior of scroll to bottom, either is fine. |
||
lastMessageId !== previousLastMessageId.current && | ||
lastMessageIsUser | ||
) { | ||
scrollToBottom(); | ||
} | ||
previousLastMessageId.current = lastMessageId; | ||
}, [lastMessageId, lastMessageIsUser, scrollToBottom]); | ||
|
||
useEffect(() => { | ||
const hasExistingNonGenuineWarning = chat.messages.some( | ||
(message) => message.id === 'non-genuine-warning' | ||
|
@@ -232,17 +257,18 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({ | |
}, [hasNonGenuineConnections, chat, setMessages]); | ||
|
||
const handleMessageSend = useCallback( | ||
(messageBody: string) => { | ||
async (messageBody: string) => { | ||
const trimmedMessageBody = messageBody.trim(); | ||
if (trimmedMessageBody) { | ||
await chat.stop(); | ||
void ensureOptInAndSend?.({ text: trimmedMessageBody }, {}, () => { | ||
track('Assistant Prompt Submitted', { | ||
user_input_length: trimmedMessageBody.length, | ||
}); | ||
}); | ||
} | ||
}, | ||
[track, ensureOptInAndSend] | ||
[track, ensureOptInAndSend, chat] | ||
); | ||
|
||
const handleFeedback = useCallback( | ||
|
@@ -343,6 +369,7 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({ | |
<div | ||
data-testid="assistant-chat-messages" | ||
className={messageFeedFixesStyles} | ||
ref={messagesContainerRef} | ||
> | ||
<div className={messagesWrapStyles}> | ||
{messages.map((message, index) => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.