Skip to content

Commit 1ce1851

Browse files
committed
feat: auto scroll to bottom when submitting a question or finishing an answer
1 parent 48c2618 commit 1ce1851

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/components/ConversationCard/index.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo, useEffect, useState } from 'react'
1+
import { memo, useEffect, useRef, useState } from 'react'
22
import PropTypes from 'prop-types'
33
import Browser from 'webextension-polyfill'
44
import InputBox from '../InputBox'
@@ -34,6 +34,7 @@ function ConversationCard(props) {
3434
const [port, setPort] = useState(() => Browser.runtime.connect())
3535
const [session, setSession] = useState(props.session)
3636
const windowSize = useClampWindowSize([0, Infinity], [250, 1100])
37+
const bodyRef = useRef(null)
3738
/**
3839
* @type {[ConversationItemData[], (conversationItemData: ConversationItemData[]) => void]}
3940
*/
@@ -67,6 +68,10 @@ function ConversationCard(props) {
6768
if (props.onUpdate) props.onUpdate()
6869
})
6970

71+
useEffect(() => {
72+
bodyRef.current.scrollTop = bodyRef.current.scrollHeight
73+
}, [session])
74+
7075
useEffect(() => {
7176
// when the page is responsive, session may accumulate redundant data and needs to be cleared after remounting and before making a new request
7277
if (props.question) {
@@ -215,7 +220,11 @@ function ConversationCard(props) {
215220
</span>
216221
</div>
217222
<hr />
218-
<div className="markdown-body" style={{ maxHeight: windowSize[1] * 0.75 + 'px' }}>
223+
<div
224+
ref={bodyRef}
225+
className="markdown-body"
226+
style={{ maxHeight: windowSize[1] * 0.75 + 'px' }}
227+
>
219228
{conversationItemData.map((data, idx) => (
220229
<ConversationItem
221230
content={data.content}

0 commit comments

Comments
 (0)