All files / src/features/Minibuilders/Conversations/Replies RepliesChat.tsx

70.96% Statements 22/31
33.33% Branches 8/24
62.5% Functions 5/8
75.86% Lines 22/29

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 198 199 200 201 202 203 204 205 206 207 208 209 210 211                                          21x           5x 5x   5x 2x     5x 5x   5x   5x 5x 5x 3x 3x     5x 11x               5x 5x   5x                                                                         5x                                                                                                                                             11x     11x                                                                           21x      
import { ConversationsEmptyStateIcons } from "@assets/icons";
import { Button } from "@common/Button";
import { Text, TruncateText } from "@common/Text";
import { Box, Stack, styled } from "@mui/material";
import { TelegramLogoIcon } from "@phosphor-icons/react";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import {
  selectConversationTopic,
  selectReplies,
  selectThreads,
  setConversationTopic,
} from "@redux/slices/conversations";
import moment from "moment";
import { PLATFORM_TIMEZONE } from "@utils/timezones";
import { useEffect, useRef, useState } from "react";
import Comment from "../Chat/Comment";
import ComponentWithAnimation from "../Chat/ComponentWithAnimation";
import TextDivider from "../components/TextDivider";
import { getGlobalTopic } from "../hooks/useConversationsModal";
import ConversationsEmptyState from "../Modal/ConversationsEmptyState";
 
const topicNameMap = {
  risk_monitor: "Risk Activity",
  underwriting: "Underwriting",
};
 
function RepliesChat({ isMobileView }: { isMobileView: boolean; data: any }) {
  const threads = useAppSelector(selectThreads);
  const [isRendered, setIsRendered] = useState<boolean>(false);
 
  useEffect(() => {
    setIsRendered(true);
  }, []);
 
  const { commentId, index } = useAppSelector(selectReplies);
  const scrollRef = useRef<any>(null);
 
  Iif (typeof index !== "number") return <></>;
 
  const thread = threads[index];
  const { queryObject } = useAppSelector(selectConversationTopic);
  useEffect(() => {
    Eif (scrollRef.current) {
      scrollRef.current.scrollIntoView({ behavior: "smooth" });
    }
  }, [thread.messages.length]);
  const messages = thread?.messages?.filter(
    (message) => message?.body !== thread.originalThread?.messageBody,
  );
  const {
    createdAt,
    authorFirstName,
    authorLastName,
    messageBody,
    subjectAccName,
  } = thread.originalThread || {};
  const dispatch = useAppDispatch();
 
  const handleReadMore = async () => {
    const { data: topics } = await getGlobalTopic({});
    const isRiskMonitor = thread?.topicName === "risk_activity";
    const name = isRiskMonitor ? "risk_monitor" : "underwriting";
    const pathName = topicNameMap[name];
 
    const topic = topics?.find(
      (item: any) => item?.Type === "activity" && item?.Name === name,
    );
 
    dispatch(
      setConversationTopic({
        isOpen: true,
        isOpenedFromSidePanel: false,
        queryObject: {
          ...queryObject,
          id: topic?.ID,
          name: thread?.originalThread?.subjectAccName || "",
          tabs: "Activity",
          paths: [
            {
              pathID: topic?.ID,
              pathName: pathName || "",
              avatars: [],
            },
            {
              avatars: [],
              isConversation: true,
              pathName: thread?.originalThread?.title || "",
              pathID: thread?.originalThread?.id,
              hideInputs: ["module", "subject"],
            },
          ],
        },
      }),
    );
  };
  return (
    <Box sx={{ flex: 1 }}>
      {!thread ? (
        <ConversationsEmptyState
          icon={<ConversationsEmptyStateIcons />}
          message="No replies..."
        />
      ) : (
        <Box>
          {thread?.originalThread && (
            <Stack
              flexDirection="row"
              borderRadius="8px"
              bgcolor="#FFFFFF"
              mx="16px"
              p="12px 16px"
              alignItems="self-start"
              gap="8px"
            >
              <Stack
                height="24px"
                width="24px"
                justifyContent="center"
                alignItems="center"
                bgcolor="#6D9CF8"
                borderRadius="100%"
                flex={1}
              >
                <TelegramLogoIcon color="#F8F8F6" weight="fill" size={16} />
              </Stack>
              <Box flex={15}>
                {createdAt && (
                  <Text fontWeight="book" fontSize="12px" color="#8F8F8F">
                    {moment.unix(createdAt).tz(PLATFORM_TIMEZONE).format("DD MMM YYYY, HH:mm")}
                  </Text>
                )}
                <Text
                  mb="12px"
                  fontWeight="regular"
                  color="#403D3D"
                  fontSize="14px"
                >
                  Conversation started with{" "}
                  <TextSpan>{subjectAccName}</TextSpan> underwriting by
                  <TextSpan> {`${authorFirstName} ${authorLastName}`}</TextSpan>
                </Text>
                <TruncateText
                  fontWeight="book"
                  fontSize="14px"
                  color="#575353"
                  lineClamp={2}
                >
                  {messageBody}
                </TruncateText>
                <Button
                  sx={{
                    margin: 0,
                    padding: 0,
                    color: "#6D9CF8",
                    fontSize: "14px",
                  }}
                  background="tertiary"
                  variant="text"
                  onClick={handleReadMore}
                >
                  Read All
                </Button>
              </Box>
            </Stack>
          )}
          {messages?.map((comment, index) => {
            const commentCreatedAt = Math.floor(
              new Date(comment?.createdAt).getTime(),
            );
            return (
              <ComponentWithAnimation
                id={comment.id}
                shouldAnimate={!isRendered}
                index={index}
                key={index}
              >
                <Comment
                  index={index}
                  isError={thread.isError}
                  isSending={thread.isSending}
                  isMobileView={isMobileView}
                  parentThreadAuthorID={thread.authorAccID}
                  {...comment}
                  parentThreadId={thread.id}
                  createdAt={commentCreatedAt}
                  isReplyComment
                />
                {index === 0 && (
                  <TextDivider
                    color="neutral.70"
                    wrapperProps={{ gap: 1, padding: 1 }}
                  >
                    {thread.messages.length - 1} replies
                  </TextDivider>
                )}
              </ComponentWithAnimation>
            );
          })}
          <Box ref={scrollRef} />
        </Box>
      )}
    </Box>
  );
}
 
export default RepliesChat;
 
const TextSpan = styled("span")(() => ({
  color: "#292727",
}));