Skip to content

Commit

Permalink
web/roomview: scroll to bottom on new message
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Oct 9, 2024
1 parent 3f2ca03 commit ca13912
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions web/src/ui/timeline/TimelineView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import { use, useMemo } from "react"
import { use, useEffect, useMemo, useRef } from "react"
import { RoomStateStore } from "../../api/statestore.ts"
import { useNonNullEventAsState } from "../../util/eventdispatcher.ts"
import { ClientContext } from "../ClientContext.ts"
Expand All @@ -31,11 +31,26 @@ const TimelineView = ({ room }: TimelineViewProps) => {
client.loadMoreHistory(room.roomID)
.catch(err => console.error("Failed to load history", err))
}, [client, room.roomID])
return <div className="timeline-view">
const bottomRef = useRef<HTMLDivElement>(null)
const isAtBottom = useRef(true)
const handleScroll = useMemo(() => () => {
if (!bottomRef.current?.parentElement) {
return
}
const timelineView = bottomRef.current.parentElement
isAtBottom.current = timelineView.scrollTop + timelineView.clientHeight + 1 >= timelineView.scrollHeight
}, [])
useEffect(() => {
if (bottomRef.current && isAtBottom.current) {
bottomRef.current.scrollIntoView()
}
}, [timeline])
return <div className="timeline-view" onScroll={handleScroll}>
<button onClick={loadHistory}>Load history</button>
{timeline.map(entry => <TimelineEvent
key={entry.event_rowid} room={room} eventRowID={entry.event_rowid}
/>)}
<div ref={bottomRef}/>
</div>
}

Expand Down

0 comments on commit ca13912

Please sign in to comment.