Skip to content

Commit 3b7a83a

Browse files
committed
attempt at improving & optimizing autoscrollQ
1 parent 7ded5db commit 3b7a83a

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/client/hooks/useChatScroll.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
1-
import { useEffect, useState } from 'react'
1+
import { useEffect, useRef, useState, useTransition } from 'react'
22

33
let isUserDisabled = false
44

55
export const useChatScroll = () => {
66
// Todo: on long conversations this state update is not optimal. Ideally move it down the component tree.
77
const [isAutoScrolling, setIsAutoScrolling] = useState(false)
88

9+
const prevHeight = useRef(0)
10+
11+
const [transitionPending, startTransition] = useTransition()
12+
913
const autoScroll = () => {
10-
if (isUserDisabled) return
14+
if (isUserDisabled || transitionPending) {
15+
console.log('skipped', isUserDisabled, transitionPending)
16+
return
17+
}
1118

12-
window.scrollTo({
13-
top: document.body.scrollHeight,
14-
})
19+
const heightDiff = document.body.scrollHeight - prevHeight.current
20+
21+
if (heightDiff > 0) {
22+
startTransition(() => {
23+
window.scrollTo({
24+
top: document.body.scrollHeight,
25+
behavior: 'instant',
26+
})
27+
prevHeight.current = document.body.scrollHeight
28+
})
29+
}
1530
}
1631

1732
const beginAutoscroll = () => {
18-
isUserDisabled = false
19-
setIsAutoScrolling(true)
20-
window.scrollTo({
21-
top: document.body.scrollHeight,
33+
startTransition(() => {
34+
isUserDisabled = false
35+
setIsAutoScrolling(true)
36+
window.scrollTo({
37+
top: document.body.scrollHeight,
38+
behavior: 'smooth',
39+
})
2240
})
2341
}
2442

src/client/stores/analytics.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ type AnalyticsAction =
1717
const initialFeedbackMetadata: FeedbackMetadata = {}
1818

1919
function analyticsReducer(state: FeedbackMetadata, action: AnalyticsAction): FeedbackMetadata {
20-
console.log('analyticsReducer', action)
2120
switch (action.type) {
2221
case 'SET_ANALYTICS_DATA':
2322
return {

0 commit comments

Comments
 (0)