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 unread channels in place in the rail (#9105)

Eric Hayesb454005395b9
diff --git a/apps/web/src/features/channels-view/core/channel-sort.test.ts b/apps/web/src/features/channels-view/core/channel-sort.test.ts
new file mode 100644
index 0000000000..27485b5e92
--- /dev/null
+++ b/apps/web/src/features/channels-view/core/channel-sort.test.ts
@@ -0,0 +1,68 @@
+import type { ChannelEntity } from '@entity';
+import { describe, expect, it } from 'vitest';
+import { sortChannels } from './channel-sort';
+
+const channel = (
+ id: string,
+ updatedAt: string,
+ unread: { createdAt: string; state?: 'unseen' | 'seen' | 'done' }[] = []
+) =>
+ ({
+ id,
+ type: 'channel',
+ name: id,
+ channelType: 'public',
+ updatedAt,
+ unreadNotifications: unread.map((notification, index) => ({
+ id: `${id}-${index}`,
+ state: notification.state ?? 'unseen',
+ createdAt: notification.createdAt,
+ })),
+ }) as ChannelEntity;
+
+const ids = (channels: ChannelEntity[]) => channels.map(({ id }) => id);
+
+describe('sortChannels', () => {
+ it('keeps unread channels in place when they get newer messages', () => {
+ const before = [
+ channel('a', '2026-01-01T10:00:00Z', [
+ { createdAt: '2026-01-01T10:00:00Z' },
+ ]),
+ channel('b', '2026-01-01T10:05:00Z', [
+ { createdAt: '2026-01-01T10:05:00Z' },
+ ]),
+ channel('c', '2026-01-01T10:02:00Z'),
+ ];
+ expect(ids(sortChannels(before, 'updated_at'))).toEqual(['b', 'c', 'a']);
+
+ const after = [
+ channel('a', '2026-01-01T10:10:00Z', [
+ { createdAt: '2026-01-01T10:00:00Z' },
+ { createdAt: '2026-01-01T10:10:00Z' },
+ ]),
+ before[1],
+ before[2],
+ ];
+ expect(ids(sortChannels(after, 'updated_at'))).toEqual(['b', 'c', 'a']);
+ });
+
+ it('moves a channel up when it first goes unread', () => {
+ const channels = [
+ channel('a', '2026-01-01T10:10:00Z', [
+ { createdAt: '2026-01-01T10:10:00Z' },
+ ]),
+ channel('b', '2026-01-01T10:05:00Z'),
+ ];
+ expect(ids(sortChannels(channels, 'updated_at'))).toEqual(['a', 'b']);
+ });
+
+ it('sorts read channels by their latest activity', () => {
+ const channels = [
+ channel('a', '2026-01-01T10:10:00Z', [
+ { createdAt: '2026-01-01T10:00:00Z', state: 'seen' },
+ ]),
+ channel('b', '2026-01-01T10:05:00Z'),
+ ];
+ expect(ids(sortChannels(channels, 'updated_at'))).toEqual(['a', 'b']);
+ });
+});
diff --git a/apps/web/src/features/channels-view/core/channel-sort.ts b/apps/web/src/features/channels-view/core/channel-sort.ts
new file mode 100644
index 0000000000..1a696ef6f3
--- /dev/null
+++ b/apps/web/src/features/channels-view/core/channel-sort.ts
@@ -0,0 +1,44 @@
+import {
+ compareDateAsc,
+ compareDateDesc,
+ type DateValue,
+} from '@core/util/date';
+import type { ChannelEntity } from '@entity';
+import { match } from 'ts-pattern';
+import type { ChannelListSort } from '../types';
+
+/**
+ * When a channel last moved in the `updated_at` sort. An unread channel holds
+ * the place it took when it first went unread: the rail shows only a dot for
+ * unread, so a later message moving it up would look like it jumped for no
+ * reason. It takes its latest place again once it is read.
+ */
+function activitySortDate(
+ channel: ChannelEntity
+): DateValue | null | undefined {
+ const oldestUnseen = channel.unreadNotifications
+ ?.filter((notification) => notification.state === 'unseen')
+ .map((notification) => notification.createdAt)
+ .sort(compareDateAsc)[0];
+ if (oldestUnseen === undefined) return channel.updatedAt;
+ if (!channel.updatedAt) return oldestUnseen;
+ return compareDateAsc(oldestUnseen, channel.updatedAt) < 0
+ ? oldestUnseen
+ : channel.updatedAt;
+}
+
+export function sortChannels(
+ channels: readonly ChannelEntity[],
+ sort: ChannelListSort
+): ChannelEntity[] {
+ const sortDate = (channel: ChannelEntity) =>
+ match(sort)
+ .with('created_at', () => channel.createdAt)
+ .with('viewed_at', () => channel.viewedAt)
+ .with('updated_at', () => activitySortDate(channel))
+ .exhaustive();
+
+ return channels
+ .slice()
+ .sort((left, right) => compareDateDesc(sortDate(left), sortDate(right)));
+}
diff --git a/apps/web/src/features/channels-view/queries.ts b/apps/web/src/features/channels-view/queries.ts
index d2f2c4b57f..a650994250 100644
--- a/apps/web/src/features/channels-view/queries.ts
+++ b/apps/web/src/features/channels-view/queries.ts
@@ -6,7 +6,6 @@ import {
} from '@app/features/next-soup/filters/filter-store';
import { useChannelsContext } from '@core/context/channels';
import { useUserId } from '@core/context/user';
-import { compareDateDesc } from '@core/util/date';
import { fireAndForget } from '@core/util/fireAndForget';
import {
type ChannelEntity,
@@ -20,6 +19,7 @@ import {
useSoupAstItemsQuery,
} from '@queries/soup/items';
import { type Accessor, createEffect, createMemo } from 'solid-js';
+import { sortChannels } from './core/channel-sort';
import { channelThreadsQueryArgs } from './core/channel-threads-query';
import {
applySupportChannelVisibility,
@@ -236,22 +236,6 @@ export function resolveSelectedChannel(
);
}
-function sortChannels(
- channels: readonly ChannelEntity[],
- sort: ChannelListSort
-): ChannelEntity[] {
- const sortDate = (channel: ChannelEntity) =>
- sort === 'created_at'
- ? channel.createdAt
- : sort === 'viewed_at'
- ? channel.viewedAt
- : channel.updatedAt;
-
- return channels
- .slice()
- .sort((left, right) => compareDateDesc(sortDate(left), sortDate(right)));
-}
-
/**
* The Threads rail: channels and DMs holding threads the user takes part in.
* Channels have no thread filter, so the rail pages through the user's