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(search): target the clicked channel search hit, not the channel's notification (#4761)
gbirman20d93be629e1
diff --git a/apps/web/src/features/entity/types/entity.ts b/apps/web/src/features/entity/types/entity.tsindex ae75c6cbeb..ec618f68c1 100644--- a/apps/web/src/features/entity/types/entity.ts+++ b/apps/web/src/features/entity/types/entity.ts@@ -69,6 +69,17 @@ type ChannelEntityLatestMessage = {mentions: string[];};+/**+ * The message a channel-family row activates when opened. Stamped at row+ * construction by producers whose row stands for one specific message (e.g.+ * search hits). Rows without a target are containers — the driving+ * notification decides at click time.+ */+export type ChannelEntityTarget = {+ messageId: string;+ threadId?: string;+};+export type ChannelEntity = EntityBase & {type: 'channel';channelType: 'direct_message' | 'private' | 'public' | 'team';@@ -76,6 +87,7 @@ export type ChannelEntity = EntityBase & {participantIds?: string[];latestMessage?: ChannelEntityLatestMessage;latestRootMessage?: ChannelEntityLatestMessage;+ target?: ChannelEntityTarget;};export type ChannelMessageEntity = EntityBase & {@@ -87,6 +99,7 @@ export type ChannelMessageEntity = EntityBase & {threadId?: string;senderId: string;content: string;+ target?: ChannelEntityTarget;};export type ChannelThreadEntity = EntityBase & {@@ -95,6 +108,7 @@ export type ChannelThreadEntity = EntityBase & {channelType?: ChannelEntity['channelType'];messageId: string;threadId: string;+ target?: ChannelEntityTarget;senderId: string;sender: SoupMessageSender;content: string;diff --git a/apps/web/src/features/next-soup/utils.test.ts b/apps/web/src/features/next-soup/utils.test.tsnew file mode 100644index 0000000000..9117f8befe--- /dev/null+++ b/apps/web/src/features/next-soup/utils.test.ts@@ -0,0 +1,165 @@+import { describe, expect, it, vi } from 'vitest';++// utils.ts transitively imports the websocket client modules, which open real+// sockets at module scope and reject under jsdom.+vi.mock('@service-storage/websocket', () => ({+ storageWS: { reconnectIfDisconnected: vi.fn() },+ createWebSocketJob: vi.fn(),+}));+vi.mock('@service-connection/websocket', () => ({+ ws: { addEventListener: vi.fn(), send: vi.fn() },+ state: () => 'closed',+ createConnectionBlockWebsocketEffect: vi.fn(),+ createConnectionWebsocketEffect: vi.fn(),+}));++import type { ChannelEntityTarget, EntityData } from '@entity';+import type { UnifiedNotification } from '@notifications';+import { getChannelEntityTarget } from './utils';++const sendNotification = (id: string, messageId: string): UnifiedNotification =>+ ({+ id,+ entity_type: 'channel',+ entity_id: 'channel-1',+ notification_event_type: 'channel_message_send',+ notification_metadata: {+ tag: 'channel_message_send',+ content: { messageId },+ },+ }) as unknown as UnifiedNotification;++const replyNotification = (+ id: string,+ messageId: string,+ threadId: string+): UnifiedNotification =>+ ({+ id,+ entity_type: 'channel',+ entity_id: 'channel-1',+ notification_event_type: 'channel_message_reply',+ notification_metadata: {+ tag: 'channel_message_reply',+ content: { messageId, threadId },+ },+ }) as unknown as UnifiedNotification;++const channelMessageRow = (opts?: {+ target?: ChannelEntityTarget;+ notifications?: UnifiedNotification[];+}): EntityData =>+ ({+ type: 'channel_message',+ id: 'channel-1:hit-msg',+ channelId: 'channel-1',+ messageId: 'hit-msg',+ threadId: 'hit-thread',+ ...(opts?.target ? { target: opts.target } : {}),+ ...(opts?.notifications ? { notifications: () => opts.notifications } : {}),+ }) as unknown as EntityData;++const channelRow = (opts?: {+ target?: ChannelEntityTarget;+ notifications?: UnifiedNotification[];+}): EntityData =>+ ({+ type: 'channel',+ id: 'channel-1',+ ...(opts?.target ? { target: opts.target } : {}),+ ...(opts?.notifications ? { notifications: () => opts.notifications } : {}),+ }) as unknown as EntityData;++const channelThreadRow = (opts?: {+ target?: ChannelEntityTarget;+ notifications?: UnifiedNotification[];+}): EntityData =>+ ({+ type: 'channel_thread',+ id: 'root-msg',+ channelId: 'channel-1',+ messageId: 'root-msg',+ threadId: 'root-msg',+ ...(opts?.target ? { target: opts.target } : {}),+ ...(opts?.notifications ? { notifications: () => opts.notifications } : {}),+ }) as unknown as EntityData;++describe('getChannelEntityTarget', () => {+ it('activates a stamped target over attached channel notifications (search message hit)', () => {+ const entity = channelMessageRow({+ target: { messageId: 'hit-msg', threadId: 'hit-thread' },+ notifications: [sendNotification('n1', 'recent-unread-msg')],+ });+ expect(getChannelEntityTarget(entity)).toEqual({+ messageId: 'hit-msg',+ threadId: 'hit-thread',+ });+ });++ it('activates a stamped target on a channel_thread row over notifications (future thread hit)', () => {+ const entity = channelThreadRow({+ target: { messageId: 'hit-reply', threadId: 'root-msg' },+ notifications: [replyNotification('n1', 'newest-reply', 'root-msg')],+ });+ expect(getChannelEntityTarget(entity)).toEqual({+ messageId: 'hit-reply',+ threadId: 'root-msg',+ });+ });++ it('falls back to own ids for an unstamped channel_message row without notifications', () => {+ expect(getChannelEntityTarget(channelMessageRow())).toEqual({+ messageId: 'hit-msg',+ threadId: 'hit-thread',+ });+ });++ it('targets the driving notification for a channel row', () => {+ const entity = channelRow({+ notifications: [sendNotification('n1', 'notif-msg')],+ });+ expect(getChannelEntityTarget(entity)).toEqual({+ messageId: 'notif-msg',+ threadId: undefined,+ });+ });++ it('returns no target for a channel row without notifications', () => {+ expect(getChannelEntityTarget(channelRow())).toBeUndefined();+ });++ it('skips thread-reply notifications for a channel row', () => {+ const entity = channelRow({+ notifications: [replyNotification('n1', 'reply-msg', 'other-thread')],+ });+ expect(getChannelEntityTarget(entity)).toBeUndefined();+ });++ it('targets the reply notification scoped to a channel_thread row', () => {+ const entity = channelThreadRow({+ notifications: [+ replyNotification('n1', 'reply-in-other-thread', 'other-thread'),+ replyNotification('n2', 'reply-msg', 'root-msg'),+ ],+ });+ expect(getChannelEntityTarget(entity)).toEqual({+ messageId: 'reply-msg',+ threadId: 'root-msg',+ });+ });++ it('falls back to the thread root when no notification matches the thread', () => {+ const entity = channelThreadRow({+ notifications: [replyNotification('n1', 'reply-msg', 'other-thread')],+ });+ expect(getChannelEntityTarget(entity)).toEqual({+ messageId: 'root-msg',+ threadId: 'root-msg',+ });+ });++ it('returns undefined for non-channel entities', () => {+ const entity = { type: 'email', id: 'e1' } as unknown as EntityData;+ expect(getChannelEntityTarget(entity)).toBeUndefined();+ });+});diff --git a/apps/web/src/features/next-soup/utils.ts b/apps/web/src/features/next-soup/utils.tsindex 26363c4122..a1145f4d0f 100644--- a/apps/web/src/features/next-soup/utils.ts+++ b/apps/web/src/features/next-soup/utils.ts@@ -25,6 +25,7 @@ import { throwOnErr } from '@core/util/result';import { waitForFrames } from '@core/util/sleep';import { openExternalUrl } from '@core/util/url';import {+ type ChannelEntityTarget,type EntityData,getSnippetHit,isGithubPrEntity,@@ -330,22 +331,26 @@ interface OpenEntityOptions {/*** Resolve which channel message to activate when a channel row is opened.*- * Inbox rows are keyed by the channel or thread, not by the message that was- * actually sent — a `channel` row is the whole channel and a `channel_thread`- * row is keyed by its root, so neither carries the id of the new message/reply.- * That id lives only on the driving notification (the same data the card- * renders), so read the target from there, exactly like the old inbox did via- * getChannelNotificationParams. Notifications are scoped to the row first- * (top-level sends for a channel, this thread's replies for a thread) and the- * most recent one wins.+ * A row with an explicit `target` (stamped at construction, e.g. a search hit+ * standing for one matched message) always activates it. Notifications must+ * never override a stamped target: the unified list attaches a channel-wide+ * notifications() accessor to every row (search results included), so their+ * presence says nothing about why a row exists.*- * Search hits are the other caller: a `channel_message` hit carries a real,- * distinct messageId and has no notification, so fall back to the entity's ids.- * A `channel` row with no notification has no message to target — open latest.+ * Rows without a target are containers — a `channel` row is the whole channel+ * and a `channel_thread` row is keyed by its root, so neither carries the id+ * of the message/reply that put it in the inbox. That id lives only on the+ * driving notification (the same data the card renders), so read the target+ * from there, exactly like the old inbox did via getChannelNotificationParams.+ * Notifications are scoped to the row first (top-level sends for a channel,+ * this thread's replies for a thread) and the most recent one wins. With no+ * notification either, fall back to the row's own ids — a `channel_thread`+ * row opens at its root and a `channel` row has no message to target (open+ * latest).*/export function getChannelEntityTarget(entity: EntityData-): { messageId: string; threadId?: string } | undefined {+): ChannelEntityTarget | undefined {if (entity.type !== 'channel' &&entity.type !== 'channel_message' &&@@ -354,6 +359,8 @@ export function getChannelEntityTarget(return undefined;}+ if (entity.target) return entity.target;+const fallback =entity.type === 'channel'? undefineddiff --git a/apps/web/src/lib/queries/soup/transform-utils.ts b/apps/web/src/lib/queries/soup/transform-utils.tsindex 9a8cdc0dfb..2531c463e8 100644--- a/apps/web/src/lib/queries/soup/transform-utils.ts+++ b/apps/web/src/lib/queries/soup/transform-utils.ts@@ -270,6 +270,10 @@ export function mapChannelSearchResultItem(channelType,messageId: msg.message_id!,threadId: msg.thread_id ?? undefined,+ target: {+ messageId: msg.message_id!,+ threadId: msg.thread_id ?? undefined,+ },senderId: msg.sender_id!,content,name: channelName,