diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index b2d5afbd..eae129b6 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -202,6 +202,15 @@ export function RoomTimeline({ eventId, roomInputRef, editor, timeline }: RoomTi console.log('mark as read.'); }, []); + const newDividerRef = useRef(null); + // if new divider exist always start form here. + useEffect(() => { + const newDividerEl = newDividerRef.current; + if (newDividerEl) { + scrollToElement(newDividerEl, { align: 'center', behavior: 'instant' }); + } + }, [scrollToElement]); + const [atBottom, atBottomAnchorRef] = useAtBottomToggle( getScrollElement, messageBatch.next.end ?? false, @@ -259,12 +268,12 @@ export function RoomTimeline({ eventId, roomInputRef, editor, timeline }: RoomTi const stickToBottom = useCallback(() => { if (!atBottom) return; - // setTimeout(() => { - // const scrollEl = getScrollElement(); - // if (scrollEl) { - // scrollToBottom(scrollEl, 'smooth'); - // } - // }, 100); + setTimeout(() => { + const scrollEl = getScrollElement(); + if (scrollEl) { + scrollToBottom(scrollEl, 'smooth'); + } + }, 100); }, [getScrollElement, atBottom]); useObservableValueListener(timeline.outbox.senders, stickToBottom); @@ -489,6 +498,7 @@ export function RoomTimeline({ eventId, roomInputRef, editor, timeline }: RoomTi messageSpacing={messageSpacing} ts={dayDivider ? event.origin_server_ts : undefined} unread={newDivider} + ref={newDivider ? newDividerRef : undefined} /> ) : null; diff --git a/src/app/features/room/components/TimelineDivider.tsx b/src/app/features/room/components/TimelineDivider.tsx index 2ff1008e..7f17fd88 100644 --- a/src/app/features/room/components/TimelineDivider.tsx +++ b/src/app/features/room/components/TimelineDivider.tsx @@ -1,4 +1,4 @@ -import React, { ReactNode } from 'react'; +import React, { forwardRef, ReactNode } from 'react'; import { as, Badge, Box, ContainerColor, Line, Text } from 'folds'; import classNames from 'classnames'; import { DividerTime, MessageBase } from '../../../components/message'; @@ -30,9 +30,9 @@ type ComboDividerProps = { replyCount?: number; unread?: boolean; }; -export function ComboDivider({ messageSpacing, ts, replyCount, unread }: ComboDividerProps) { - return ( - +export const ComboDivider = forwardRef( + ({ messageSpacing, ts, replyCount, unread }, ref) => ( + - ); -} + ) +); diff --git a/src/app/features/room/hooks/useTimeline.ts b/src/app/features/room/hooks/useTimeline.ts index a9a63d1d..9941bcae 100644 --- a/src/app/features/room/hooks/useTimeline.ts +++ b/src/app/features/room/hooks/useTimeline.ts @@ -15,7 +15,7 @@ import { Editor } from 'slate'; import { ReactEditor } from 'slate-react'; import { MessageEventWithoutRoomId } from 'neurone/lib/api/types'; import { scrollToBottom } from '../../../utils/dom'; -import { MessageBatch } from '../../../../client/room/messages'; +import { MessageBatch, RangeInfo } from '../../../../client/room/messages'; import { getResizeObserverEntry, useResizeObserver } from '../../../hooks/useResizeObserver'; import { getIntersectionObserverEntry, @@ -50,18 +50,19 @@ export const useTimelineView = (): TimelineView => { }; export const useTimelinePrefetch = (timeline: TimelineView, limit: number) => { - useEffect( - () => - timeline.range.info.subscribe((info) => { - if (info.start <= limit) { - requestAnimationFrame(() => timeline.paginateBackward(limit)); - } - if (info.end >= Math.max(timeline.batch.read.size - limit, 0)) { - requestAnimationFrame(() => timeline.paginateForward(limit)); - } - }), - [timeline, limit] - ); + useEffect(() => { + const handleInfo = (info: RangeInfo) => { + if (info.start <= limit) { + requestAnimationFrame(() => timeline.paginateBackward(limit)); + } + if (info.end >= Math.max(timeline.batch.read.size - limit, 0)) { + requestAnimationFrame(() => timeline.paginateForward(limit)); + } + }; + + handleInfo(timeline.range.info.read); + return timeline.range.info.subscribe(handleInfo); + }, [timeline, limit]); }; export const useTimelinePaginate = (timeline: TimelineView, limit: number) => diff --git a/src/app/features/room/thread-view/ThreadView.tsx b/src/app/features/room/thread-view/ThreadView.tsx index 15d3e538..69c41860 100644 --- a/src/app/features/room/thread-view/ThreadView.tsx +++ b/src/app/features/room/thread-view/ThreadView.tsx @@ -75,7 +75,7 @@ export function ThreadView({ threadTimeline, threadFocusRef, requestClose }: Thr - +
; }; const PAGINATION_LIMIT = 80; -export function TimelineView({ threadTimeline }: TimelineViewProps) { +export function TimelineView({ threadTimeline, threadInputRef }: TimelineViewProps) { const ct = useClient(); const roomId = useRoom(); const encryptedRooms = useObservableValue(ct.encryptedRooms); const editor = useEditor(); - const roomInputRef = useRef(null); const [hideActivity] = useSetting(settingsAtom, 'hideActivity'); const [messageLayout] = useSetting(settingsAtom, 'messageLayout'); @@ -196,7 +204,7 @@ export function TimelineView({ threadTimeline }: TimelineViewProps) { useEditorResizeStickBottom( getScrollElement, - useCallback(() => roomInputRef?.current ?? null, [roomInputRef]), + useCallback(() => threadInputRef?.current ?? null, [threadInputRef]), atBottom );