Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): stop replies re-ranking the comment thread you are reading (#80100)

Shy Altere92ce4e2490d
diff --git a/products/desktop/packages/ui/src/features/canvas/components/TaskCommentsList.test.tsx b/products/desktop/packages/ui/src/features/canvas/components/TaskCommentsList.test.tsx
index 5bce35441dce..64cafa55db79 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/TaskCommentsList.test.tsx
+++ b/products/desktop/packages/ui/src/features/canvas/components/TaskCommentsList.test.tsx
@@ -762,6 +762,14 @@ describe("TaskCommentsList", () => {
context: { anchor: { kind: "document" } },
}),
);
+ // The list is ordered by when a thread started, so the new one lands at the
+ // top, away from the composer it was written in.
+ expect(
+ useCommentNavigationStore.getState().focusByTask["task-1"],
+ ).toMatchObject({
+ target: { scope: "task", itemId: "task-1" },
+ threadId: "created-comment",
+ });
});
it("polls at most 20 artifact comment targets plus the task", () => {
diff --git a/products/desktop/packages/ui/src/features/canvas/components/TaskCommentsList.tsx b/products/desktop/packages/ui/src/features/canvas/components/TaskCommentsList.tsx
index 7f50a2ebe03f..28968de5f4de 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/TaskCommentsList.tsx
+++ b/products/desktop/packages/ui/src/features/canvas/components/TaskCommentsList.tsx
@@ -34,7 +34,7 @@ import {
taskCommentTarget,
} from "@posthog/ui/features/canvas/components/taskArtifactRows";
import {
- byNewestActivity,
+ byNewestThread,
prCommentThreads,
resourceCommentThreads,
type SourceKind,
@@ -405,7 +405,6 @@ export function TaskCommentsList({
const taskTarget = useMemo(() => taskCommentTarget(task.id), [task.id]);
const composerTarget = onlySource?.target ?? taskTarget;
- const composerSourceKey = commentTargetKey(composerTarget);
const createComment = useCreateComment(composerTarget, task.id);
const threads = useMemo(() => {
@@ -423,7 +422,7 @@ export function TaskCommentsList({
conversationByUrl.get(prUrl) ?? [],
),
);
- return [...resourceThreads, ...prThreads].sort(byNewestActivity);
+ return [...resourceThreads, ...prThreads].sort(byNewestThread);
}, [
commentsQuery.data,
sources,
@@ -732,7 +731,7 @@ export function TaskCommentsList({
value={draft}
onValueChange={setDraft}
onSubmit={async (content, mentions) => {
- await createComment.mutateAsync({
+ const created = await createComment.mutateAsync({
content,
context: {
anchor: { kind: "document" },
@@ -741,15 +740,10 @@ export function TaskCommentsList({
mentions,
});
setDraft("");
- // Show the thread that was just opened: open state, and a source
- // filter that isn't hiding the task's own comments.
- setStateFilter("open");
- if (
- sourceFilter !== ALL_SOURCES &&
- sourceFilter !== composerSourceKey
- ) {
- setSourceFilter(ALL_SOURCES);
- }
+ // Show the thread that was just opened. The list is ordered by when
+ // a thread started, so a new one lands at the top, away from the
+ // composer — focusing it clears the filters hiding it and scrolls.
+ requestCommentFocus(task.id, composerTarget, created.id);
}}
members={members}
placeholder={`Comment on this ${onlySource ? "canvas" : "task"}… Type @ to mention someone`}
diff --git a/products/desktop/packages/ui/src/features/canvas/components/taskCommentThreads.test.ts b/products/desktop/packages/ui/src/features/canvas/components/taskCommentThreads.test.ts
index 08be7f9b7501..85a852ea3ce1 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/taskCommentThreads.test.ts
+++ b/products/desktop/packages/ui/src/features/canvas/components/taskCommentThreads.test.ts
@@ -3,7 +3,7 @@ import type { PrConversationComment, PrReviewThread } from "@posthog/shared";
import { describe, expect, it } from "vitest";
import type { CommentSource } from "./taskArtifactRows";
import {
- byNewestActivity,
+ byNewestThread,
prCommentThreads,
resourceCommentThreads,
threadSourceOptions,
@@ -59,6 +59,9 @@ describe("resourceCommentThreads", () => {
"root",
"reply",
]);
+ // The reply is newer, but ordering follows the root: replying must not move
+ // the thread out from under whoever is reading it.
+ expect(threads[0].startedAt).toBe("2024-01-01T00:00:00Z");
});
// A resolve/reopen reply is thread state, not something anyone said.
@@ -82,7 +85,7 @@ describe("resourceCommentThreads", () => {
expect(threads[0].resolved).toBe(true);
expect(threads[0].entries).toHaveLength(1);
- expect(threads[0].lastActivityAt).toBe("2024-01-01T00:00:00Z");
+ expect(threads[0].startedAt).toBe("2024-01-01T00:00:00Z");
});
});
@@ -148,7 +151,8 @@ describe("prCommentThreads", () => {
threadNodeId: "node-1",
filePath: "src/App.tsx",
});
- expect(thread.lastActivityAt).toBe("2024-01-01T00:05:00Z");
+ // The thread's own start, so a reply can't re-rank it in the list.
+ expect(thread.startedAt).toBe("2024-01-01T00:00:00Z");
});
it("makes each conversation comment its own unresolvable thread", () => {
@@ -194,7 +198,7 @@ describe("prCommentThreads", () => {
});
});
-describe("threadSourceOptions / byNewestActivity", () => {
+describe("threadSourceOptions / byNewestThread", () => {
it("lists each source once and sorts newest first", () => {
const threads = [
...resourceCommentThreads(
@@ -216,7 +220,7 @@ describe("threadSourceOptions / byNewestActivity", () => {
},
],
),
- ].sort(byNewestActivity);
+ ].sort(byNewestThread);
expect(threads[0].entries[0].body).toBe("new");
// Options follow list order, which is newest-first, and carry the kind so
@@ -251,7 +255,7 @@ describe("threadSourceOptions / byNewestActivity", () => {
],
[fileSource, taskSource],
),
- ].sort(byNewestActivity);
+ ].sort(byNewestThread);
expect(threadSourceOptions(threads).map((option) => option.kind)).toEqual([
"task",
diff --git a/products/desktop/packages/ui/src/features/canvas/components/taskCommentThreads.ts b/products/desktop/packages/ui/src/features/canvas/components/taskCommentThreads.ts
index eafab64b8686..27972a40bd92 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/taskCommentThreads.ts
+++ b/products/desktop/packages/ui/src/features/canvas/components/taskCommentThreads.ts
@@ -53,8 +53,9 @@ export type TaskCommentThread = {
sourceKind: "file" | "canvas" | "task" | "pr";
entries: CommentEntry[];
resolved: boolean;
- /** Newest comment in the thread, for ordering the list. */
- lastActivityAt: string;
+ /** When the thread was opened, for ordering the list. Ordering on the newest
+ * reply instead would re-rank a thread the moment someone replies to it. */
+ startedAt: string;
origin: ThreadOrigin;
};
@@ -103,8 +104,7 @@ export function resourceCommentThreads(
sourceKind: source.kind,
entries: [thread.root, ...visibleReplies].map(resourceEntry),
resolved: thread.resolved,
- lastActivityAt:
- visibleReplies.at(-1)?.created_at ?? thread.root.created_at,
+ startedAt: thread.root.created_at,
origin: { kind: "resource", source, root: thread.root },
},
];
@@ -145,7 +145,8 @@ export function prCommentThreads(
format: "markdown" as const,
})),
resolved: thread.isResolved,
- lastActivityAt: humanComments.at(-1)?.created_at ?? root.created_at,
+ // The visible root: a bot may have opened the thread, and bots are filtered out.
+ startedAt: humanComments[0].created_at,
origin: {
kind: "pr-review" as const,
prUrl,
@@ -178,7 +179,7 @@ export function prCommentThreads(
},
],
resolved: false,
- lastActivityAt: comment.createdAt,
+ startedAt: comment.createdAt,
origin: { kind: "pr-conversation", prUrl, url: comment.url },
});
}
@@ -186,11 +187,12 @@ export function prCommentThreads(
return threads;
}
-export function byNewestActivity(
+export function byNewestThread(
a: TaskCommentThread,
b: TaskCommentThread,
): number {
- return b.lastActivityAt.localeCompare(a.lastActivityAt);
+ // Total, so threads opened in the same second can't swap places between renders.
+ return b.startedAt.localeCompare(a.startedAt) || a.id.localeCompare(b.id);
}
export type SourceKind = TaskCommentThread["sourceKind"];