All files / src/features/Minibuilders/Conversations/Modal/Sections/Table ConversationsTableBody.tsx

75% Statements 6/8
70% Branches 14/20
75% Functions 3/4
85.71% Lines 6/7

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                                          21x                 11x 11x 11x     33x   6x                                                                                                                                                
import { Text, TruncateText } from "@common/Text";
import { Stack, TableBody } from "@mui/material";
import { TableCell, TableRow } from "@mui/material";
import { formatDistanceToNowStrict, fromUnixTime } from "date-fns";
import ImagesStacker from "features/Minibuilders/Conversations/utils/ImagesStacker";
import TotalUnread from "./TotalUnread";
import { TConversationTopic } from "features/Minibuilders/Conversations/hooks/useListTopics";
import { ReactNode } from "react";
import { ChatsCircleIcon, TelegramLogoIcon } from "@phosphor-icons/react";
import { getGlobalTopicLabel } from "features/Minibuilders/Conversations/hooks/useConversationsModal";
 
type Props = {
  isMobileView: boolean;
  handleClick: (item: any, index: number) => void;
  data: TConversationTopic[];
  renderOnCondition?: () => ReactNode;
  condition?: boolean;
  isInternal?: boolean;
  hasUnreadActivity?: boolean;
};
 
const ConversationsTableBody = ({
  isMobileView,
  data,
  handleClick,
  renderOnCondition,
  condition,
  isInternal,
  hasUnreadActivity,
}: Props) => {
  Iif (condition && renderOnCondition) return <>{renderOnCondition()}</>;
  const ICON = isInternal ? ChatsCircleIcon : TelegramLogoIcon;
  return (
    <TableBody>
      {data?.map((item, index) => (
        <TableRow
          key={item.id}
          onClick={(e) => handleClick(item, index)}
          sx={{
            cursor: "pointer",
            maxHeight: "48px",
            padding: "8px",
          }}
          data-testid={`conversation-${item.name}-row`}
        >
          <TableCell sx={{ padding: "8px" }}>
            <Stack gap={1.5} direction="row" alignItems="center">
              <ICON size={18} weight="fill" color="#8F8F8F" />
              <TruncateText
                fontSize="14px"
                fontWeight="book"
                color="#403D3D"
                lineClamp={1}
              >
                {getGlobalTopicLabel(item.name) || item.displayName}
              </TruncateText>
              {item.totalUnread > 0 && (
                <TotalUnread
                  hasUnreadActivity={hasUnreadActivity}
                  item={item}
                />
              )}
            </Stack>
          </TableCell>
          <TableCell sx={{ padding: "8px" }}>
            <ImagesStacker
              data={item.userAvatars}
              imageWidth={isMobileView ? 24 : 20}
              condition={
                item?.userAvatars?.length === 4 && item?.totalUsers > 4
              }
              renderAdditional={
                item.totalUnread ? () => <TotalUnread item={item} /> : undefined
              }
            />
          </TableCell>
          {!isMobileView && (
            <>
              <TableCell
                sx={{ textAlign: "center", padding: "12px" }}
                data-testid={`conversation-${item.displayName}-total-messages`}
              >
                {item.totalMessages}
              </TableCell>
              <TableCell
                sx={{ padding: "8px", textAlign: "center", fontSize: "14px" }}
              >
                {item.lastActivity ? (
                  formatDistanceToNowStrict(fromUnixTime(item.lastActivity), {
                    addSuffix: true,
                  })
                ) : (
                  <Text
                    textAlign="center"
                    data-testid={`conversation-${item.displayName}-no-activity`}
                  >
                    -
                  </Text>
                )}
              </TableCell>
            </>
          )}
        </TableRow>
      ))}
    </TableBody>
  );
};
 
export default ConversationsTableBody;