All files / app/features/messages/messagelist ControlMessageView.tsx

100% Statements 29/29
100% Branches 4/4
100% Functions 5/5
100% Lines 25/25

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 774x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 4x 4x           56x           56x           56x       19x       7x 56x 56x 56x 56x 56x   56x 56x                                                          
import { Skeleton, styled } from "@mui/material";
import RelativeTime from "components/RelativeTime";
import TextBody from "components/TextBody";
import { useAuthContext } from "features/auth/AuthProvider";
import { useLiteUser } from "features/userQueries/useLiteUsers";
import { useTranslation } from "i18n";
import { MESSAGES } from "i18n/namespaces";
import React from "react";
import { theme } from "theme";
 
import { firstName } from "../../../utils/names";
import useOnVisibleEffect from "../../../utils/useOnVisibleEffect";
import { controlMessage, messageTargetId } from "../utils";
import { messageElementId, MessageProps } from "./MessageView";
 
interface ControlMessageProps extends MessageProps {
  isOffer?: boolean;
}
 
const StyledWrapper = styled("div")(() => ({
  marginInlineEnd: "auto",
  marginInlineStart: "auto",
  textAlign: "center",
}));
 
const StyledRelativeTime = styled(RelativeTime)(() => ({
  ...theme.typography.caption,
  fontSize: "0.75rem", // 12px
  display: "block",
}));
 
const StyledBodyWrapper = styled("div")(() => ({
  paddingInlineEnd: theme.spacing(1),
}));
 
const StyledSkeleton = styled(Skeleton)(() => ({
  minWidth: 100,
}));
 
export default function ControlMessageView({ message, onVisible, className, isOffer }: ControlMessageProps) {
  const { t } = useTranslation(MESSAGES);
  const { authState } = useAuthContext();
  const { data: author, isLoading: isAuthorLoading } = useLiteUser(message.authorUserId);
  const { data: target, isLoading: isTargetLoading } = useLiteUser(messageTargetId(message));
  const { ref } = useOnVisibleEffect(onVisible);
 
  const authorName = firstName(author?.name);
  const targetName = firstName(target?.name);
  return (
    <StyledWrapper
      className={className}
      data-testid={`message-${message.messageId}`}
      ref={ref}
      id={messageElementId(message.messageId)}
    >
      <StyledRelativeTime instant={message.time!} />
 
      <StyledBodyWrapper>
        {!isAuthorLoading && !isTargetLoading ? (
          <TextBody>
            {controlMessage({
              message,
              user: authorName,
              target_user: targetName,
              t,
              isCurrentUser: message.authorUserId === authState.userId,
              isOffer,
            })}
          </TextBody>
        ) : (
          <StyledSkeleton />
        )}
      </StyledBodyWrapper>
    </StyledWrapper>
  );
}