| 1 | import type { RoomSummary } from './types'; |
| 2 | |
| 3 | export interface PaneBadge { |
| 4 | count: number; |
| 5 | mention: boolean; |
| 6 | } |
| 7 | |
| 8 | export interface PaneBadges { |
| 9 | rail: PaneBadge; |
| 10 | channels: PaneBadge; |
| 11 | } |
| 12 | |
| 13 | /** DMs and mentions, each counted on whichever folded pane reaches its room in the fewest clicks. */ |
| 14 | export function paneBadges( |
| 15 | rooms: RoomSummary[], |
| 16 | channelIds: Set<string>, |
| 17 | openRoomId: string | null, |
| 18 | muted: (room: RoomSummary) => boolean |
| 19 | ): PaneBadges { |
| 20 | const rail = { count: 0, mention: false }; |
| 21 | const channels = { count: 0, mention: false }; |
| 22 | for (const room of rooms) { |
| 23 | if (room.space || room.membership !== 'Joined' || room.room_id === openRoomId || muted(room)) continue; |
| 24 | const count = room.direct |
| 25 | ? room.notification_mode === 'MentionsAndKeywordsOnly' ? room.unread_mentions : room.unread_messages + room.unread_mentions |
| 26 | : room.unread_mentions; |
| 27 | if (count === 0) continue; |
| 28 | const badge = channelIds.has(room.room_id) ? channels : rail; |
| 29 | badge.count += count; |
| 30 | badge.mention ||= room.unread_mentions > 0; |
| 31 | } |
| 32 | return { rail, channels }; |
| 33 | } |