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

fix: fix chat autoscrolling if position of previous chat not at the bottom (Issue #501) #581

58 changes: 34 additions & 24 deletions src/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export const Chat = memo(() => {
const nextMessageBoxRef = useRef<HTMLDivElement | null>(null);
const [inputHeight, setInputHeight] = useState<number>(142);
const [notAllowedType, setNotAllowedType] = useState<EntityType | null>(null);
const isSentRef = useRef(false);
const disableAutoScrollTimeoutRef = useRef<ReturnType<typeof setTimeout>>();

const showReplayControls = useMemo(() => {
Expand All @@ -117,30 +118,6 @@ export const Chat = memo(() => {
(conv) => conv.messages.length > 0,
);

useEffect(() => {
setIsShowChatSettings(false);

if (selectedConversations.length > 0) {
const mergedMessages: MergedMessages[] = [];
for (let i = 0; i < selectedConversations[0].messages.length; i++) {
if (selectedConversations[0].messages[i].role === Role.System) continue;

mergedMessages.push(
selectedConversations.map((conv) => [
conv,
conv.messages[i] || { role: Role.Assistant, content: '' },
i,
]),
);
}
setMergedMessages([...mergedMessages]);
}

if (selectedConversations.some((conv) => conv.messages.length === 0)) {
setShowScrollDownButton(false);
}
}, [selectedConversations]);

useEffect(() => {
const modelIds = models.map((model) => model.id);
const isNotAllowedModel =
Expand Down Expand Up @@ -254,6 +231,7 @@ export const Chat = memo(() => {
threshold: 0.1,
},
);

const messagesEndElement = messagesEndRef.current;
if (messagesEndElement) {
observer.observe(messagesEndElement);
Expand All @@ -265,6 +243,35 @@ export const Chat = memo(() => {
};
}, [messagesEndRef]);

useEffect(() => {
setIsShowChatSettings(false);

if (selectedConversations.length > 0) {
if (isSentRef.current) {
handleScroll();
isSentRef.current = false;
}

const mergedMessages: MergedMessages[] = [];
for (let i = 0; i < selectedConversations[0].messages.length; i++) {
if (selectedConversations[0].messages[i].role === Role.System) continue;

mergedMessages.push(
selectedConversations.map((conv) => [
conv,
conv.messages[i] || { role: Role.Assistant, content: '' },
i,
]),
);
}
setMergedMessages([...mergedMessages]);
}

if (selectedConversations.some((conv) => conv.messages.length === 0)) {
setShowScrollDownButton(false);
}
}, [handleScroll, selectedConversations]);

const handleClearConversation = useCallback(
(conversation: Conversation) => {
if (conversation) {
Expand Down Expand Up @@ -436,6 +443,7 @@ export const Chat = memo(() => {
activeReplayIndex: 0,
}),
);
isSentRef.current = true;
},
[dispatch, selectedConversations],
);
Expand All @@ -453,6 +461,7 @@ export const Chat = memo(() => {
activeReplayIndex: 0,
}),
);
isSentRef.current = true;
}, [dispatch, selectedConversations]);

const onEditMessage = useCallback(
Expand All @@ -466,6 +475,7 @@ export const Chat = memo(() => {
activeReplayIndex: 0,
}),
);
isSentRef.current = true;
},
[dispatch, selectedConversations],
);
Expand Down
Loading