Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
refactor(mobile): reuse core's inbox grouping and landing rule (MUL-9346) (#10683)
Jiayuan Zhang091e014fa82e
diff --git a/apps/mobile/lib/inbox-display.test.ts b/apps/mobile/lib/inbox-display.test.tsindex c5a6174b1a..a2c9e14930 100644--- a/apps/mobile/lib/inbox-display.test.ts+++ b/apps/mobile/lib/inbox-display.test.ts@@ -31,71 +31,18 @@ function item(overrides: Partial<InboxItem>): InboxItem {};}+// The grouping and anchor rule itself is tested in packages/core/inbox/queries.test.ts.describe("deduplicateInboxItems", () => {- it("keeps the newest issue row while preserving an older comment anchor", () => {- const merged = deduplicateInboxItems([- item({- id: "comment-notification",- created_at: "2026-06-15T08:00:00Z",- details: { comment_id: "comment-1" },- }),- item({- id: "status-notification",- type: "status_changed",- created_at: "2026-06-15T08:01:00Z",- details: { from: "in_progress", to: "in_review" },- }),- ]);-- expect(merged).toHaveLength(1);- expect(merged[0]).toMatchObject({- id: "status-notification",- type: "status_changed",- details: {- from: "in_progress",- to: "in_review",- comment_id: "comment-1",- },- });- });-- it("anchors at the oldest unread comment, or the newest once all are read", () => {- const unread = deduplicateInboxItems([- item({ id: "older", created_at: "2026-06-15T08:00:00Z", details: { comment_id: "comment-1" } }),- item({ id: "newer", created_at: "2026-06-15T08:02:00Z", details: { comment_id: "comment-2" } }),- ]);- expect(unread[0]?.details?.comment_id).toBe("comment-1");-- const read = deduplicateInboxItems([- item({ id: "older", read: true, created_at: "2026-06-15T08:00:00Z", details: { comment_id: "comment-1" } }),- item({ id: "newer", read: true, created_at: "2026-06-15T08:02:00Z", details: { comment_id: "comment-2" } }),- ]);- expect(read[0]?.details?.comment_id).toBe("comment-2");- });-- it("anchors an unread failed run at the run, and no unread entry at nothing", () => {- const failed = deduplicateInboxItems([- item({ id: "read", read: true, created_at: "2026-06-15T08:00:00Z", details: { comment_id: "comment-1" } }),- item({ id: "failed", type: "task_failed", created_at: "2026-06-15T08:02:00Z", details: { task_id: "task-1" } }),- ]);- expect(failed[0]?.details?.comment_id).toBe("task-1");-- const status = deduplicateInboxItems([- item({ id: "read", read: true, created_at: "2026-06-15T08:00:00Z", details: { comment_id: "comment-1" } }),- item({ id: "status", type: "status_changed", created_at: "2026-06-15T08:02:00Z", details: { to: "in_review" } }),- ]);- expect(status[0]?.details?.comment_id).toBeUndefined();- });-- it("lets an unread reply lead over an earlier unread reaction", () => {- const merged = deduplicateInboxItems([- item({ id: "reaction", type: "reaction_added", created_at: "2026-06-15T08:00:00Z", details: { comment_id: "old-comment" } }),- item({ id: "reply", created_at: "2026-06-15T08:02:00Z", details: { comment_id: "comment-2" } }),+ it("anchors an app request row like any other row, since mobile renders no requests", () => {+ const [row] = deduplicateInboxItems([+ item({ id: "comment", created_at: "2026-06-15T08:00:00Z", details: { comment_id: "comment-1" } }),+ item({ id: "app", type: "app_action_requested", created_at: "2026-06-15T08:02:00Z", details: { request_id: "request-1" } }),]);- expect(merged[0]?.details?.comment_id).toBe("comment-2");+ expect(row?.id).toBe("app");+ expect(getInboxNavigationTarget(row!, "acme", "tap")?.params).toMatchObject({ id: "issue-1", highlight: "comment-1" });});- it("gives an unread app request no target, since mobile renders no requests", () => {+ it("gives an app request row no target when nothing unread has an entry", () => {const [row] = deduplicateInboxItems([item({ id: "comment", read: true, created_at: "2026-06-15T08:00:00Z", details: { comment_id: "comment-1" } }),item({ id: "app", type: "app_action_requested", created_at: "2026-06-15T08:02:00Z", details: { request_id: "request-1" } }),diff --git a/apps/mobile/lib/inbox-display.ts b/apps/mobile/lib/inbox-display.tsindex aea82d04e5..a8af079d06 100644--- a/apps/mobile/lib/inbox-display.ts+++ b/apps/mobile/lib/inbox-display.ts@@ -7,6 +7,7 @@* web for the same item. When the web version changes, sync this file.*/import type { InboxItem, TimelineEntry } from "@multica/core/types";+import { groupInboxItemsByIssue } from "@multica/core/inbox/group";import { i18n } from "@/lib/i18n/singleton";function formatResetAt(value: string | undefined): string {@@ -156,70 +157,15 @@ export function resolveTimelineHighlight(}/**- * Deduplicate inbox items by issue_id (Linear-style: one entry per issue).- *- * Mirrors packages/core/inbox/queries.ts deduplicateInboxItems. **MUST stay- * aligned with that function** — see the inbox dedup incident in this file's- * companion `apps/mobile/CLAUDE.md` "Behavioral parity" section. Skipping- * this step makes the same workspace/user show different unread counts on- * mobile vs web.- *- * Steps:- * 1. Drop archived rows (these never appear in web's inbox view).- * 2. Group by `issue_id` (fall back to `id` for items with no issue- * attached — e.g. quick_create_failed).- * 3. In each group, keep the newest by `created_at`.- * 4. Anchor the row (`details.comment_id`) on the timeline entry it opens- * at, as web's `withCommentAnchor` does: unread, the oldest unread- * comment or failed run, a reaction only when nothing else unread has- * one, and none when no unread notification has an entry; read, the- * newest notification's entry. Mobile renders no app requests, so- * unlike web an app request row gets no target of its own.- * 5. Sort the result newest-first.+ * The inbox list: archived rows dropped, one row per issue, each opening at+ * the timeline entry web opens it at. The grouping and anchor rule is core's+ * `groupInboxItemsByIssue`, the one web runs. Mobile renders no app+ * requests, so an app request row anchors like any other row instead of+ * keeping its request.*/export function deduplicateInboxItems(items: InboxItem[]): InboxItem[] {- const active = items.filter((i) => !i.archived);- const groups = new Map<string, InboxItem[]>();- for (const item of active) {- const key = item.issue_id ?? item.id;- const group = groups.get(key) ?? [];- group.push(item);- groups.set(key, group);- }- const merged: InboxItem[] = [];- for (const group of groups.values()) {- group.sort(- (a, b) =>- new Date(b.created_at).getTime() - new Date(a.created_at).getTime(),- );- const newest = group[0];- if (!newest) continue;-- const target = (item: InboxItem) =>- item.details?.comment_id ||- (item.type === "task_failed" ? item.details?.task_id : undefined) ||- undefined;- const unread = group.filter((item) => item.read !== true);- const anchor =- unread.length > 0- ? (unread.findLast(- (item) => item.type !== "reaction_added" && !!target(item),- ) ?? unread.findLast((item) => !!target(item)))- : group.find((item) => !!target(item));- const anchorId = anchor ? target(anchor) : undefined;-- if (anchorId === newest.details?.comment_id) {- merged.push(newest);- continue;- }- const { comment_id: _dropped, ...details } = newest.details ?? {};- merged.push({- ...newest,- details: anchorId ? { ...details, comment_id: anchorId } : details,- });- }- return merged.sort(- (a, b) =>- new Date(b.created_at).getTime() - new Date(a.created_at).getTime(),+ return groupInboxItemsByIssue(+ items.filter((i) => !i.archived),+ { keepRequestTarget: false },);}diff --git a/apps/mobile/lib/unread-counts.ts b/apps/mobile/lib/unread-counts.tsindex 3058753009..4107fcc2ca 100644--- a/apps/mobile/lib/unread-counts.ts+++ b/apps/mobile/lib/unread-counts.ts@@ -35,9 +35,8 @@ import { chatSessionsOptions } from "@/data/queries/chat";** The lookup mirrors `unreadCountForWorkspace` in* packages/core/inbox/queries.ts rather than importing it — that module pulls- * in core's API client, which mobile does not use (same reason- * `deduplicateInboxItems` is mirrored into lib/inbox-display.ts). A workspace- * with nothing unread is absent from the response, so a missing entry is zero.+ * in core's API client, which mobile does not use. A workspace with nothing+ * unread is absent from the response, so a missing entry is zero.*/export function useInboxUnreadCount(wsId: string | null | undefined): number {const { data } = useQuery({diff --git a/packages/core/inbox/group.ts b/packages/core/inbox/group.tsindex 6727926cb5..0deced7111 100644--- a/packages/core/inbox/group.ts+++ b/packages/core/inbox/group.ts@@ -18,6 +18,11 @@ export function groupInboxRows(items: readonly InboxItem[]): InboxItem[][] {return out;}+export interface InboxAnchorOptions {+ /** An app request row opens at its request rather than a timeline entry. Defaults to true. */+ keepRequestTarget?: boolean;+}+/*** The timeline entry a notification is about: its comment, or the failed run* it reports, which the timeline addresses by task id like a comment.@@ -36,10 +41,15 @@ function inboxTimelineTarget(item: InboxItem): string | undefined {* Unread with no entry at all (a status change, an assignment), it opens* where the reader left off rather than at a comment already read. Read, it* opens at the newest notification's entry. An app notification lands on its- * own request instead, so it never borrows one.+ * own request instead, so it never borrows one — unless `keepRequestTarget`+ * is off, for a client that renders no requests.*/-export function withCommentAnchor(row: InboxItem, group: readonly InboxItem[]): InboxItem {- if (row.type.startsWith("app_") && row.details?.request_id) return row;+export function withCommentAnchor(+ row: InboxItem,+ group: readonly InboxItem[],+ { keepRequestTarget = true }: InboxAnchorOptions = {},+): InboxItem {+ if (keepRequestTarget && row.type.startsWith("app_") && row.details?.request_id) return row;const unread = group.filter((item) => item.read !== true);const anchor =unread.length > 0@@ -53,9 +63,12 @@ export function withCommentAnchor(row: InboxItem, group: readonly InboxItem[]):}/** One row per issue: its newest notification. Newest group first. */-export function groupInboxItemsByIssue(items: readonly InboxItem[]): InboxItem[] {+export function groupInboxItemsByIssue(+ items: readonly InboxItem[],+ options?: InboxAnchorOptions,+): InboxItem[] {return groupInboxRows(items)- .flatMap((group) => (group[0] ? [withCommentAnchor(group[0], group)] : []))+ .flatMap((group) => (group[0] ? [withCommentAnchor(group[0], group, options)] : [])).sort(byNewest);}diff --git a/packages/core/package.json b/packages/core/package.jsonindex 25fc1219d6..def71e2494 100644--- a/packages/core/package.json+++ b/packages/core/package.json@@ -68,6 +68,7 @@"./issues/stores/view-store-context": "./issues/stores/view-store-context.tsx","./issues/stores/*": "./issues/stores/*.ts","./inbox": "./inbox/index.ts",+ "./inbox/group": "./inbox/group.ts","./inbox/queries": "./inbox/queries.ts","./inbox/mutations": "./inbox/mutations.ts","./inbox/filter-store": "./inbox/filter-store.ts",