Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(channels): keep channelId when opening a rail conversation (#6972)

teob651b4771522
diff --git a/apps/web/src/features/channels-view/channels-view-context.tsx b/apps/web/src/features/channels-view/channels-view-context.tsx
index d93c2ee8e7..ecc7f56b6b 100644
--- a/apps/web/src/features/channels-view/channels-view-context.tsx
+++ b/apps/web/src/features/channels-view/channels-view-context.tsx
@@ -1,5 +1,6 @@
import {
type ChannelPreviewSelection,
+ channelIdForPreviewNavigation,
getChannelEntityTarget,
} from '@app/features/next-soup/utils';
import { makePersistedState } from '@app/lib/persistence';
@@ -146,10 +147,11 @@ export const [ChannelsViewProvider, useChannelsView] =
channel: ChannelPreviewSelection,
replace = false
) => {
- const channelId =
- channel.type === 'channel' ? channel.id : channel.channelId;
navigate(
- { route: channelDetailRoute, params: { channelId } },
+ {
+ route: channelDetailRoute,
+ params: { channelId: channelIdForPreviewNavigation(channel) },
+ },
{
replace,
search: { [channelsSearch.namespace]: routeSearch(channel) },
diff --git a/apps/web/src/features/channels-view/components/rail/ChannelsRail.tsx b/apps/web/src/features/channels-view/components/rail/ChannelsRail.tsx
index 430e9faabd..0281bacd17 100644
--- a/apps/web/src/features/channels-view/components/rail/ChannelsRail.tsx
+++ b/apps/web/src/features/channels-view/components/rail/ChannelsRail.tsx
@@ -11,6 +11,7 @@ import {
import { QUERY_FILTERS_BASE } from '@app/features/next-soup/filters/query-filters';
import {
type ChannelPreviewSelection,
+ channelPreviewSelection,
getChannelEntityTarget,
navigateChannelEntityToTarget,
} from '@app/features/next-soup/utils';
@@ -158,16 +159,18 @@ export function ChannelsRail(props: ChannelsRailProps) {
toast.failure('Unable to open conversation. Please try again.');
};
- const selectChannel = async (channel: WithNotification<ChannelEntity>) => {
+ const selectChannel = async (
+ channel: WithNotification<ChannelEntity>,
+ channelId: string
+ ) => {
try {
const entity = withEntityNotifications(channel, notificationSource);
- const target = getChannelEntityTarget(entity, {
- scopeChannelThreads: false,
+ const selection = channelPreviewSelection(channelId, {
+ target: getChannelEntityTarget(entity, {
+ scopeChannelThreads: false,
+ }),
+ notifications: entity.notifications,
});
- const selection = {
- ...entity,
- target: target?.kind === 'message' ? target : undefined,
- };
const previous = selectedChannel();
if (!setSelectedChannel(selection)) return;
// Repeated clicks must navigate even when the route stays the same.
@@ -185,14 +188,15 @@ export function ChannelsRail(props: ChannelsRailProps) {
const selectHydratedChannel = async (
pending: Promise<WithNotification<ChannelEntity>>,
- request: number
+ request: number,
+ channelId: string
) => {
try {
const channel = await pending;
if (request !== activation) return;
// Only fetched selections need to join the browser router's transition.
await startTransition(() => {
- if (request === activation) void selectChannel(channel);
+ if (request === activation) void selectChannel(channel, channelId);
});
} catch (error) {
if (request === activation) reportActivationError(error);
@@ -201,14 +205,21 @@ export function ChannelsRail(props: ChannelsRailProps) {
const activateChannel = (channel: ChannelEntity) => {
const request = ++activation;
+ // Capture before hydrate/await — store proxies from the list can lose
+ // fields if the query refreshes while notifications are fetched.
+ const channelId = channel.id;
+ if (!channelId) {
+ reportActivationError(new Error('Missing channel id'));
+ return;
+ }
const selection = hydrateChannelNotificationSelection(
channel,
notificationSource.withLocalOverrides
);
if (selection instanceof Promise) {
- void selectHydratedChannel(selection, request);
+ void selectHydratedChannel(selection, request, channelId);
} else {
- void selectChannel(selection);
+ void selectChannel(selection, channelId);
}
};
diff --git a/apps/web/src/features/next-soup/utils.test.ts b/apps/web/src/features/next-soup/utils.test.ts
index 93f5f08122..b508cc5b11 100644
--- a/apps/web/src/features/next-soup/utils.test.ts
+++ b/apps/web/src/features/next-soup/utils.test.ts
@@ -126,6 +126,8 @@ import type { NotificationSource, UnifiedNotification } from '@notifications';
import {
type CalendarPreviewSelection,
type ChannelPreviewSelection,
+ channelIdForPreviewNavigation,
+ channelPreviewSelection,
executeMarkEntitiesDone,
executeMarkEntitiesUndone,
getChannelEntityTarget,
@@ -331,6 +333,74 @@ describe('channel unread clicks', () => {
created_at: '2026-09-23T12:00:00Z',
};
+ it('builds a route selection with a plain id and ChannelEntityTarget', () => {
+ expect(
+ channelPreviewSelection('channel-1', {
+ target: {
+ kind: 'message',
+ messageId: 'newer',
+ threadId: 'thread',
+ },
+ })
+ ).toEqual({
+ type: 'channel',
+ id: 'channel-1',
+ target: { messageId: 'newer', threadId: 'thread' },
+ });
+ expect(
+ channelPreviewSelection('channel-1', { target: { kind: 'latest' } })
+ ).toEqual({ type: 'channel', id: 'channel-1' });
+ expect(() => channelPreviewSelection('')).toThrow(/Missing channel id/);
+ });
+
+ it('resolves a path channelId and rejects empty selections', () => {
+ expect(
+ channelIdForPreviewNavigation({ type: 'channel', id: 'channel-1' })
+ ).toBe('channel-1');
+ expect(
+ channelIdForPreviewNavigation({
+ type: 'channel_message',
+ id: 'msg',
+ channelId: 'channel-1',
+ messageId: 'msg',
+ })
+ ).toBe('channel-1');
+ expect(() =>
+ channelIdForPreviewNavigation({
+ type: 'channel',
+ id: undefined as unknown as string,
+ })
+ ).toThrow(/Missing channel id for channel preview navigation/);
+ expect(() =>
+ channelIdForPreviewNavigation({
+ type: 'channel_thread',
+ id: 'root',
+ channelId: undefined as unknown as string,
+ messageId: 'root',
+ threadId: 'root',
+ })
+ ).toThrow(/Missing channel id for channel preview navigation/);
+ });
+
+ it('keeps a captured id when the entity proxy later loses its id', () => {
+ const entity = {
+ type: 'channel' as const,
+ id: undefined as unknown as string,
+ notifications: () => [newer],
+ };
+ const selection = channelPreviewSelection('channel-1', {
+ target: getChannelEntityTarget(entity, { scopeChannelThreads: false }),
+ notifications: entity.notifications,
+ });
+ expect(selection).toMatchObject({
+ type: 'channel',
+ id: 'channel-1',
+ target: { messageId: 'newer', threadId: 'thread' },
+ });
+ expect(selection).not.toHaveProperty('kind');
+ expect(selection.target).not.toHaveProperty('kind');
+ });
+
it('reads current unread state on every click without revisiting read targets', () => {
let notifications = [older, newer, { ...newer, id: 'mention' }];
const row: ChannelPreviewSelection = {
diff --git a/apps/web/src/features/next-soup/utils.ts b/apps/web/src/features/next-soup/utils.ts
index e5b03b6444..e914b665b9 100644
--- a/apps/web/src/features/next-soup/utils.ts
+++ b/apps/web/src/features/next-soup/utils.ts
@@ -472,6 +472,52 @@ export type ChannelPreviewSelection = WithNotification<
>
>;
+/**
+ * Build the route selection a channels-rail click navigates with.
+ *
+ * Takes a plain `id` (capture it before any `await` — store proxies held across
+ * hydration can lose fields) and a resolved `ChannelClickTarget`. Stamps only
+ * `ChannelEntityTarget` fields onto `target` — never `kind`, which belongs on
+ * the click intent, not the route selection.
+ */
+export function channelPreviewSelection(
+ channelId: string,
+ options: {
+ target?: ChannelClickTarget;
+ notifications?: WithNotification<ChannelEntity>['notifications'];
+ } = {}
+): ChannelPreviewSelection {
+ if (!channelId) {
+ throw new Error('Missing channel id for channel preview selection');
+ }
+ const target =
+ options.target?.kind === 'message'
+ ? {
+ messageId: options.target.messageId,
+ ...(options.target.threadId != null
+ ? { threadId: options.target.threadId }
+ : {}),
+ }
+ : undefined;
+ return {
+ type: 'channel',
+ id: channelId,
+ ...(target ? { target } : {}),
+ ...(options.notifications ? { notifications: options.notifications } : {}),
+ };
+}
+
+/** Resolve the path param for a channel preview route, or throw. */
+export function channelIdForPreviewNavigation(
+ channel: ChannelPreviewSelection
+): string {
+ const channelId = channel.type === 'channel' ? channel.id : channel.channelId;
+ if (typeof channelId !== 'string' || channelId.length === 0) {
+ throw new Error('Missing channel id for channel preview navigation');
+ }
+ return channelId;
+}
+
export function getChannelEntityTarget(
entity: EntityData | ChannelPreviewSelection,
options: { scopeChannelThreads?: boolean } = {}
diff --git a/apps/web/src/lib/queries/channel/notification-selection.test.ts b/apps/web/src/lib/queries/channel/notification-selection.test.ts
index 626c1e660d..c84b5fa3fd 100644
--- a/apps/web/src/lib/queries/channel/notification-selection.test.ts
+++ b/apps/web/src/lib/queries/channel/notification-selection.test.ts
@@ -47,6 +47,20 @@ describe('channel selection hydration', () => {
expect(full.target?.messageId).toBe('explicit-search-target');
});
+ it('keeps the pre-await channel id if the store proxy loses it mid-fetch', async () => {
+ const row = channel([
+ { id: 'one', state: 'unseen', createdAt: '2026-01-01' },
+ ]);
+ fetchNotifications.mockImplementation(async () => {
+ (row as { id?: string }).id = undefined;
+ return [{ id: 'one' }];
+ });
+ const full = await hydrateChannelNotificationSelection(row);
+ expect(full.id).toBe('channel');
+ expect(full.type).toBe('channel');
+ expect(full.notifications?.()).toEqual([{ id: 'one' }]);
+ });
+
it('does not silently mark a partial selection on a failed full read', async () => {
fetchNotifications.mockRejectedValue(new Error('offline and uncached'));
await expect(
diff --git a/apps/web/src/lib/queries/channel/notification-selection.ts b/apps/web/src/lib/queries/channel/notification-selection.ts
index 4324a1164b..a9206910d8 100644
--- a/apps/web/src/lib/queries/channel/notification-selection.ts
+++ b/apps/web/src/lib/queries/channel/notification-selection.ts
@@ -25,14 +25,23 @@ async function fetchChannelNotificationSelection(
channel: ChannelEntity,
applyLocalOverrides?: (notification: Notification) => Notification
): Promise<WithNotification<ChannelEntity>> {
- const input = buildGraphqlEntitySoupInput('CHANNEL', channel.id);
+ // Snapshot identity before the await. Callers often pass Solid store proxies
+ // from the rail list; those can lose fields if the query refreshes mid-fetch.
+ const channelId = channel.id;
+ const name = channel.name;
+ const ownerId = channel.ownerId;
+ const channelType = channel.channelType;
+ const target = channel.target;
+ const input = buildGraphqlEntitySoupInput('CHANNEL', channelId);
if (!input) throw new Error('Invalid channel notification selection');
- const notifications = await fetchGraphqlEntityNotifications(
- input,
- channel.id
- );
+ const notifications = await fetchGraphqlEntityNotifications(input, channelId);
return {
- ...channel,
+ type: 'channel',
+ id: channelId,
+ name,
+ ownerId,
+ channelType,
+ ...(target ? { target } : {}),
unreadNotifications: undefined,
notifications: () =>
applyLocalOverrides