All files / src/features/GiveConversation/components Threads.tsx

90.9% Statements 20/22
80.76% Branches 21/26
100% Functions 5/5
100% Lines 19/19

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                                          23x 21x 21x 21x 21x 5x 5x     21x 5x   21x   14x 5x                               9x                                       45x   45x             45x             45x 99x     45x                                                            
import { useConversationContext } from "../ConversationProvider";
import GiveEmptyStateWrapper from "@shared/EmptyState/GiveEmptyStateWrapper";
import ConversationsSkeleton from "@components/CustomSkeletons/ConversationsSkeleton";
import { TThreadItem } from "../types";
import ThreadItem from "./ThreadItem";
import { useInView } from "react-intersection-observer";
import { useEffect } from "react";
import { Box, Stack } from "@mui/material";
import { every, isEmpty } from "lodash";
import { getCounterVariant } from "../utils";
 
export default function Threads() {
  const {
    fetchNextPageThreadList,
    tab,
    currentThread,
    showMentions,
    threads,
    isThreadListLoading,
    hasNextPageThreadList,
    isFetchingNextPageThreadList,
  } = useConversationContext();
  const isTeamTab = tab === "team";
  const hasMentionsSwitch = isTeamTab && !currentThread?.id;
  const [ref, inView] = useInView();
  const getEmptySection = () => {
    Iif (hasMentionsSwitch && showMentions) return "no-mentions";
    return isTeamTab ? "no-team-conversations" : "no-merchant-conversations";
  };
 
  useEffect(() => {
    Iif (inView) fetchNextPageThreadList?.();
  }, [inView]);
  if (isThreadListLoading) return <ConversationsSkeleton />;
 
  if (!threads?.length) {
    return (
      <Stack
        alignItems="center"
        justifyContent="center"
        width="100%"
        height="90%"
        data-testid="give-conversation-empty-state-thread-list"
        sx={{
          paddingTop: hasMentionsSwitch ? "0px" : "57px",
        }}
      >
        <GiveEmptyStateWrapper section={getEmptySection()} isEmpty />
      </Stack>
    );
  }
 
  return (
    <Box pb="80px">
      {threads.map((thread: TThreadItem) => {
        const {
          id,
          title,
          lastMessageBody,
          lastMessageSentAt,
          unreadMessagesCount,
          isMentioned,
          lastAuthorFirstName,
          lastAuthorLastName,
          lastAuthorAvatarImageURL,
          secondLastAuthorFirstName,
          secondLastAuthorLastName,
          secondLastAuthorAvatarImageURL,
          lastAuthorEmail,
          secondLastAuthorEmail,
          taskID,
          didSubjectAccReply,
        } = thread;
 
        const lastAuthor = {
          firstName: lastAuthorFirstName,
          lastName: lastAuthorLastName,
          imageURL: lastAuthorAvatarImageURL,
          email: lastAuthorEmail,
        };
 
        const secondLastAuthor = {
          firstName: secondLastAuthorFirstName,
          lastName: secondLastAuthorLastName,
          imageURL: secondLastAuthorAvatarImageURL,
          email: secondLastAuthorEmail,
        };
 
        const isSecondLastAuthorEmpty = every(secondLastAuthor, (value) =>
          isEmpty(value),
        );
 
        return (
          <ThreadItem
            key={id}
            id={id}
            title={title}
            body={lastMessageBody}
            date={lastMessageSentAt}
            count={unreadMessagesCount}
            counterVariant={getCounterVariant({
              // For team tab, we should not show red icon for merchant reply
              didMerchantReply: isTeamTab ? false : didSubjectAccReply,
              isMentioned: isMentioned,
              unreadMessagesCount: unreadMessagesCount,
            })}
            primaryUser={
              isSecondLastAuthorEmpty ? lastAuthor : secondLastAuthor
            }
            secondaryUser={
              !isSecondLastAuthorEmpty ? lastAuthor : secondLastAuthor
            }
            taskID={taskID}
          />
        );
      })}
 
      {hasNextPageThreadList && <div ref={ref} />}
      {isFetchingNextPageThreadList && <ConversationsSkeleton />}
    </Box>
  );
}