Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(views): sort timeline entries by created_at on WebSocket append (MUL-2582) (#3139)

lucccf077bc055f716
diff --git a/apps/mobile/data/realtime/issue-ws-updaters.ts b/apps/mobile/data/realtime/issue-ws-updaters.ts
index 3366e54f51..8607e4f23f 100644
--- a/apps/mobile/data/realtime/issue-ws-updaters.ts
+++ b/apps/mobile/data/realtime/issue-ws-updaters.ts
@@ -17,7 +17,7 @@
* Cache shapes (the design contract here):
* - Issue detail: Issue (keyed by detail(wsId, id))
* - Issue timeline: TimelineEntry[] (keyed by timeline(wsId, id))
- * ASC oldest-first; new entries append to the end.
+ * ASC oldest-first; new entries inserted at sorted position.
* - My Issues list: Issue[] (keyed by myList(wsId, scope, filter))
* Multiple list caches per wsId (one per scope/filter combo).
* Patch ALL of them via setQueriesData on myAll(wsId).
@@ -81,7 +81,12 @@ export function appendTimelineEntry(
if (old.some((e) => e.id === entry.id && e.type === entry.type)) {
return old;
}
- return [...old, entry];
+ const next = [...old, entry];
+ next.sort((a, b) => {
+ if (a.created_at !== b.created_at) return a.created_at < b.created_at ? -1 : 1;
+ return a.id < b.id ? -1 : 1;
+ });
+ return next;
},
);
}
diff --git a/packages/core/issues/mutations.ts b/packages/core/issues/mutations.ts
index ac5b0740e8..b17a01a4c8 100644
--- a/packages/core/issues/mutations.ts
+++ b/packages/core/issues/mutations.ts
@@ -32,6 +32,7 @@ import type {
ListIssuesCache,
} from "../types";
import type { TimelineEntry, IssueSubscriber, Reaction } from "../types";
+import { sortTimelineEntriesAsc } from "./timeline-sort";
// ---------------------------------------------------------------------------
// Shared mutation variable types — used by both mutation hooks and
@@ -555,7 +556,7 @@ export function useCreateComment(issueId: string) {
qc.setQueryData<TimelineCache>(issueKeys.timeline(issueId), (old) => {
if (!old) return [entry];
if (old.some((e) => e.id === entry.id)) return old;
- return [...old, entry];
+ return sortTimelineEntriesAsc([...old, entry]);
});
},
// No onSettled invalidate. The `comment:created` WS broadcast keeps
diff --git a/packages/core/issues/timeline-sort.ts b/packages/core/issues/timeline-sort.ts
new file mode 100644
index 0000000000..1fd89eec20
--- /dev/null
+++ b/packages/core/issues/timeline-sort.ts
@@ -0,0 +1,22 @@
+import type { TimelineEntry } from "@multica/core/types";
+
+/**
+ * Stable-ascending sort for flat TimelineEntry[] caches.
+ *
+ * All writers that append to an issue timeline cache MUST pass through
+ * this helper so the display order stays `created_at` ASC (id tie-breaker)
+ * even when WebSocket events and mutation onSuccess callbacks arrive
+ * out of chronological order.
+ *
+ * Observers that mutate in place (map / filter by id) don't need this —
+ * they preserve the existing relative order.
+ */
+export function sortTimelineEntriesAsc(entries: TimelineEntry[]): TimelineEntry[] {
+ entries.sort((a, b) => {
+ if (a.created_at !== b.created_at) {
+ return a.created_at < b.created_at ? -1 : 1;
+ }
+ return a.id < b.id ? -1 : 1;
+ });
+ return entries;
+}
diff --git a/packages/core/package.json b/packages/core/package.json
index 535adb6152..9b6f001abf 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -27,6 +27,7 @@
"./issues": "./issues/index.ts",
"./issues/queries": "./issues/queries.ts",
"./issues/mutations": "./issues/mutations.ts",
+ "./issues/timeline-sort": "./issues/timeline-sort.ts",
"./issues/ws-updaters": "./issues/ws-updaters.ts",
"./issues/config": "./issues/config/index.ts",
"./issues/config/status": "./issues/config/status.ts",
diff --git a/packages/views/issues/hooks/use-issue-timeline.test.tsx b/packages/views/issues/hooks/use-issue-timeline.test.tsx
index 04dcb6ddee..d5a5cbfe76 100644
--- a/packages/views/issues/hooks/use-issue-timeline.test.tsx
+++ b/packages/views/issues/hooks/use-issue-timeline.test.tsx
@@ -177,6 +177,62 @@ describe("useIssueTimeline", () => {
expect(updated.map((e) => e.id)).toEqual(["new-c"]);
});
+ it("comment:created inserts at the correct sorted position by created_at", () => {
+ queryState.data = [
+ { type: "comment", id: "c1", actor_type: "member", actor_id: "u", created_at: "2026-05-06T01:00:00Z" },
+ { type: "comment", id: "c3", actor_type: "member", actor_id: "u", created_at: "2026-05-06T03:00:00Z" },
+ ];
+ renderHook(() => useIssueTimeline("issue-1", "user-1"));
+ const handler = wsHandlers.get("comment:created");
+ act(() => {
+ handler!({
+ comment: {
+ id: "c2",
+ issue_id: "issue-1",
+ author_type: "member",
+ author_id: "u",
+ content: "",
+ parent_id: null,
+ created_at: "2026-05-06T02:00:00Z",
+ updated_at: "2026-05-06T02:00:00Z",
+ type: "comment",
+ reactions: [],
+ attachments: [],
+ },
+ });
+ });
+ const updated = cacheUpdates.last as Array<{ id: string }>;
+ expect(updated.map((e) => e.id)).toEqual(["c1", "c2", "c3"]);
+ });
+
+ it("comment:created re-sorts when the new entry is oldest", () => {
+ queryState.data = [
+ { type: "comment", id: "c2", actor_type: "member", actor_id: "u", created_at: "2026-05-06T02:00:00Z" },
+ { type: "comment", id: "c3", actor_type: "member", actor_id: "u", created_at: "2026-05-06T03:00:00Z" },
+ ];
+ renderHook(() => useIssueTimeline("issue-1", "user-1"));
+ const handler = wsHandlers.get("comment:created");
+ act(() => {
+ handler!({
+ comment: {
+ id: "c1",
+ issue_id: "issue-1",
+ author_type: "member",
+ author_id: "u",
+ content: "",
+ parent_id: null,
+ created_at: "2026-05-06T01:00:00Z",
+ updated_at: "2026-05-06T01:00:00Z",
+ type: "comment",
+ reactions: [],
+ attachments: [],
+ },
+ });
+ });
+ const updated = cacheUpdates.last as Array<{ id: string }>;
+ expect(updated.map((e) => e.id)).toEqual(["c1", "c2", "c3"]);
+ });
+
it("ignores WS events for other issues", () => {
queryState.data = [];
renderHook(() => useIssueTimeline("issue-1", "user-1"));
diff --git a/packages/views/issues/hooks/use-issue-timeline.ts b/packages/views/issues/hooks/use-issue-timeline.ts
index 1d92ee1d30..b84ef7b0b3 100644
--- a/packages/views/issues/hooks/use-issue-timeline.ts
+++ b/packages/views/issues/hooks/use-issue-timeline.ts
@@ -33,6 +33,7 @@ import {
useToggleCommentReaction,
type ToggleCommentReactionVars,
} from "@multica/core/issues/mutations";
+import { sortTimelineEntriesAsc } from "@multica/core/issues/timeline-sort";
import { useWSEvent, useWSReconnect } from "@multica/core/realtime";
import { toast } from "sonner";
import { useT } from "../../i18n";
@@ -100,7 +101,7 @@ export function useIssueTimeline(issueId: string, userId?: string) {
const entry = commentToTimelineEntry(comment);
if (!old) return [entry];
if (old.some((e) => e.id === comment.id)) return old;
- return [...old, entry];
+ return sortTimelineEntriesAsc([...old, entry]);
});
},
[qc, issueId],
@@ -204,7 +205,7 @@ export function useIssueTimeline(issueId: string, userId?: string) {
qc.setQueryData<TLCache>(issueKeys.timeline(issueId), (old) => {
if (!old) return [entry];
if (old.some((e) => e.id === entry.id)) return old;
- return [...old, entry];
+ return sortTimelineEntriesAsc([...old, entry]);
});
},
[qc, issueId],