// Unread rules for the chat badges, ported from a Golem15 SPA's // chatUnread.ts. The Polish copy helpers are kept for the SPA's tests; the // layer's components word the badges through i18n (chat.unread.*). import type { ChatMessage } from './types' import { count, plural } from './format' export interface ReadInputs { /** The conversation is the active one. */ active: boolean /** The panel is on screen: the desktop column, or the Czat tab selected. */ visible: boolean /** document.hasFocus(). */ focused: boolean /** The list is within 48px of its bottom. */ atBottom: boolean } /** shouldMarkRead is D-18: a conversation is read only when it is really seen. */ export function shouldMarkRead({ active, visible, focused, atBottom }: ReadInputs): boolean { return active && visible && focused && atBottom } /** badgeText is the visible count: none for 0, capped above 99. */ export function badgeText(n: number): string { if (!(n > 0)) return '' return n > 99 ? '99+' : String(n) } /** unreadLabel is the sr-only text of an unread badge. */ export function unreadLabel(n: number): string { return count(n, 'nieprzeczytana wiadomość', 'nieprzeczytane wiadomości', 'nieprzeczytanych wiadomości') } /** mentionLabel is the sr-only text of the @ badge. */ export function mentionLabel(n: number): string { if (n === 1) return 'wzmianka o Tobie' return `${n} ${plural(n, 'wzmianka', 'wzmianki', 'wzmianek')} o Tobie` } /** * dividerMessageId places "Nowe wiadomości" before the first unread message: * the first of the last `unread` messages from someone else. With fewer * loaded than that, the oldest loaded one from someone else. */ export function dividerMessageId(messages: readonly ChatMessage[], unread: number, meId: number | null): number | null { if (!(unread > 0)) return null let left = unread let found: number | null = null for (let i = messages.length - 1; i >= 0 && left > 0; i--) { const m = messages[i] if (m.sender_id === meId) continue found = m.id left-- } return found } /** typingText is the typing row (D-15): zero, one, two, or many people. */ export function typingText(names: readonly string[]): string { if (names.length === 0) return '' if (names.length === 1) return `${names[0]} pisze…` if (names.length === 2) return `${names[0]} i ${names[1]} piszą…` return 'Kilka osób pisze…' }