-
-
Notifications
You must be signed in to change notification settings - Fork 730
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
chore: scroll-related UX adjustments in the Unleash AI chat #8478
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 |
---|---|---|
|
@@ -20,6 +20,10 @@ | |
content: `I'm sorry, I'm having trouble understanding you right now. I've reported the issue to the team. Please try again later.`, | ||
} as const; | ||
|
||
type ScrollOptions = ScrollIntoViewOptions & { | ||
onlyIfAtEnd?: boolean; | ||
}; | ||
|
||
const StyledAIIconContainer = styled('div')(({ theme }) => ({ | ||
position: 'fixed', | ||
bottom: 20, | ||
|
@@ -88,22 +92,45 @@ | |
|
||
const [messages, setMessages] = useState<ChatMessage[]>([]); | ||
|
||
const isAtEndRef = useRef(true); | ||
const chatEndRef = useRef<HTMLDivElement | null>(null); | ||
|
||
const scrollToEnd = (options?: ScrollIntoViewOptions) => { | ||
const scrollToEnd = (options?: ScrollOptions) => { | ||
if (chatEndRef.current) { | ||
chatEndRef.current.scrollIntoView(options); | ||
const shouldScroll = !options?.onlyIfAtEnd || isAtEndRef.current; | ||
|
||
if (shouldScroll) { | ||
chatEndRef.current.scrollIntoView(options); | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
scrollToEnd({ behavior: 'smooth' }); | ||
}, [messages]); | ||
|
||
useEffect(() => { | ||
scrollToEnd(); | ||
|
||
const intersectionObserver = new IntersectionObserver( | ||
Check failure on line 111 in frontend/src/component/ai/AIChat.tsx GitHub Actions / buildsrc/component/changeRequest/ChangeRequest.test.tsx > add flag change to pending change request
|
||
([entry]) => { | ||
isAtEndRef.current = entry.isIntersecting; | ||
}, | ||
{ threshold: 1.0 }, | ||
); | ||
|
||
const target = chatEndRef.current; | ||
if (target) { | ||
intersectionObserver.observe(target); | ||
} | ||
|
||
return () => { | ||
if (target) { | ||
intersectionObserver.unobserve(target); | ||
} | ||
}; | ||
}, [open]); | ||
|
||
useEffect(() => { | ||
scrollToEnd({ behavior: 'smooth', onlyIfAtEnd: true }); | ||
}, [messages]); | ||
|
||
const onSend = async (content: string) => { | ||
if (!content.trim() || loading) return; | ||
|
||
|
@@ -153,7 +180,7 @@ | |
minSize={{ width: '270px', height: '200px' }} | ||
maxSize={{ width: '90vw', height: '90vh' }} | ||
defaultSize={{ width: '320px', height: '450px' }} | ||
onResize={scrollToEnd} | ||
onResize={() => scrollToEnd({ onlyIfAtEnd: true })} | ||
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. Should we always scroll to end when resizing? I mean if you scroll up on a conversation and then resize... 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. That's why we have |
||
> | ||
<StyledChat> | ||
<AIChatHeader | ||
|
@@ -176,7 +203,13 @@ | |
)} | ||
<div ref={chatEndRef} /> | ||
</StyledChatContent> | ||
<AIChatInput onSend={onSend} loading={loading} /> | ||
<AIChatInput | ||
onSend={onSend} | ||
loading={loading} | ||
onHeightChange={() => | ||
scrollToEnd({ onlyIfAtEnd: true }) | ||
} | ||
/> | ||
</StyledChat> | ||
</StyledResizable> | ||
</StyledAIChatContainer> | ||
|
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.
Should this be like this? target will get a reference to the target and the function will just keep it referenced
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.
Good point! I simplified this to always use the ref: 8d473e1