From 200701b4ac98697c07db04cb36ee067d470d752b Mon Sep 17 00:00:00 2001 From: AAA <35992542+TNTcraftHIM@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:50:26 -0400 Subject: [PATCH] fix(chat): start unread divider at another person's message --- .../channel/components/ChannelConversation.tsx | 6 +++--- src/client/channel/presentation.ts | 5 +++++ tests/channel-presentation.test.ts | 13 ++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/client/channel/components/ChannelConversation.tsx b/src/client/channel/components/ChannelConversation.tsx index 4c20e5af0..0156f81f9 100644 --- a/src/client/channel/components/ChannelConversation.tsx +++ b/src/client/channel/components/ChannelConversation.tsx @@ -6,7 +6,7 @@ import { motionDuration } from "../../ui/animation"; import { useCopy } from "../../ui/copy"; import { MAX_MESSAGE_CODE_POINTS, type ChannelMessage, type ChannelUser, type ChannelUserSummary } from "../protocol"; import { ChannelAction } from "./ChannelAction"; -import { messageAuthorName } from "../presentation"; +import { firstUnreadMessageId, messageAuthorName } from "../presentation"; import { ChannelAttachment } from "./ChannelAttachment"; import { ChannelMessageContent } from "./ChannelMessageContent"; import { type AttachmentLimits, type DraftFile, type ChannelAttachments, fileSize } from "../attachments"; @@ -54,6 +54,7 @@ export function ChannelConversation({ roomId, roomName, roomIcon, entry, message const jumpRequest = useRef(null); const [jumpFailed, setJumpFailed] = useState(false); const [unreadBoundary, setUnreadBoundary] = useState({ room: roomId, after: unreadAfter }); + const unreadMessage = firstUnreadMessageId(messages, unreadBoundary.room === roomId ? unreadBoundary.after : undefined, selfUserId); // Capture a visit's divider before markRead advances the durable personal cursor. useLayoutEffect(() => { setUnreadBoundary(previous => previous.room !== roomId @@ -295,8 +296,7 @@ export function ChannelConversation({ roomId, roomName, roomIcon, entry, message const newDay = !!previousDate && (date.getFullYear() !== previousDate.getFullYear() || date.getMonth() !== previousDate.getMonth() || date.getDate() !== previousDate.getDate()); const elapsed = previous ? message.time - previous.time : 0; - const after = unreadBoundary.room === roomId ? unreadBoundary.after : undefined; - const newMessages = after !== undefined && message.seq > after && (!previous || previous.seq <= after); + const newMessages = message.id === unreadMessage; const grouped = !!previous && !newDay && !newMessages && !message.reply && previous.userId === message.userId && elapsed >= 0 && elapsed < 120_000; if (!grouped) authorMessageId = message.id; const author = profiles.get(message.userId), summary = users.find(user => user.id === message.userId); diff --git a/src/client/channel/presentation.ts b/src/client/channel/presentation.ts index 400e1e9fb..0a28338a1 100644 --- a/src/client/channel/presentation.ts +++ b/src/client/channel/presentation.ts @@ -1,5 +1,10 @@ import type { ChannelMessage, ChannelPublication, ChannelSnapshot, ChannelUser } from "./protocol"; +/** A visit's unread divider starts at another person's message, never our own send. */ +export function firstUnreadMessageId(messages: readonly Pick[], after: number | undefined, selfUserId?: string | null): string | undefined { + return after === undefined ? undefined : messages.find(message => message.seq > after && message.userId !== selfUserId)?.id; +} + export function messageAuthorName(message: ChannelMessage, users: readonly Pick[], fallback: string): string { return message.archivedAuthorName ?? users.find(user => user.id === message.userId)?.name ?? fallback; } diff --git a/tests/channel-presentation.test.ts b/tests/channel-presentation.test.ts index dbda161f2..24edf7b2e 100644 --- a/tests/channel-presentation.test.ts +++ b/tests/channel-presentation.test.ts @@ -1,7 +1,18 @@ import { describe, expect, it } from "vitest"; -import { roomPeople, roomRealtimePeople, screenTitle } from "../src/client/channel/presentation"; +import { firstUnreadMessageId, roomPeople, roomRealtimePeople, screenTitle } from "../src/client/channel/presentation"; import { snapshotSchema } from "../src/client/channel/protocol"; +it("starts unread messages at another person without consuming earlier unread history", () => { + const own = { id: "own", seq: 1, userId: "self" }; + const other = { id: "other", seq: 2, userId: "friend" }; + expect(firstUnreadMessageId([own], 0, "self")).toBeUndefined(); + expect(firstUnreadMessageId([own, other], 0, "self")).toBe("other"); + expect(firstUnreadMessageId([other, { ...own, seq: 3 }], 0, "self")).toBe("other"); + expect(firstUnreadMessageId([own, other], 2, "self")).toBeUndefined(); + expect(firstUnreadMessageId([own, other], undefined, "self")).toBeUndefined(); + expect(firstUnreadMessageId([own, other], 0, "friend")).toBe("own"); +}); + describe("screen titles", () => { it("keeps a custom title across source replacement, then clears to the latest source", () => { const publication = { source: { kind: "screen" as const, name: "First window" }, title: null as string | null };