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

96.42% Statements 27/28
93.75% Branches 15/16
100% Functions 7/7
100% Lines 20/20

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                                2x 22x 22x 22x 22x   8x             22x 19x 17x   9x 26x   9x         2x 2x   2x                       2x       8x 80x 41x   22x                  
import { useQuery, useQueryClient } from '@tanstack/react-query'
import type { ReactElement } from 'react'
import { NotFound, RoutePending } from '@/features/shell'
import { useNow } from '@chattra/hooks/use-now'
import { fetchMessages, fetchSidebar, messagesKey, sidebarKey } from '../../api'
import type { ConversationMessages } from '../../types'
import { Composer } from '../Composer'
import { ConversationHeader } from '../ConversationHeader'
import { ConversationTabs } from '../ConversationTabs'
import { MessageList } from '../MessageList'
 
interface ConversationViewProps {
  conversationId: string
}
 
// One conversation: header, messages, composer (the prototype's main pane).
export const ConversationView = ({ conversationId }: ConversationViewProps): ReactElement => {
  const queryClient = useQueryClient()
  const now = useNow()
  const sidebar = useQuery({ queryKey: sidebarKey, queryFn: fetchSidebar })
  const messages = useQuery({
    queryKey: messagesKey(conversationId),
    queryFn: () => fetchMessages(conversationId),
    // shortcut: never refetch while the fixture backs this, or the refetch drops sent messages;
    // M2 drops it when messages come from the API.
    staleTime: Infinity,
  })
 
  // The router's error boundary shows RouteError with the request id.
  if (sidebar.isError) throw sidebar.error
  if (messages.isError) throw messages.error
  if (!sidebar.data || !messages.data) return <RoutePending />
 
  const { me, people } = sidebar.data
  const conversation = sidebar.data.conversations.find((item) => item.id === conversationId)
 
  if (!conversation) return <NotFound />
 
  // shortcut: a sent message lives only in the query cache and is gone on reload; M2 posts it
  // with client_msg_id (design 4.5).
  function send(body: string): void {
    queryClient.setQueryData<ConversationMessages>(messagesKey(conversationId), (current) => {
      Iif (!current) return current
 
      const sent = {
        id: crypto.randomUUID(),
        authorId: me.id,
        sentAt: Date.now(),
        body,
        reactions: [],
        thread: null,
        attachments: [],
        isPinned: false,
        mentionsMe: false,
      }
 
      return { lastReadMessageId: null, messages: [...current.messages, sent] }
    })
  }
 
  const isChannel = conversation.kind === 'channel' || conversation.kind === 'private'
  const members = people.filter((person) => conversation.memberIds.includes(person.id))
  const pinCount = messages.data.messages.filter((item) => item.isPinned).length
 
  return (
    <>
      <ConversationHeader conversation={conversation} members={members} />
      <ConversationTabs pinCount={pinCount} />
      <MessageList conversation={messages.data} people={people} now={now} />
      <Composer target={isChannel ? `#${conversation.name}` : conversation.name} onSend={send} />
    </>
  )
}