|
1 | 1 | import React, { useEffect } from 'react'; |
2 | 2 | import { throttle } from '../utils/misc'; |
3 | 3 |
|
4 | | -export const scrollToBottom = throttle( |
5 | | - (requiresNearBottom: boolean, delay?: number) => { |
6 | | - const mainScrollElem = document.getElementById('main-scroll'); |
7 | | - if (!mainScrollElem) return; |
8 | | - const spaceToBottom = |
9 | | - mainScrollElem.scrollHeight - |
10 | | - mainScrollElem.scrollTop - |
11 | | - mainScrollElem.clientHeight; |
12 | | - if (!requiresNearBottom || spaceToBottom < 100) { |
13 | | - setTimeout( |
14 | | - () => mainScrollElem.scrollTo({ top: mainScrollElem.scrollHeight }), |
15 | | - delay ?? 80 |
16 | | - ); |
17 | | - } |
18 | | - }, |
19 | | - 80 |
20 | | -); |
| 4 | +export const scrollToBottom = (requiresNearBottom: boolean, delay?: number) => { |
| 5 | + console.log({ requiresNearBottom }); |
| 6 | + const mainScrollElem = document.getElementById('main-scroll'); |
| 7 | + if (!mainScrollElem) return; |
| 8 | + const spaceToBottom = |
| 9 | + mainScrollElem.scrollHeight - |
| 10 | + mainScrollElem.scrollTop - |
| 11 | + mainScrollElem.clientHeight; |
| 12 | + if (!requiresNearBottom || spaceToBottom < 100) { |
| 13 | + setTimeout( |
| 14 | + () => mainScrollElem.scrollTo({ top: mainScrollElem.scrollHeight }), |
| 15 | + delay ?? 80 |
| 16 | + ); |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +const scrollToBottomThrottled = throttle(scrollToBottom, 80); |
21 | 21 |
|
22 | 22 | export function useChatScroll(msgListRef: React.RefObject<HTMLDivElement>) { |
23 | 23 | useEffect(() => { |
24 | 24 | if (!msgListRef.current) return; |
25 | 25 |
|
26 | 26 | const resizeObserver = new ResizeObserver((_) => { |
27 | | - scrollToBottom(true); |
| 27 | + scrollToBottomThrottled(true, 10); |
28 | 28 | }); |
29 | 29 |
|
30 | 30 | resizeObserver.observe(msgListRef.current); |
|
0 commit comments