Skip to content

Commit

Permalink
Scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed Jun 25, 2024
1 parent 3a0494f commit e39244c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
15 changes: 12 additions & 3 deletions web-app/src/components/ai/chat-list.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Message } from "ai";
import { ChatMessage } from "components/ai/chat-message";
import { useCallback, useEffect, useRef } from "react";

export interface ChatListInterface {
messages: Message[];
Expand All @@ -22,14 +23,21 @@ export const ChatList = ({
handleSaveEdit,
editingIndex,
}: ChatListInterface) => {
if (!messages?.length) {
return null;
}
const bottomRef = useRef<null | HTMLDivElement>(null);
const scrollToBottom = useCallback(() => {
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
}, [bottomRef]);

const lastUserResponse = messages.findLast((message: Message) => {
return message?.name === "basic_assistant";
});

useEffect(() => {
setTimeout(() => {
scrollToBottom();
}, 150);
}, [messages, scrollToBottom]);

return (
<div>
{messages.map((message, index) => (
Expand All @@ -47,6 +55,7 @@ export const ChatList = ({
/>
</div>
))}
<div ref={bottomRef} />
</div>
);
};
21 changes: 6 additions & 15 deletions web-app/src/components/site/indexes/AskIndexes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ const AskIndexes: FC<AskIndexesProps> = ({ sources }) => {
const [input, setInput] = useState<string>("");
const [defaultQuestions, setDefaultQuestions] = useState<string[]>([]);

const bottomRef = useRef<null | HTMLDivElement>(null);

const fetchDefaultQuestions = useCallback(async (): Promise<void> => {
if (!apiReady || !isIndex || !id) return;
try {
Expand Down Expand Up @@ -214,14 +212,6 @@ const AskIndexes: FC<AskIndexesProps> = ({ sources }) => {
session,
]);

const scrollToBottom = useCallback(() => {
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
}, [bottomRef]);

useEffect(() => {
// scrollToBottom();
}, [viewedConversation, isLoading, scrollToBottom]);

const stop = () => {
setIsLoading(false);
// TODO send stop signal to backend.
Expand Down Expand Up @@ -288,12 +278,14 @@ const AskIndexes: FC<AskIndexesProps> = ({ sources }) => {

useEffect(() => {
!viewedConversation && setStreamingMessages([]);
if (!isLocalUpdate.current) {
if (
!isLocalUpdate.current &&
viewedConversation &&
viewedConversation.messages &&
setStreamingMessages(viewedConversation.messages);
viewedConversation.messages
) {
setStreamingMessages(viewedConversation.messages);
isLocalUpdate.current = false;
}
isLocalUpdate.current = false;
}, [viewedConversation]);

useEffect(() => {
Expand Down Expand Up @@ -361,7 +353,6 @@ const AskIndexes: FC<AskIndexesProps> = ({ sources }) => {
handleRegenerate={handleRegenerate}
editingIndex={editingIndex}
/>
<div ref={bottomRef} />
<ChatScrollAnchor trackVisibility={isLoading} />
</>
) : (
Expand Down

0 comments on commit e39244c

Please sign in to comment.