fix(chat): start unread divider at another person's message

This commit is contained in:
AAA committed 2026-11-13 02:50:26 -04:00
1 parent 022c1f0ace
commit 200701b4ac
3 files changed
+20 -4

No files matched your search

@@ -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<object | null>(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);
+5
View File
@@ -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<ChannelMessage, "id" | "seq" | "userId">[], 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<ChannelUser, "id" | "name">[], fallback: string): string {
return message.archivedAuthorName ?? users.find(user => user.id === message.userId)?.name ?? fallback;
}
+12 -1
View File
@@ -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 };