fix: auto scroll to the lastest message

This commit is contained in:
RugerMc 2023-03-31 00:15:50 +08:00
parent 8b4db412d8
commit a1873eaa9d
2 changed files with 8 additions and 15 deletions

View File

@ -218,7 +218,7 @@
flex: 1; flex: 1;
overflow: auto; overflow: auto;
padding: 20px; padding: 20px;
margin-bottom: 100px; margin-bottom: 160px;
} }
.chat-body-title { .chat-body-title {

View File

@ -282,10 +282,9 @@ export function Chat(props: {
}; };
// for auto-scroll // for auto-scroll
const latestMessageRef = useRef<HTMLDivElement>(null); const chatBodyRef = useRef<HTMLDivElement>(null);
// wont scroll while hovering messages // wont scroll while hovering messages
const [autoScroll, setAutoScroll] = useState(false); const [autoScroll, setAutoScroll] = useState(true);
// preview messages // preview messages
const messages = (session.messages as RenderMessage[]) const messages = (session.messages as RenderMessage[])
@ -317,15 +316,12 @@ export function Chat(props: {
// auto scroll // auto scroll
useLayoutEffect(() => { useLayoutEffect(() => {
setTimeout(() => { setTimeout(() => {
const dom = latestMessageRef.current; const chatBody = chatBodyRef.current;
if (dom && !isIOS() && autoScroll) { if (chatBody && !isIOS() && autoScroll) {
dom.scrollIntoView({ chatBody.scrollTo(0, chatBody.scrollHeight)
block: "end",
});
} }
}, 500); }, 500);
}); });
return ( return (
<div className={styles.chat} key={session.id}> <div className={styles.chat} key={session.id}>
<div className={styles["window-header"]}> <div className={styles["window-header"]}>
@ -382,10 +378,10 @@ export function Chat(props: {
</div> </div>
</div> </div>
<div className={styles["chat-body"]}> <div ref={chatBodyRef} className={styles["chat-body"]}>
{messages.map((message, i) => { {messages.map((message, i) => {
const isUser = message.role === "user"; const isUser = message.role === "user";
return ( return (
<div <div
key={i} key={i}
@ -454,9 +450,6 @@ export function Chat(props: {
</div> </div>
); );
})} })}
<div ref={latestMessageRef} style={{ opacity: 0, height: "4em" }}>
-
</div>
</div> </div>
<div className={styles["chat-input-panel"]}> <div className={styles["chat-input-panel"]}>