All files / src/features/Minibuilders/Conversations/Modal MessageSection.tsx

70.83% Statements 17/24
50% Branches 10/20
70% Functions 7/10
77.27% Lines 17/22

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 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                                                                  30x   30x 15x 15x                 30x         30x           30x       30x 30x 30x                                                                                                                                                       15x                             23x             30x   30x                               23x 1x     2x                                                                  
import { TDocument } from "@common/FilePreview/types";
import { Text } from "@common/Text";
import { useDocumentPreview } from "@hooks/common/documents";
import { downloadDocument } from "@hooks/common/documents/utils";
import { Box, Divider, Skeleton, Stack, StackProps } from "@mui/material";
import { PencilSimpleIcon } from "@phosphor-icons/react";
import { Message } from "./types";
import moment from "moment";
import { PLATFORM_TIMEZONE } from "@utils/timezones";
import AvatarPlaceholder from "@assets/images/avatar-placeholder.png";
import { IconButton } from "@common/IconButton";
import { isFunction } from "lodash";
import { DocumentListItem } from "features/Notifications/NotificationsCenter/modals/DocumentListItem";
import { TMerchantDocument } from "@components/Merchants/MerchantPreview/data.types";
import { palette } from "@palette";
 
interface Props {
  message: Message;
  isEnterprise?: boolean;
  merchantID: number;
  stack?: StackProps;
  showDivider?: boolean;
  onHandleEdit?: (data: { message: string; id: number }) => void;
}
 
function MessageSection({
  message,
  isEnterprise,
  merchantID,
  stack,
  showDivider = true,
  onHandleEdit,
}: Props) {
  const {
    authorFirstName,
    authorLastName,
    authorAvatarImageURL,
    body,
    createdAt,
    attachments,
    contentLastEditedAt,
  } = message;
  const documents =
    attachments
      ?.filter((item: any) => item?.isUploaded)
      ?.map((attachment: any, idx: any) => ({
        id: idx,
        fileURL: attachment?.fileURL,
        fileName: attachment?.fileName,
        fileSize: attachment?.fileSize,
        fileType: attachment?.fileType,
        tag: "",
      })) || [];
 
  const handleDownload = (file: TDocument | null) => {
    if (!file?.URL) return;
    downloadDocument({ fileName: file.name, fileURL: file.URL });
  };
 
  const { handlePreview } = useDocumentPreview({
    list: documents,
    merchantID,
    handlers: { handleDownload },
    isEnterprise,
  });
  const showPreview = (document: TMerchantDocument) => {
    if (!document) return;
    handlePreview(document);
  };
  const isEdited = Boolean(contentLastEditedAt);
  const showEdited = isFunction(onHandleEdit) && isEdited;
  return (
    <Stack
      px="16px"
      my="4px"
      {...stack}
      sx={{
        "&:hover .icon-button": {
          display: "flex",
        },
      }}
    >
      {showDivider && (
        <Divider
          sx={{
            width: "1px",
            height: "28px",
            bgcolor: "#B8B8B8",
            ml: "10px",
            mb: "8px",
          }}
        />
      )}
 
      <Text mb="8px" fontWeight="book" fontSize="12px" color="#8F8F8F">
        {moment.unix(createdAt).tz(PLATFORM_TIMEZONE).format("DD MMM YYYY, HH:mm")}
      </Text>
      <Stack
        p="12px 16px"
        borderRadius="8px"
        bgcolor="#FFFFFF"
        direction="column"
        gap="12px"
        alignItems="stretch"
      >
        <Box alignItems="center" justifyContent="space-between" display="flex">
          <Stack mb="12px" alignItems="center" gap="8px" flexDirection="row">
            <Box
              width="32px"
              height="32px"
              borderRadius="50%"
              src={
                authorAvatarImageURL
                  ? authorAvatarImageURL + "/small"
                  : AvatarPlaceholder
              }
              component="img"
            />
            <Text fontWeight="book" fontSize="14px" color="#403D3D">
              {`${authorFirstName} ${authorLastName}`}
            </Text>
          </Stack>
          {isFunction(onHandleEdit) && !isEdited && (
            <IconButton
              className="icon-button"
              sx={{
                boxShadow: "none",
                border: "none",
                borderRadius: "50%",
                height: "28px",
                width: "28px",
                display: "none",
              }}
              onClick={() =>
                onHandleEdit({
                  message: body,
                  id: message?.id,
                })
              }
            >
              <PencilSimpleIcon size={20} />
            </IconButton>
          )}
        </Box>
        <BodyText body={body} showEdited={showEdited} />
        {attachments &&
          documents.map((document: any, idx: any) => (
            <DocumentListItem
              key={idx}
              document={document}
              fileName={document.fileName}
              fileSize={document.fileSize}
              onPreview={showPreview}
            />
          ))}
      </Stack>
    </Stack>
  );
}
 
export default MessageSection;
 
const BodyText = ({
  body,
  showEdited,
}: {
  body: string;
  showEdited: boolean;
}) => {
  const editedText = showEdited ? "(Edited)" : null;
 
  return (
    <Text
      fontSize="14px"
      fontWeight="book"
      color="#575353"
      whiteSpace="pre-wrap"
      sx={{ overflowWrap: "break-word" }}
    >
      <Text component="span" useParse>
        {body}
      </Text>{" "}
      <span style={{ color: palette.neutral[60] }}>{editedText}</span>
    </Text>
  );
};
 
export const MessageSkeleton = ({ count }: { count: number }) => {
  return (
    <>
      {Array.from({ length: count }).map((_, index) => (
        <Stack
          data-testid="message-loading-container"
          key={index}
          px="16px"
          mt="10px"
          mb="30px"
          gap="10px"
        >
          <Skeleton
            variant="rectangular"
            width={75}
            height={12}
            sx={{ fontSize: "12px", mb: "8px", borderRadius: "8px" }}
          />
          <Stack direction="row" gap={2} alignItems="center" paddingLeft="15px">
            <Skeleton variant="circular" width={32} height={32} />
            <Skeleton
              variant="rectangular"
              width={100}
              height={18}
              sx={{ fontSize: "14px", borderRadius: "8px" }}
            />
          </Stack>
          <Skeleton
            variant="rectangular"
            height={70}
            sx={{ fontSize: "14px", borderRadius: "8px" }}
          />
        </Stack>
      ))}
    </>
  );
};