Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(community): reliable message-list auto-scroll on channel switch + self-send

Gener5c7e448f26db
diff --git a/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx b/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx
index 1c8556c84..b6aeb3150 100644
--- a/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx
+++ b/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx
@@ -115,7 +115,8 @@ function ChannelView() {
// Frozen-once snapshot of the viewer's read pointer for this channel — the
// anchor for the "New" divider AND the mount-time initial scroll target.
// The value NEVER changes during the mount even as the watermark advances.
- const { snapshot: readSnapshot } = useChannelReadStateSnapshot(channelId)
+ const { snapshot: readSnapshot, isFetching: readSnapshotFetching } =
+ useChannelReadStateSnapshot(channelId)
// The message immediately after the viewer's `lastReadMessageId` inside the
// current window. id-first: the invariant guarantees
@@ -506,6 +507,7 @@ function ChannelView() {
/>
<main className="flex min-h-0 min-w-0 flex-1 flex-col">
<MessageList
+ key={channelId}
channel={channelName}
messages={messages}
loading={messagesLoading}
@@ -517,6 +519,7 @@ function ChannelView() {
resolveUserName={resolveUserName}
scrollToMessageId={scrollToMessageId}
hero={opener}
+ viewerUserId={currentUser.id}
/>
<Composer
channel={channelName}
@@ -606,6 +609,9 @@ function ChannelView() {
/>
<main className="flex min-h-0 min-w-0 flex-1 flex-col">
<MessageList
+ // Remount per channel so mount-time initial-scroll fires afresh
+ // and internal refs (didInitialScrollRef, lastTailIdRef) reset.
+ key={channelId}
channel={channelName}
messages={messages}
loading={messagesLoading}
@@ -618,6 +624,11 @@ function ChannelView() {
resolveUserName={resolveUserName}
scrollToMessageId={scrollToMessageId}
onScrollRoot={setScrollRootEl}
+ viewerUserId={currentUser.id}
+ // Delay initial scroll until the read-state snapshot resolves —
+ // otherwise the effect fires with newDividerBefore still stale
+ // and snaps to bottom before the anchor is known.
+ initialScrollReady={!readSnapshotFetching}
/>
<Composer
channel={channelName}
diff --git a/src/web/src/app/community/me/[dmId]/page.tsx b/src/web/src/app/community/me/[dmId]/page.tsx
index 052014611..9d136403d 100644
--- a/src/web/src/app/community/me/[dmId]/page.tsx
+++ b/src/web/src/app/community/me/[dmId]/page.tsx
@@ -199,6 +199,7 @@ function DmView() {
<DmHeader dm={dm} onBack={bp === "mobile" ? goBack : undefined} />
<main className="flex min-h-0 flex-1 flex-col">
<MessageList
+ key={dmId}
channel={dm.name}
messages={messages}
loading={messagesLoading}
@@ -210,6 +211,7 @@ function DmView() {
onRetry={dmBlocked ? undefined : messageActions.onRetry}
onOpenProfile={openProfile}
resolveUserName={resolveUserName}
+ viewerUserId={currentUser.id}
hero={
<>
<div className="relative mb-3 w-fit"><Avatar label={dm.avatar} size={64} /></div>
diff --git a/src/web/src/components/community/dividers.tsx b/src/web/src/components/community/dividers.tsx
index 1048e5bc8..5e1e36420 100644
--- a/src/web/src/components/community/dividers.tsx
+++ b/src/web/src/components/community/dividers.tsx
@@ -11,11 +11,13 @@ export function DateDivider({ label }: { label: string }) {
}
export function NewDivider() {
+ // The row's height tracks the separator line only; the "New" pill is taken
+ // out of flow so its taller box doesn't push messages apart.
return (
- <div className="my-1 flex items-center gap-2">
+ <div className="relative my-1 flex items-center">
<Separator className="flex-1 bg-destructive/60" />
<span
- className="rounded-sm bg-destructive px-1.5 py-0.5 text-xs font-semibold uppercase tracking-wide text-white"
+ className="pointer-events-none absolute right-0 top-1/2 -translate-y-1/2 rounded-sm bg-destructive px-1.5 py-0.5 text-xs font-semibold uppercase tracking-wide text-white"
style={{ WebkitTextStroke: "0.4px currentColor" }}
>
New
diff --git a/src/web/src/components/community/message-list.tsx b/src/web/src/components/community/message-list.tsx
index c2d8f4475..549300454 100644
--- a/src/web/src/components/community/message-list.tsx
+++ b/src/web/src/components/community/message-list.tsx
@@ -1,6 +1,6 @@
"use client"
-import { useCallback, useEffect, useMemo, useRef, useState } from "react"
+import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"
import { ArrowDown } from "lucide-react"
import { DateDivider, NewDivider } from "./dividers"
import { Message } from "./message"
@@ -17,7 +17,7 @@ export function MessageList({
channel, messages, loading, pinnedIds, newDividerBefore, typingUsers, onOpenThread, onOpenProfile,
onToggleReaction, onReact,
onReply, onPin, onCreateThread, onCopy, onRetry, onPreviewImage, onDownloadFile,
- resolveUserName, scrollToMessageId, hero, onScrollRoot,
+ resolveUserName, scrollToMessageId, hero, onScrollRoot, viewerUserId, initialScrollReady = true,
}: {
channel: string
messages: Msg[]
@@ -46,6 +46,16 @@ export function MessageList({
* page's default viewport.
*/
onScrollRoot?: (el: HTMLDivElement | null) => void
+ // Viewer id — enables "scroll to bottom when the viewer sends a message".
+ // Without this the auto-follow only fires at mount time; incoming peer
+ // messages never pull the view.
+ viewerUserId?: string
+ // Gate for the mount-time initial scroll. Owners that need to wait for
+ // async NEW-divider anchor data (`useChannelReadStateSnapshot`) pass
+ // `false` until the snapshot resolves; otherwise the effect fires with a
+ // stale `newDividerBefore = undefined` and snaps to bottom before the
+ // anchor is known.
+ initialScrollReady?: boolean
}) {
const [jumped, setJumped] = useState<string | null>(null)
const scrollRef = useRef<HTMLDivElement>(null)
@@ -74,26 +84,59 @@ export function MessageList({
// 2. No NEW divider → snap to the bottom.
// Fires once per mount; deliberately no near-bottom heuristic. If the
// user has scrolled up, incoming messages do NOT pull the view back —
- // the floating "↓ N" button below is how they return.
- useEffect(() => {
+ // the floating "↓ N" button below is how they return, and the sibling
+ // "self-send" effect handles the composer path.
+ //
+ // Owners key `<MessageList>` on channelId/dmId, so channel switches
+ // remount the component and reset this ref — no explicit reset logic
+ // needed here.
+ useLayoutEffect(() => {
const el = scrollRef.current
if (!el) return
if (didInitialScrollRef.current) return
if (messages.length === 0) return
+ // Wait for the owner's async anchor (e.g. useChannelReadStateSnapshot)
+ // — running the effect before newDividerBefore is known silently snaps
+ // to the bottom and burns the one-shot gate.
+ if (!initialScrollReady) return
if (newDividerBefore) {
const target = el.querySelector<HTMLElement>(
`[data-msg-id="${cssEscape(newDividerBefore)}"]`,
)
if (target) {
- target.scrollIntoView({ block: "center" })
+ target.scrollIntoView({ block: "center", behavior: "smooth" })
didInitialScrollRef.current = true
return
}
}
- el.scrollTop = el.scrollHeight
+ el.scrollTo({ top: el.scrollHeight, behavior: "smooth" })
didInitialScrollRef.current = true
- }, [messages, newDividerBefore])
+ }, [messages, newDividerBefore, initialScrollReady])
+
+ // Rule #3: when the viewer sends a message, snap to the bottom.
+ // Tracks the tail message id across renders and only fires when the tail
+ // moves AND the new tail is authored by the viewer. `fetchOlder` prepends
+ // older rows and leaves the tail id unchanged, so paging up never triggers
+ // a jump. Peer sends move the tail but with a different authorId — those
+ // stay on the "↓ N" pill path.
+ const lastTailIdRef = useRef<string | null>(null)
+ useLayoutEffect(() => {
+ const el = scrollRef.current
+ if (!el) return
+ if (messages.length === 0) {
+ lastTailIdRef.current = null
+ return
+ }
+ const tail = messages[messages.length - 1]
+ const prev = lastTailIdRef.current
+ lastTailIdRef.current = tail.id
+ if (prev === null) return
+ if (prev === tail.id) return
+ if (!viewerUserId) return
+ if (tail.authorId !== viewerUserId) return
+ el.scrollTo({ top: el.scrollHeight, behavior: "smooth" })
+ }, [messages, viewerUserId])
// Live count of messages sitting below the viewport. Recomputed on scroll,
// on messages change, and via a ResizeObserver so appended rows update the