Use key prop to re-render virtuoso when task changes

This commit is contained in:
Saoud Rizwan
2024-10-02 21:19:10 -04:00
parent 0c290b4108
commit 1a5e9759d8

View File

@@ -51,7 +51,6 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
const [showScrollToBottom, setShowScrollToBottom] = useState(false)
const isAtBottomRef = useRef(false)
const taskMsgTsRef = useRef<number | undefined>(undefined)
const [didScrollUp, setDidScrollUp] = useState(false)
const lastScrollTopRef = useRef(0)
const [didClickScrollToBottom, setDidClickScrollToBottom] = useState(false)
@@ -484,18 +483,6 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
[visibleMessages, expandedRows, scrollToBottomAuto]
)
// scroll to bottom if task changes
// (this gets called when messages changes, so we use ref to ts to detect new task)
useEffect(() => {
if (task && task.ts !== taskMsgTsRef.current) {
taskMsgTsRef.current = task.ts
const timer = setTimeout(() => {
scrollToBottomSmooth()
}, 50)
return () => clearTimeout(timer)
}
}, [task, scrollToBottomSmooth, visibleMessages.length])
const handleRowHeightChange = useCallback(() => {
if (isAtBottomRef.current || didClickScrollToBottom || !didScrollUp) {
scrollToBottomSmooth()
@@ -590,6 +577,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
<>
<Virtuoso
ref={virtuosoRef}
key={task.ts} // trick to make sure virtuoso re-renders when task changes, and we use initialTopMostItemIndex to start at the bottom
className="scrollable"
style={{
flexGrow: 1,
@@ -637,6 +625,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
}}
atBottomThreshold={10} // anything lower causes issues with followOutput
onScroll={handleScroll}
initialTopMostItemIndex={visibleMessages.length - 1}
/>
{showScrollToBottom ? (
<div