From db176102786b8261790ef524bfb23dc741a46daf Mon Sep 17 00:00:00 2001 From: 7w1 Date: Wed, 18 Mar 2026 21:53:33 -0500 Subject: [PATCH] Redo ALLLLLL the scrollllllllllll --- src/app/features/room/RoomTimeline.tsx | 96 +++++++++++++---------- src/app/hooks/timeline/useTimelineSync.ts | 8 +- src/app/hooks/useScrollManager.ts | 80 +++++++++---------- 3 files changed, 98 insertions(+), 86 deletions(-) diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 5d946c19..dc42731c 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -62,7 +62,6 @@ import { getEventIdAbsoluteIndex, } from '$utils/timeline'; import { useScrollManager } from '$hooks/useScrollManager'; -import { getResizeObserverEntry, useResizeObserver } from '$hooks/useResizeObserver'; import { useTimelineSync } from '$hooks/timeline/useTimelineSync'; import { useTimelineActions } from '$hooks/timeline/useTimelineActions'; import { useProcessedTimeline } from '$hooks/timeline/useProcessedTimeline'; @@ -155,8 +154,6 @@ export function RoomTimeline({ const [editId, setEditId] = useState(); const [unreadInfo, setUnreadInfo] = useState(() => getRoomUnreadInfo(room, true)); - const unreadInfoRef = useRef(unreadInfo); - unreadInfoRef.current = unreadInfo; const readUptoEventIdRef = useRef(undefined); if (unreadInfo) readUptoEventIdRef.current = unreadInfo.readUptoEventId; @@ -183,7 +180,9 @@ export function RoomTimeline({ const setOpenThread = useSetAtom(openThreadAtom); const scrollRef = useRef(null); - const { isAtBottom, onScroll, scrollToBottom } = useScrollManager(scrollRef); + const messageListRef = useRef(null); + + const { isAtBottom, onScroll, scrollToBottom, sentryRef } = useScrollManager(scrollRef); const timelineSync = useTimelineSync({ room, @@ -197,8 +196,6 @@ export function RoomTimeline({ readUptoEventIdRef, }); - const getScrollElement = useCallback(() => scrollRef.current, []); - const virtualPaginator = useVirtualPaginator({ count: timelineSync.eventsLength, limit: PAGINATION_LIMIT, @@ -214,7 +211,7 @@ export function RoomTimeline({ }, [timelineSync] ), - getScrollElement, + getScrollElement: useCallback(() => scrollRef.current, []), getItemElement: useCallback( (index: number) => (scrollRef.current?.querySelector(`[data-message-item="${index}"]`) as HTMLElement) ?? @@ -224,6 +221,53 @@ export function RoomTimeline({ onEnd: timelineSync.handleTimelinePagination, }); + useEffect(() => { + let timeoutId: ReturnType | undefined; + if (timelineSync.focusItem) { + if (timelineSync.focusItem.scrollTo) { + virtualPaginator.scrollToItem(timelineSync.focusItem.index, { + behavior: reducedMotion ? 'instant' : 'smooth', + align: 'center', + stopInView: true, + }); + timelineSync.setFocusItem((prev) => (prev ? { ...prev, scrollTo: false } : undefined)); + } + timeoutId = setTimeout(() => { + timelineSync.setFocusItem(undefined); + }, 2000); + } + return () => { + if (timeoutId !== undefined) { + clearTimeout(timeoutId); + } + }; + }, [timelineSync.focusItem, virtualPaginator, timelineSync, reducedMotion]); + + useEffect(() => { + if (eventId) return; + + const { readUptoEventId, inLiveTimeline, scrollTo } = unreadInfo ?? {}; + if (readUptoEventId && inLiveTimeline && scrollTo) { + const evtTimeline = getEventTimeline(room, readUptoEventId); + const absoluteIndex = evtTimeline + ? getEventIdAbsoluteIndex( + timelineSync.timeline.linkedTimelines, + evtTimeline, + readUptoEventId + ) + : undefined; + + if (absoluteIndex !== undefined) { + virtualPaginator.scrollToItem(absoluteIndex, { + behavior: 'instant', + align: 'start', + stopInView: true, + }); + setUnreadInfo((prev) => (prev ? { ...prev, scrollTo: false } : prev)); + } + } + }, [room, unreadInfo, timelineSync.timeline.linkedTimelines, virtualPaginator, eventId]); + const actions = useTimelineActions({ room, mx, @@ -246,16 +290,12 @@ export function RoomTimeline({ : undefined; if (typeof absoluteIndex === 'number') { - const scrolled = virtualPaginator.scrollToItem(absoluteIndex, { + virtualPaginator.scrollToItem(absoluteIndex, { behavior: reducedMotion ? 'instant' : 'smooth', align: 'center', stopInView: true, }); - timelineSync.setFocusItem({ - index: absoluteIndex, - scrollTo: !scrolled, - highlight: true, - }); + timelineSync.setFocusItem({ index: absoluteIndex, scrollTo: false, highlight: true }); } else { timelineSync.loadEventTimeline(id); } @@ -350,12 +390,7 @@ export function RoomTimeline({ setOpenThread: actions.setOpenThread, handleOpenReply: actions.handleOpenReply, }, - utils: { - htmlReactParserOptions, - linkifyOpts, - getMemberPowerTag, - parseMemberEvent, - }, + utils: { htmlReactParserOptions, linkifyOpts, getMemberPowerTag, parseMemberEvent }, }); const tryAutoMarkAsRead = useCallback(() => { @@ -390,27 +425,6 @@ export function RoomTimeline({ } }, [isAtBottom, timelineSync.liveTimelineLinked, timelineSync.rangeAtEnd, tryAutoMarkAsRead]); - useResizeObserver( - useMemo(() => { - let mounted = false; - return (entries: ResizeObserverEntry[]) => { - if (!mounted) { - mounted = true; - return; - } - if (!roomInputRef.current) return; - - const editorBaseEntry = getResizeObserverEntry(roomInputRef.current, entries); - if (!editorBaseEntry || !scrollRef.current) return; - - if (isAtBottom) { - scrollToBottom('instant'); - } - }; - }, [isAtBottom, scrollToBottom, roomInputRef]), - useCallback(() => roomInputRef.current, [roomInputRef]) - ); - return ( {unreadInfo?.readUptoEventId && !unreadInfo?.inLiveTimeline && ( @@ -438,6 +452,7 @@ export function RoomTimeline({ @@ -516,6 +531,7 @@ export function RoomTimeline({ )} +
diff --git a/src/app/hooks/timeline/useTimelineSync.ts b/src/app/hooks/timeline/useTimelineSync.ts index 6baa2dd2..0689d05a 100644 --- a/src/app/hooks/timeline/useTimelineSync.ts +++ b/src/app/hooks/timeline/useTimelineSync.ts @@ -565,7 +565,13 @@ export function useTimelineSync({ useEffect(() => { const resetPending = timelineJustResetRef.current; - if ((isAtBottom || resetPending) && liveTimelineLinked && eventsLength > timeline.range.end) { + const isActuallyAtLiveEnd = isAtBottom && atLiveEndRef.current; + + if ( + (isActuallyAtLiveEnd || resetPending) && + liveTimelineLinked && + eventsLength > timeline.range.end + ) { if (resetPending) timelineJustResetRef.current = false; scrollToBottom('instant'); setTimeline((ct) => ({ diff --git a/src/app/hooks/useScrollManager.ts b/src/app/hooks/useScrollManager.ts index 0cf0dcb5..63edfb19 100644 --- a/src/app/hooks/useScrollManager.ts +++ b/src/app/hooks/useScrollManager.ts @@ -1,79 +1,69 @@ -import { useCallback, useRef, useState, useLayoutEffect } from 'react'; -import { scrollToBottom as domScrollToBottom, getScrollInfo } from '$utils/dom'; +import { useCallback, useEffect, useRef, useState } from 'react'; +import { scrollToBottom as domScrollToBottom } from '$utils/dom'; type ScrollBehavior = 'auto' | 'instant' | 'smooth'; export const useScrollManager = (scrollRef: React.RefObject) => { const [isAtBottom, setIsAtBottom] = useState(true); const isAtBottomRef = useRef(true); - const autoScrollRef = useRef(false); + const sentryRef = useRef(null); - const checkAtBottom = useCallback(() => { - const el = scrollRef.current; - if (!el) return; + useEffect(() => { + const sentryEl = sentryRef.current; + const scrollEl = scrollRef.current; - const { top, height, viewHeight } = getScrollInfo(el); - const distanceToBottom = height - top - viewHeight; + let observer: IntersectionObserver | undefined; - const atBottom = distanceToBottom <= 5; + if (sentryEl && scrollEl) { + observer = new IntersectionObserver( + ([entry]) => { + const atBottom = entry.isIntersecting; - if (atBottom !== isAtBottomRef.current) { - isAtBottomRef.current = atBottom; - setIsAtBottom(atBottom); + isAtBottomRef.current = atBottom; + setIsAtBottom(atBottom); + }, + { + root: scrollEl, + rootMargin: '10px 0px 0px 0px', + threshold: 0, + } + ); + + observer.observe(sentryEl); } + + return () => { + if (observer) { + observer.disconnect(); + } + }; }, [scrollRef]); const scrollToBottom = useCallback( (behavior: ScrollBehavior = 'instant') => { const el = scrollRef.current; if (el) { - autoScrollRef.current = true; domScrollToBottom(el, behavior); + isAtBottomRef.current = true; + setIsAtBottom(true); + if (behavior === 'instant') { - setTimeout(() => { + requestAnimationFrame(() => { if (scrollRef.current) domScrollToBottom(scrollRef.current, 'instant'); - }, 50); + }); } } }, [scrollRef] ); - const onScroll = useCallback(() => { - if (autoScrollRef.current) { - autoScrollRef.current = false; - return; - } - checkAtBottom(); - }, [checkAtBottom]); - - useLayoutEffect(() => { - const el = scrollRef.current; - - if (!el) { - return undefined; - } - - const resizeObserver = new ResizeObserver(() => { - if (isAtBottomRef.current) { - scrollToBottom('instant'); - } - }); - - if (el.firstElementChild) { - resizeObserver.observe(el.firstElementChild); - } - - return () => { - resizeObserver.disconnect(); - }; - }, [scrollRef, scrollToBottom]); + const onScroll = useCallback(() => {}, []); return { isAtBottom, onScroll, scrollToBottom, - checkAtBottom, + sentryRef, }; };