diff --git a/nx-dev/feature-ai/src/lib/feed-container.tsx b/nx-dev/feature-ai/src/lib/feed-container.tsx index d4f9c038d0c8b..0e5405e9e7ff2 100644 --- a/nx-dev/feature-ai/src/lib/feed-container.tsx +++ b/nx-dev/feature-ai/src/lib/feed-container.tsx @@ -4,7 +4,6 @@ import { type JSX, RefObject, useEffect, - useMemo, useRef, useState, } from 'react'; @@ -28,8 +27,6 @@ export function FeedContainer(): JSX.Element { const [startedReply, setStartedReply] = useState(false); const [isStopped, setStopped] = useState(false); - const feedContainer: RefObject | undefined = useRef(null); - const { messages, setMessages, @@ -57,14 +54,28 @@ export function FeedContainer(): JSX.Element { }, }); - const hasReply = useMemo(() => messages.length > 0, [messages]); - + /* + * Determine whether we should scroll to the bottom of new messages. + * Scroll if: + * 1. New message has come in (length > previous length) + * 2. User is close to the bottom of the messages + * + * Otherwise, user is probably reading messages, so don't scroll. + */ + const scrollableWrapperRef: RefObject | undefined = + useRef(null); + const currentMessagesLength = useRef(0); useEffect(() => { - if (feedContainer.current) { - const elements = - feedContainer.current.getElementsByClassName('feed-item'); - elements[elements.length - 1].scrollIntoView({ behavior: 'smooth' }); + if (!scrollableWrapperRef.current) return; + const el = scrollableWrapperRef.current; + let shouldScroll = false; + if (messages.length > currentMessagesLength.current) { + currentMessagesLength.current = messages.length; + shouldScroll = true; + } else if (el.scrollTop + el.clientHeight + 50 >= el.scrollHeight) { + shouldScroll = true; } + if (shouldScroll) el.scrollTo(0, el.scrollHeight); }, [messages, isLoading]); const handleSubmit = (event: FormEvent) => { @@ -100,6 +111,7 @@ export function FeedContainer(): JSX.Element { <> {/*WRAPPER*/}
{/*MAIN CONTENT*/} -
+
0} showRegenerateCta={isStopped} />
diff --git a/nx-dev/nx-dev/pages/ai-chat/index.tsx b/nx-dev/nx-dev/pages/ai-chat/index.tsx index 6c229d8ffcd48..f951582a71fcd 100644 --- a/nx-dev/nx-dev/pages/ai-chat/index.tsx +++ b/nx-dev/nx-dev/pages/ai-chat/index.tsx @@ -2,6 +2,7 @@ import { FeedContainer } from '@nx/nx-dev/feature-ai'; import { DocumentationHeader } from '@nx/nx-dev/ui-common'; import { NextSeo } from 'next-seo'; import { useNavToggle } from '../../lib/navigation-toggle.effect'; +import { cx } from '@nx/nx-dev/ui-primitives'; export default function AiDocs(): JSX.Element { const { toggleNav, navIsOpen } = useNavToggle(); @@ -21,7 +22,14 @@ export default function AiDocs(): JSX.Element { maxVideoPreview: -1, }} /> -
+