feat(web): add sortDmChannels utility with unread-first sorting

This commit is contained in:
Jannis Braun
2026-04-02 17:48:24 +02:00
parent 03667947aa
commit 68fe04ef58
2 changed files with 119 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
import type { DmChannel } from '@backspace/shared';
/**
* Sort DM channels: unread first (by recency), then read (by recency).
* Does not mutate the input array.
*
* @param dmChannels - The DM channels to sort
* @param unreadChannels - Set of channel IDs that have unread messages
* @param currentChannelId - The currently active channel (excluded from unread group)
*/
export function sortDmChannels(
dmChannels: DmChannel[],
unreadChannels: Set<string>,
currentChannelId: string | null,
): DmChannel[] {
const getTime = (dm: DmChannel) => dm.lastMessage?.createdAt ?? dm.createdAt;
const isUnread = (dm: DmChannel) => unreadChannels.has(dm.id) && dm.id !== currentChannelId;
const unread: DmChannel[] = [];
const read: DmChannel[] = [];
for (const dm of dmChannels) {
if (isUnread(dm)) {
unread.push(dm);
} else {
read.push(dm);
}
}
const byRecency = (a: DmChannel, b: DmChannel) => getTime(b) - getTime(a);
unread.sort(byRecency);
read.sort(byRecency);
return [...unread, ...read];
}