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

chore: scroll-related UX adjustments in the Unleash AI chat #8478

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions frontend/src/component/ai/AIChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / build

src/component/changeRequest/ChangeRequest.test.tsx > add flag change to pending change request

ReferenceError: IntersectionObserver is not defined ❯ src/component/ai/AIChat.tsx:111:38 ❯ commitHookEffectListMount node_modules/react-dom/cjs/react-dom.development.js:23189:26 ❯ commitPassiveMountOnFiber node_modules/react-dom/cjs/react-dom.development.js:24970:11 ❯ commitPassiveMountEffects_complete node_modules/react-dom/cjs/react-dom.development.js:24930:9 ❯ commitPassiveMountEffects_begin node_modules/react-dom/cjs/react-dom.development.js:24917:7 ❯ commitPassiveMountEffects node_modules/react-dom/cjs/react-dom.development.js:24905:3 ❯ flushPassiveEffectsImpl node_modules/react-dom/cjs/react-dom.development.js:27078:3 ❯ flushPassiveEffects node_modules/react-dom/cjs/react-dom.development.js:27023:14 ❯ performSyncWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:26115:3 ❯ flushSyncCallbacks node_modules/react-dom/cjs/react-dom.development.js:12042:22
([entry]) => {
isAtEndRef.current = entry.isIntersecting;
},
{ threshold: 1.0 },
);

const target = chatEndRef.current;
if (target) {
intersectionObserver.observe(target);
}

return () => {
if (target) {
Copy link
Contributor

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

Suggested change
if (target) {
if (chatEndRef.current) {

Copy link
Member Author

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

intersectionObserver.unobserve(target);
}
};
}, [open]);

useEffect(() => {
scrollToEnd({ behavior: 'smooth', onlyIfAtEnd: true });
}, [messages]);

const onSend = async (content: string) => {
if (!content.trim() || loading) return;

Expand Down Expand Up @@ -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 })}
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why we have onlyIfAtEnd: true. This is actually an improvement on: #8456 (comment)

>
<StyledChat>
<AIChatHeader
Expand All @@ -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>
Expand Down
36 changes: 33 additions & 3 deletions frontend/src/component/ai/AIChatInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import {
IconButton,
InputAdornment,
Expand Down Expand Up @@ -32,19 +32,49 @@ const StyledIconButton = styled(IconButton)({
export interface IAIChatInputProps {
onSend: (message: string) => void;
loading: boolean;
onHeightChange?: () => void;
}

export const AIChatInput = ({ onSend, loading }: IAIChatInputProps) => {
export const AIChatInput = ({
onSend,
loading,
onHeightChange,
}: IAIChatInputProps) => {
const [message, setMessage] = useState('');

const inputContainerRef = useRef<HTMLDivElement | null>(null);
const previousHeightRef = useRef<number>(0);

useEffect(() => {
const resizeObserver = new ResizeObserver(([entry]) => {
const newHeight = entry.contentRect.height;

if (newHeight !== previousHeightRef.current) {
previousHeightRef.current = newHeight;
onHeightChange?.();
}
});

const target = inputContainerRef.current;
if (target) {
resizeObserver.observe(target);
}

return () => {
if (target) {
resizeObserver.unobserve(target);
}
};
}, [onHeightChange]);

const send = () => {
if (!message.trim() || loading) return;
onSend(message);
setMessage('');
};

return (
<StyledAIChatInputContainer>
<StyledAIChatInputContainer ref={inputContainerRef}>
<StyledAIChatInput
autoFocus
size='small'
Expand Down
Loading