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

100% Statements 35/35
100% Branches 34/34
100% Functions 11/11
100% Lines 30/30

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197                                                  4x   4x 137x   123x   116x   115x 7x         108x 7x         101x     4x 73x     137x           4x 65x 57x             8x   65x     16x                   57x   49x   42x     4x             57x 57x   57x                                                                     65x     7x                         20x                                                 30x                                        
import { cn } from 'cn'
import { Pin, SmilePlus } from 'lucide-react'
import type { ReactElement } from 'react'
import { Avatar, AvatarBadge, AvatarFallback, AvatarGroup } from '@/components/Avatar'
import { presenceClass } from '@/helpers/presence'
import { toneClass } from '@/helpers/tone'
import { initials } from '@chattra/utils/string'
import { parseBlocks, type Block } from '../../lib/blocks'
import { parseInline, type InlinePart } from '../../lib/inline'
import { formatClock, formatLastReply } from '../../lib/time'
import type { Message, Person } from '../../types'
import { MessageToolbar } from '../MessageToolbar'
 
interface MessageItemProps {
  message: Message
  author: Person
  /** The people who replied in the thread, for the stacked avatars. */
  repliers: Person[]
  /** A follow-up from the same person: no avatar, no name. */
  isCompact: boolean
  /** The time "today" and "yesterday" are measured from. */
  now: number
}
 
const CHIP =
  'inline-flex h-6.5 items-center gap-1.5 rounded-full border bg-glass-strong px-2 text-sm outline-none hover:border-primary-line focus-visible:ring-3 focus-visible:ring-ring aria-pressed:border-primary-line aria-pressed:bg-accent'
 
const Inline = ({ part }: { part: InlinePart }): ReactElement => {
  if (part.type === 'bold') return <strong className="font-bold">{part.text}</strong>
 
  if (part.type === 'italic') return <em>{part.text}</em>
 
  if (part.type === 'strike') return <s>{part.text}</s>
 
  if (part.type === 'code') {
    return (
      <code className="rounded-md border bg-muted px-1 py-px font-mono text-sm">{part.text}</code>
    )
  }
 
  if (part.type === 'mention') {
    return (
      <span className="rounded-md bg-mention-bg px-1 font-semibold text-mention">{part.text}</span>
    )
  }
 
  return <>{part.text}</>
}
 
const InlineText = ({ text }: { text: string }): ReactElement => {
  return (
    <>
      {parseInline(text).map((part, index) => (
        <Inline key={index} part={part} />
      ))}
    </>
  )
}
 
const BlockView = ({ block }: { block: Block }): ReactElement => {
  if (block.type === 'paragraph') {
    return (
      <p className="leading-relaxed break-words whitespace-pre-wrap">
        <InlineText text={block.text} />
      </p>
    )
  }
 
  const List = block.isOrdered ? 'ol' : 'ul'
 
  return (
    <List className={cn('my-0.5 pl-6', block.isOrdered ? 'list-decimal' : 'list-disc')}>
      {block.items.map((item, index) => (
        <li key={index} className="leading-relaxed break-words">
          <InlineText text={item} />
        </li>
      ))}
    </List>
  )
}
 
// The row's own tint: a mention beats a pin, and a plain row only lights up under the pointer.
function getRowTint(message: Message): string {
  if (message.mentionsMe) return 'bg-mention-row'
 
  if (message.isPinned) return 'bg-pin-row'
 
  return 'hover:bg-hover'
}
 
export const MessageItem = ({
  message,
  author,
  repliers,
  isCompact,
  now,
}: MessageItemProps): ReactElement => {
  const time = formatClock(message.sentAt)
  const { thread } = message
 
  return (
    <article
      aria-label={`${author.name}, ${time}`}
      className={cn(
        'group/message relative grid grid-cols-[36px_minmax(0,1fr)] gap-x-3 px-3 has-focus-visible:ring-2 has-focus-visible:ring-primary has-focus-visible:ring-inset md:px-5',
        isCompact ? 'py-px' : 'py-1.5',
        getRowTint(message),
      )}
    >
      {isCompact ? (
        <time className="pt-1 text-right text-2xs text-transparent tabular-nums group-hover/message:text-faint">
          {time}
        </time>
      ) : (
        <Avatar>
          <AvatarFallback className={toneClass(author.name)}>
            {initials(author.name)}
          </AvatarFallback>
          <AvatarBadge className={presenceClass(author.presence)} />
        </Avatar>
      )}
      <div className="min-w-0">
        {message.isPinned ? (
          <p className="flex items-center gap-1.5 text-xs font-semibold text-muted-foreground">
            <Pin aria-hidden="true" className="size-3.5" />
            Pinned
          </p>
        ) : null}
        {isCompact ? null : (
          <p className="flex items-baseline gap-2">
            <span className="font-bold">{author.name}</span>
            <time className="text-xs text-faint tabular-nums">{time}</time>
          </p>
        )}
        {parseBlocks(message.body).map((block, index) => (
          <BlockView key={index} block={block} />
        ))}
        {message.attachments.map((attachment) => (
          <img
            key={attachment.id}
            src={attachment.src}
            alt={attachment.name}
            width={attachment.width}
            height={attachment.height}
            className="mt-2 block h-auto w-full max-w-90 rounded-xl border"
          />
        ))}
        {/* shortcut: reactions, add-reaction and the replies link do nothing yet; reactions and threads are M3. */}
        {message.reactions.length > 0 ? (
          <div className="mt-1 flex flex-wrap gap-1">
            {message.reactions.map((reaction) => (
              <button
                key={reaction.emoji}
                type="button"
                aria-pressed={reaction.isMine}
                aria-label={`${reaction.emoji} ${reaction.count}`}
                className={CHIP}
              >
                <span aria-hidden="true">{reaction.emoji}</span>
                <span aria-hidden="true" className="text-xs font-bold text-muted-foreground">
                  {reaction.count}
                </span>
              </button>
            ))}
            <button type="button" aria-label="Add reaction" className={cn(CHIP, 'px-1.5')}>
              <SmilePlus className="size-4 text-muted-foreground" />
            </button>
          </div>
        ) : null}
        {thread ? (
          <button
            type="button"
            className="mt-1 flex max-w-full items-center gap-2 rounded-lg py-0.5 pr-2 pl-0.5 text-sm outline-none hover:bg-card focus-visible:ring-3 focus-visible:ring-ring"
          >
            <AvatarGroup aria-hidden="true" className="shrink-0 -space-x-1.5">
              {repliers.map((replier) => (
                <Avatar key={replier.id} size="sm">
                  <AvatarFallback className={toneClass(replier.name)}>
                    {initials(replier.name)}
                  </AvatarFallback>
                </Avatar>
              ))}
            </AvatarGroup>
            <span className="shrink-0 font-bold text-primary">
              {`${thread.replyCount} ${thread.replyCount === 1 ? 'reply' : 'replies'}`}
            </span>{' '}
            <span className="min-w-0 truncate text-muted-foreground">
              {`Last reply ${formatLastReply(thread.lastReplyAt, now)}`}
            </span>
          </button>
        ) : null}
      </div>
      <MessageToolbar />
    </article>
  )
}