All files / apps / web/src / features / conversation / components / MessageList index.tsx

100% Statements 27/27
100% Branches 25/25
100% Functions 8/8
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                        3x   3x 24x   24x                                 3x 13x 13x     13x 13x       20x   20x   10x   2x     13x 2x             11x           71x   51x   47x       158x   98x                      
import { useEffect, useRef, type ReactElement } from 'react'
import { buildTimeline, dayOffset } from '../../lib/timeline'
import type { ConversationMessages, Person } from '../../types'
import { MessageItem } from '../MessageItem'
 
interface MessageListProps {
  conversation: ConversationMessages
  people: Person[]
  /** The time "Today" and "Yesterday" are measured from. */
  now: number
}
 
const UNKNOWN: Person = { id: 'unknown', name: '?', presence: 'offline' }
 
const Divider = ({ label, isNew = false }: { label: string; isNew?: boolean }): ReactElement => {
  const line = isNew ? 'bg-destructive' : 'bg-border'
 
  return (
    <div role="separator" aria-label={label} className="flex items-center gap-3 px-5 py-2">
      <span className={`h-px flex-1 ${line}`} />
      <span
        className={
          isNew
            ? 'text-2xs font-bold tracking-wider text-destructive uppercase'
            : 'text-xs font-bold text-muted-foreground'
        }
      >
        {label}
      </span>
      {isNew ? null : <span className={`h-px flex-1 ${line}`} />}
    </div>
  )
}
 
export const MessageList = ({ conversation, people, now }: MessageListProps): ReactElement => {
  const scroller = useRef<HTMLDivElement>(null)
  const count = conversation.messages.length
 
  // A chat opens at its newest message and follows new ones.
  useEffect(() => {
    if (scroller.current) scroller.current.scrollTop = scroller.current.scrollHeight
  }, [count])
 
  function dayLabel(dayStart: number): string {
    const offset = dayOffset(dayStart, now)
 
    if (offset === 0) return 'Today'
 
    if (offset === 1) return 'Yesterday'
 
    return new Intl.DateTimeFormat('en', { dateStyle: 'full' }).format(dayStart)
  }
 
  if (count === 0) {
    return (
      <div className="grid flex-1 place-items-center p-6 text-muted-foreground">
        No messages yet. Send the first one.
      </div>
    )
  }
 
  return (
    // role="log": a screen reader announces a message that arrives, without moving focus.
    <div ref={scroller} role="log" aria-label="Messages" className="min-h-0 flex-1 overflow-y-auto">
      {/* Few messages sit just above the composer, as in any chat. */}
      <div className="flex min-h-full flex-col justify-end pt-2 pb-3.5">
        {buildTimeline(conversation.messages, conversation.lastReadMessageId).map((row) => {
          if (row.type === 'day') return <Divider key={row.key} label={dayLabel(row.dayStart)} />
 
          if (row.type === 'new') return <Divider key={row.key} label="New messages" isNew />
 
          return (
            <MessageItem
              key={row.key}
              message={row.message}
              author={people.find((person) => person.id === row.message.authorId) ?? UNKNOWN}
              repliers={(row.message.thread?.replierIds ?? []).map(
                (id) => people.find((person) => person.id === id) ?? UNKNOWN,
              )}
              isCompact={row.isCompact}
              now={now}
            />
          )
        })}
      </div>
    </div>
  )
}