Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix: notification stacking behavior (#2673)
Peter Chinman8721128bb4dd
diff --git a/js/app/packages/notifications/notification-source.ts b/js/app/packages/notifications/notification-source.tsindex e63ed69a95..77b466d980 100644--- a/js/app/packages/notifications/notification-source.ts+++ b/js/app/packages/notifications/notification-source.ts@@ -1,4 +1,5 @@import type { Entity } from '@core/types';+import { ENABLE_DOCUMENT_MENTION_NOTIFICATIONS } from '@core/constant/featureFlags';import {optimisticInsertNotification,useMarkNotificationsAsDoneMutation,@@ -132,6 +133,18 @@ export function createNotificationSource(setMutedEntities(reconcile(mutedEntities));});+ if (!ENABLE_DOCUMENT_MENTION_NOTIFICATIONS) {+ createEffect(() => {+ const toDiscard = notifications().filter(+ (n) => n.notification_event_type === 'document_mention' && !n.done+ );+ if (toDiscard.length === 0) return;+ void markNotificationsAsDoneMutation.mutateAsync({+ notificationIds: toDiscard.map((n) => n.id),+ });+ });+ }+const mapWebsocketNotification = (raw: ConnGatewayInnerNotifValue): UnifiedNotification => {diff --git a/js/app/packages/notifications/notification-stacking.ts b/js/app/packages/notifications/notification-stacking.tsindex 12d4279126..bf09f5a7a5 100644--- a/js/app/packages/notifications/notification-stacking.ts+++ b/js/app/packages/notifications/notification-stacking.ts@@ -28,17 +28,29 @@ export function getAllNotificationsFromGroup(}/**- * Gets the threadId from a replies stack+ * Gets the threadId from a thread stack (replies, thread-mentions, or absorbed root sends)*/export function getThreadId(group: NotificationStack): string {- const notification = group.notifications[0];- return match(notification.notification_metadata)- .with({ tag: 'channel_message_reply' }, (m) => m.content.threadId ?? '')- .otherwise(() => '');+ for (const notification of group.notifications) {+ const threadId = match(notification.notification_metadata)+ .with({ tag: 'channel_message_reply' }, (m) => m.content.threadId ?? '')+ .with({ tag: 'channel_mention' }, (m) => m.content.threadId ?? '')+ .otherwise(() => '');+ if (threadId) return threadId;+ }+ return '';}/*** Stacks notifications by type for unrolled notification display.+ *+ * Stacking rules:+ * - Replies, thread-mentions (channel_mention with threadId), and the root send+ * for a thread all group into a single thread stack.+ * - Root-level new message notifications are grouped into a single stack.+ * - Root mentions (channel_mention without threadId) each form their own stack.+ * - Any send/reply whose messageId matches a mention's messageId is shadowed+ * (the mention is more informative).*/export function stackNotifications(notifications: UnifiedNotification[]@@ -48,16 +60,29 @@ export function stackNotifications(const byTag = (tag: string) => (n: UnifiedNotification) =>n.notification_metadata.tag === tag;- // Collect mention messageIds for shadowing+ const allMentions = filteredNotifications.filter(byTag('channel_mention'));++ // Thread mentions have a threadId; root mentions don't+ const rootMentions = allMentions.filter((n) =>+ match(n.notification_metadata)+ .with({ tag: 'channel_mention' }, (m) => !m.content.threadId)+ .otherwise(() => false)+ );+ const threadMentions = allMentions.filter((n) =>+ match(n.notification_metadata)+ .with({ tag: 'channel_mention' }, (m) => !!m.content.threadId)+ .otherwise(() => false)+ );++ // All mention messageIds for shadowing sends/repliesconst mentionedMsgIds = new Set(- filteredNotifications- .filter(byTag('channel_mention'))+ allMentions.map((n) =>match(n.notification_metadata).with({ tag: 'channel_mention' }, (m) => m.content.messageId).otherwise(() => undefined))- .filter(Boolean)+ .filter((id): id is string => id !== undefined));const isShadowed = (n: UnifiedNotification) =>@@ -69,22 +94,74 @@ export function stackNotifications().otherwise(() => false);- const mentions = filteredNotifications.filter(byTag('channel_mention'));- const newMsgs = filteredNotifications- .filter(byTag('channel_message_send'))- .filter((n) => !isShadowed(n));+ const activeThreadIds = new Set(+ filteredNotifications+ .map((n) =>+ match(n.notification_metadata)+ .with({ tag: 'channel_message_reply' }, (m) => m.content.threadId)+ .with(+ { tag: 'channel_mention' },+ (m) => m.content.threadId ?? undefined+ )+ .otherwise(() => undefined)+ )+ .filter((id): id is string => id !== undefined)+ );+const replies = filteredNotifications.filter(byTag('channel_message_reply')).filter((n) => !isShadowed(n));++ const allSends = filteredNotifications.filter(byTag('channel_message_send'));++ const isAbsorbedIntoThread = (n: UnifiedNotification) =>+ match(n.notification_metadata)+ .with({ tag: 'channel_message_send' }, (m) =>+ activeThreadIds.has(m.content.messageId)+ )+ .otherwise(() => false);++ // Unshadowed sends not belonging to a thread → "new messages" stack+ const newMsgs = allSends.filter(+ (n) => !isShadowed(n) && !isAbsorbedIntoThread(n)+ );+ // Unshadowed sends whose messageId is a known threadId → join thread stack+ const absorbedSends = allSends.filter(+ (n) => !isShadowed(n) && isAbsorbedIntoThread(n)+ );++ // Root mentions whose messageId is a known threadId → join that thread stack+ const absorbedRootMentions = rootMentions.filter((n) =>+ match(n.notification_metadata)+ .with({ tag: 'channel_mention' }, (m) =>+ activeThreadIds.has(m.content.messageId)+ )+ .otherwise(() => false)+ );+ // Root mentions with no matching thread → remain as individual stacks+ const orphanRootMentions = rootMentions.filter((n) =>+ match(n.notification_metadata)+ .with(+ { tag: 'channel_mention' },+ (m) => !activeThreadIds.has(m.content.messageId)+ )+ .otherwise(() => false)+ );+const docMentions = notifications.filter(byTag('document_mention'));const others = notifications.filter((n) => !isChannelNotification(n) && !byTag('document_mention')(n));const groups: NotificationStack[] = [- ...mentions.flatMap((n) => makeStack('channel_mention', [n])),+ ...orphanRootMentions.flatMap((n) => makeStack('channel_mention', [n])),...makeStack('channel_message_send', newMsgs),- ...makeReplyStacks(replies),+ ...makeThreadStacks(+ replies,+ threadMentions,+ absorbedSends,+ absorbedRootMentions+ ),...makeStack('document_mention', docMentions),...others.flatMap((n) => makeStack(n.notification_metadata.tag, [n])),];@@ -125,12 +202,29 @@ function makeStack(return [{ type, notifications: sortByRecency(notifications) }];}-function makeReplyStacks(replies: UnifiedNotification[]): NotificationStack[] {- const byThread = groupBy(replies, (r) =>- match(r.notification_metadata)+function makeThreadStacks(+ replies: UnifiedNotification[],+ threadMentions: UnifiedNotification[],+ absorbedSends: UnifiedNotification[],+ absorbedRootMentions: UnifiedNotification[]+): NotificationStack[] {+ // Key each notification by its threadId. For absorbed root sends and root+ // mentions, their messageId IS the threadId (they are the thread root).+ const keyOf = (n: UnifiedNotification): string =>+ match(n.notification_metadata).with({ tag: 'channel_message_reply' }, (m) => m.content.threadId ?? '')- .otherwise(() => '')+ .with(+ { tag: 'channel_mention' },+ (m) => m.content.threadId ?? m.content.messageId+ )+ .with({ tag: 'channel_message_send' }, (m) => m.content.messageId)+ .otherwise(() => '');++ const byThread = groupBy(+ [...replies, ...threadMentions, ...absorbedSends, ...absorbedRootMentions],+ keyOf);+return [...byThread.entries()].filter(([threadId]) => threadId !== '').map(([, group]) => ({diff --git a/js/app/packages/notifications/tests/notification-stacking.test.ts b/js/app/packages/notifications/tests/notification-stacking.test.tsindex 62e94b4587..ab13ea967c 100644--- a/js/app/packages/notifications/tests/notification-stacking.test.ts+++ b/js/app/packages/notifications/tests/notification-stacking.test.ts@@ -134,7 +134,7 @@ describe('stackNotifications', () => {}});- it('keeps mentions as individual items', () => {+ it('keeps root mentions (no threadId) as individual items', () => {const notifications = [createMentionNotification('m1', 'msg-1', 1000),createMentionNotification('m2', 'msg-2', 2000),@@ -147,6 +147,116 @@ describe('stackNotifications', () => {});});+ describe('thread stacking', () => {+ it('absorbs root send into thread stack when replies exist', () => {+ const notifications = [+ createNewMessageNotification('n1', 'msg-1', 1000),+ createReplyNotification('r1', 'reply-1', 'msg-1', 2000),+ ];++ const result = stackNotifications(notifications);++ expect(result).toHaveLength(1);+ expect(result[0].type).toBe('channel_message_reply');+ expect(getThreadId(result[0])).toBe('msg-1');+ const ids = result[0].notifications.map((n) => n.id);+ expect(ids).toContain('n1');+ expect(ids).toContain('r1');+ });++ it('keeps root send in new messages stack when no thread activity', () => {+ const notifications = [+ createNewMessageNotification('n1', 'msg-1', 1000),+ createNewMessageNotification('n2', 'msg-2', 2000),+ ];++ const result = stackNotifications(notifications);++ expect(result).toHaveLength(1);+ expect(result[0].type).toBe('channel_message_send');+ expect(result[0].notifications).toHaveLength(2);+ });++ it('groups thread mention (with threadId) into the thread stack', () => {+ const notifications = [+ createReplyNotification('r1', 'reply-1', 'thread-A', 1000),+ createMentionNotification('m1', 'reply-2', 2000, 'thread-A'),+ ];++ const result = stackNotifications(notifications);++ expect(result).toHaveLength(1);+ expect(result[0].type).toBe('channel_message_reply');+ expect(getThreadId(result[0])).toBe('thread-A');+ const ids = result[0].notifications.map((n) => n.id);+ expect(ids).toContain('r1');+ expect(ids).toContain('m1');+ });++ it('groups root send, replies, and thread mention all in one thread stack', () => {+ const notifications = [+ createNewMessageNotification('n1', 'msg-1', 1000),+ createReplyNotification('r1', 'reply-1', 'msg-1', 2000),+ createMentionNotification('m1', 'reply-2', 3000, 'msg-1'),+ ];++ const result = stackNotifications(notifications);++ expect(result).toHaveLength(1);+ expect(result[0].type).toBe('channel_message_reply');+ expect(result[0].notifications).toHaveLength(3);+ const ids = result[0].notifications.map((n) => n.id);+ expect(ids).toContain('n1');+ expect(ids).toContain('r1');+ expect(ids).toContain('m1');+ // Sorted most-recent first+ expect(result[0].notifications[0].id).toBe('m1');+ });++ it('thread mention shadows reply with same messageId in same thread', () => {+ const notifications = [+ createReplyNotification('r1', 'reply-1', 'thread-A', 1000),+ createMentionNotification('m1', 'reply-1', 2000, 'thread-A'),+ ];++ const result = stackNotifications(notifications);++ expect(result).toHaveLength(1);+ expect(result[0].type).toBe('channel_message_reply');+ const ids = result[0].notifications.map((n) => n.id);+ expect(ids).toContain('m1');+ expect(ids).not.toContain('r1'); // shadowed by the thread mention+ });++ it('root mention for thread root merges into thread stack', () => {+ const notifications = [+ createNewMessageNotification('n1', 'msg-1', 1000),+ createReplyNotification('r1', 'reply-1', 'msg-1', 2000),+ createMentionNotification('m1', 'msg-1', 3000), // root mention, no threadId+ ];++ const result = stackNotifications(notifications);++ // All in one thread stack: m1 + r1 (n1 shadowed by root mention m1)+ expect(result).toHaveLength(1);+ expect(result[0].type).toBe('channel_message_reply');+ expect(getThreadId(result[0])).toBe('msg-1');+ const ids = result[0].notifications.map((n) => n.id);+ expect(ids).toContain('m1');+ expect(ids).toContain('r1');+ expect(ids).not.toContain('n1'); // shadowed by root mention+ });++ it('root mention with no thread activity stays as individual stack', () => {+ const notifications = [createMentionNotification('m1', 'msg-1', 1000)];++ const result = stackNotifications(notifications);++ expect(result).toHaveLength(1);+ expect(result[0].type).toBe('channel_mention');+ });+ });+describe('mention shadowing', () => {it('shadows new message notification when mention exists for same messageId', () => {const notifications = [