All files / src/features/Minibuilders/Conversations/Chat ChatMainContent.tsx

93.75% Statements 15/16
81.48% Branches 22/27
100% Functions 4/4
100% Lines 15/15

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                                                                                  21x           10x   10x 8x 8x     10x       30x   30x   30x   30x           30x                                                                     21x                       30x 10x   20x                                                      
import { ConversationsEmptyStateIcons } from "@assets/icons";
import { Box } from "@mui/material";
import { isEmpty } from "lodash";
import { Dispatch, SetStateAction, useEffect, useRef } from "react";
import ConversationsEmptyState from "../Modal/ConversationsEmptyState";
import { OriginalThread, TThreadInfo } from "../types";
import {
  TMentionedUser,
  ThreadInfos,
  getThreadInfos,
} from "../utils/functions";
import Comment from "./Comment";
import ComponentWithAnimation from "./ComponentWithAnimation";
import ThreadSkeleton from "./ThreadSkeleton";
 
type Props = {
  setMentionedUser: Dispatch<SetStateAction<TMentionedUser>>;
  topicId: number | undefined;
  parentThreadName: string;
  merchantId: number;
  hasUnreadMessages: boolean;
  isMobileView: boolean;
  threads: TThreadInfo[];
  isLoading: boolean;
  isRefetching: boolean;
  data: any;
  isExpanded: boolean;
};
 
type TListThreadProps = {
  threads: TThreadInfo[];
  isRendered?: boolean;
  topicId: number | undefined;
  parentThreadName: string;
  setMentionedUser: Dispatch<SetStateAction<TMentionedUser>>;
  isMobileView: boolean;
  hasUnreadMessages: boolean;
  merchantId: number;
  originalThread?: OriginalThread;
};
 
const ListThreads = ({
  threads,
  topicId,
  parentThreadName,
  setMentionedUser,
}: TListThreadProps) => {
  const scrollRef = useRef<any>(null);
 
  useEffect(() => {
    Eif (scrollRef.current) {
      scrollRef.current.scrollIntoView({ behavior: "smooth" });
    }
  }, [threads.length]);
  return (
    <Box sx={{ paddingBottom: "20px" }}>
      {threads.length > 0 &&
        threads.map((thread, index) => {
          Iif (!thread) return;
          const { subjectAccName, authorFirstName, authorLastName } =
            thread?.originalThread || {};
          const { comment, commentCreatedAt, threadUpdatedAt, userAvatars } =
            getThreadInfos(thread) as ThreadInfos;
 
          const customBody = `Conversation started with ${subjectAccName} ${
            thread?.topicName === "risk_activity"
              ? "monitoring risk by"
              : "underwriting by"
          } ${authorFirstName} ${authorLastName} `;
 
          return (
            comment && (
              <ComponentWithAnimation
                id={comment.id}
                key={comment.id}
                shouldAnimate={false}
                index={index}
              >
                <Comment
                  index={index}
                  parentThreadAuthorID={thread.authorAccID}
                  isError={thread.isError}
                  parentThreadId={thread.id}
                  isSending={thread.isSending}
                  topicId={topicId}
                  parentThreadName={parentThreadName}
                  userAvatars={userAvatars}
                  numberOfReplies={thread.messageCount - 1}
                  lastUpdatedAt={threadUpdatedAt}
                  setMentionedUser={setMentionedUser}
                  {...comment}
                  {...(!isEmpty(thread?.originalThread) && {
                    body: customBody,
                  })}
                  createdAt={commentCreatedAt}
                />
              </ComponentWithAnimation>
            )
          );
        })}
      <Box ref={scrollRef} />
    </Box>
  );
};
 
const ChatMainContent = ({
  setMentionedUser,
  topicId,
  parentThreadName,
  merchantId,
  hasUnreadMessages,
  isMobileView,
  threads,
  isLoading,
  isRefetching,
  ...rest
}: Props) => {
  if (isLoading || (isRefetching && threads.length === 0) || !topicId)
    return <ThreadSkeleton length={rest.isExpanded ? 7 : 3} />;
 
  return (
    <>
      {!rest.data?.data ||
      (threads.length === 1 && threads[0].messages.length === 0) ? (
        <ConversationsEmptyState
          icon={<ConversationsEmptyStateIcons />}
          message="No messages yet..."
          isThread
        />
      ) : (
        <>
          <ListThreads
            threads={threads}
            parentThreadName={parentThreadName}
            isMobileView={isMobileView}
            hasUnreadMessages={hasUnreadMessages}
            merchantId={merchantId}
            setMentionedUser={setMentionedUser}
            topicId={topicId}
          />
        </>
      )}
    </>
  );
};
 
export default ChatMainContent;