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(channels): channel clicks not going to unread notification (#7139)
Peter Chinman5345a919d66c
diff --git a/apps/web/src/features/next-soup/utils.test.ts b/apps/web/src/features/next-soup/utils.test.tsindex 323859d082..78ca2f05e1 100644--- a/apps/web/src/features/next-soup/utils.test.ts+++ b/apps/web/src/features/next-soup/utils.test.ts@@ -16,6 +16,13 @@ vi.mock('@core/mobile/isTouchDevice', () => ({}));const toastAlert = vi.hoisted(() => vi.fn());+const fetchChannelNotifications = vi.hoisted(() => vi.fn());+vi.mock('@service-storage/graphql-notifications', async (importOriginal) => ({+ ...(await importOriginal<+ typeof import('@service-storage/graphql-notifications')+ >()),+ fetchGraphqlEntityNotifications: fetchChannelNotifications,+}));vi.mock('@core/component/Toast/Toast', () => ({toast: { alert: toastAlert },}));@@ -123,6 +130,7 @@ import { setGlobalSplitManager } from '@app/signal/splitLayout';import type { SplitManager } from '@components/app/split-layout/layoutManager';import { type ChannelEntityTarget, type EntityData, queryKeys } from '@entity';import type { NotificationSource, UnifiedNotification } from '@notifications';+import { hydrateChannelNotificationSelection } from '@queries/channel/notification-selection';import {type CalendarPreviewSelection,type ChannelPreviewSelection,@@ -333,6 +341,72 @@ describe('channel unread clicks', () => {created_at: '2026-09-23T12:00:00Z',};+ it('targets the newest unread reply even when the rail has an empty cached witness', async () => {+ fetchChannelNotifications.mockResolvedValue([+ older,+ newer,+ asRead({+ ...newer,+ id: 'already-read',+ created_at: '2026-09-25T12:00:00Z',+ }),+ ]);+ const channel = await hydrateChannelNotificationSelection({+ type: 'channel',+ id: 'channel-1',+ name: 'Channel',+ ownerId: 'owner',+ channelType: 'private',+ unreadNotifications: [],+ });+ const selection = channelPreviewSelection(channel.id, {+ target: getChannelEntityTarget(channel, { scopeChannelThreads: false }),+ });++ expect(selection.target).toEqual({+ messageId: 'newer',+ threadId: 'thread',+ });+ });++ it('honors local reads while refreshing a stale empty rail row on every click', async () => {+ fetchChannelNotifications.mockResolvedValue([older, newer]);+ const row = {+ type: 'channel' as const,+ id: 'channel-1',+ name: 'Channel',+ ownerId: 'owner',+ channelType: 'private' as const,+ unreadNotifications: [],+ };+ const readIds = new Set([newer.id]);+ const click = async () => {+ const channel = await hydrateChannelNotificationSelection(row, (n) =>+ readIds.has(n.id) ? asRead(n) : n+ );+ return getChannelEntityTarget(channel, { scopeChannelThreads: false });+ };++ expect(await click()).toMatchObject({+ kind: 'message',+ messageId: 'older',+ });+ readIds.add(older.id);+ expect(await click()).toEqual({ kind: 'latest' });++ fetchChannelNotifications.mockResolvedValue([+ older,+ newer,+ replyNotification('incoming', 'new-arrival', 'thread'),+ ]);+ expect(await click()).toMatchObject({+ kind: 'message',+ messageId: 'new-arrival',+ threadId: 'thread',+ });+ expect(fetchChannelNotifications).toHaveBeenCalledTimes(3);+ });+it('builds a route selection with a plain id and ChannelEntityTarget', () => {expect(channelPreviewSelection('channel-1', {diff --git a/apps/web/src/lib/queries/channel/notification-selection.test.ts b/apps/web/src/lib/queries/channel/notification-selection.test.tsindex db45369cb7..ac3f2bf277 100644--- a/apps/web/src/lib/queries/channel/notification-selection.test.ts+++ b/apps/web/src/lib/queries/channel/notification-selection.test.ts@@ -22,17 +22,31 @@ const channel = (describe('channel selection hydration', () => {beforeEach(() => vi.clearAllMocks());- it('returns synchronously for a read channel or a legacy row', () => {+ it('returns synchronously for a legacy row', () => {const legacy = channel(undefined);expect(hydrateChannelNotificationSelection(legacy)).toBe(legacy);- const read = hydrateChannelNotificationSelection(channel([]));- expect(read).not.toBeInstanceOf(Promise);- if (read instanceof Promise)- throw new Error('Unexpected notification fetch');- expect(read.notifications?.()).toEqual([]);expect(fetchNotifications).not.toHaveBeenCalled();});+ it('refreshes an empty unread witness instead of treating a stale list as read', async () => {+ const incoming = [{ id: 'new-reply' }];+ fetchNotifications.mockResolvedValue(incoming);++ const selection = await hydrateChannelNotificationSelection(channel([]));++ expect(fetchNotifications).toHaveBeenCalledOnce();+ expect(selection.notifications?.()).toEqual(incoming);+ });++ it('returns an empty selection only after refreshing a read channel', async () => {+ fetchNotifications.mockResolvedValue([]);++ const selection = await hydrateChannelNotificationSelection(channel([]));++ expect(fetchNotifications).toHaveBeenCalledOnce();+ expect(selection.notifications?.()).toEqual([]);+ });+it('uses the complete edge, not the one unread witness, for the selected row', async () => {const complete = ['one', 'two', 'thread-reply'].map((id) => ({ id }));fetchNotifications.mockResolvedValue(complete);@@ -80,12 +94,20 @@ describe('channel selection hydration', () => {});- it('does not silently mark a partial selection on a failed full read', async () => {- fetchNotifications.mockRejectedValue(new Error('offline and uncached'));- await expect(- hydrateChannelNotificationSelection(- channel([{ id: 'one', state: 'unseen', createdAt: '2026-01-01' }])- )- ).rejects.toThrow('offline and uncached');- });+ it.each<{ unreadNotifications: ChannelEntity['unreadNotifications'] }>([+ { unreadNotifications: [] },+ {+ unreadNotifications: [+ { id: 'one', state: 'unseen', createdAt: '2026-01-01' },+ ],+ },+ ])(+ 'does not silently use a partial selection when the refresh fails (%j)',+ async ({ unreadNotifications }) => {+ fetchNotifications.mockRejectedValue(new Error('offline and uncached'));+ await expect(+ hydrateChannelNotificationSelection(channel(unreadNotifications))+ ).rejects.toThrow('offline and uncached');+ }+ );});diff --git a/apps/web/src/lib/queries/channel/notification-selection.ts b/apps/web/src/lib/queries/channel/notification-selection.tsindex 301def914f..3a941ccf59 100644--- a/apps/web/src/lib/queries/channel/notification-selection.ts+++ b/apps/web/src/lib/queries/channel/notification-selection.ts@@ -7,17 +7,15 @@ import { fetchGraphqlEntityNotifications } from '@service-storage/graphql-notifiimport { buildGraphqlEntitySoupInput } from '../soup/graphql/entity-input';/**- * Return immediately when no fetch is needed. A limit-one unread edge needs- * hydration to determine message targets, mark-read IDs, and thread membership.+ * Refresh a bounded list projection before choosing a message target. Even an+ * empty unread edge can be stale after a new notification arrives. Only rows+ * without that projection keep their existing notification source.*/export function hydrateChannelNotificationSelection(channel: ChannelEntity,applyLocalOverrides?: (notification: Notification) => Notification): WithNotification<ChannelEntity> | Promise<WithNotification<ChannelEntity>> {if (channel.unreadNotifications === undefined) return channel;- if (channel.unreadNotifications.length === 0) {- return { ...channel, notifications: () => [] };- }return fetchChannelNotificationSelection(channel, applyLocalOverrides);}