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

100% Statements 28/28
100% Branches 4/4
100% Functions 5/5
100% Lines 24/24

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 761x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   56x           56x   56x       26x       1x         56x 56x     56x     56x   56x 56x                                                          
import { Skeleton, styled } from "@mui/material";
import TextBody from "components/TextBody";
import { useLiteUser } from "features/userQueries/useLiteUsers";
import { useTranslation } from "i18n";
import { MESSAGES } from "i18n/namespaces";
import React from "react";
import { theme } from "theme";
 
import { timestamp2Date } from "../../../utils/date";
import { firstName } from "../../../utils/names";
import useOnVisibleEffect from "../../../utils/useOnVisibleEffect";
import { controlMessage, messageTargetId } from "../utils";
import { messageElementId, MessageProps } from "./MessageView";
import TimeInterval from "./TimeInterval";
 
const StyledWrapper = styled("div")(() => ({
  marginInlineEnd: "auto",
  marginInlineStart: "auto",
  textAlign: "center",
}));
 
const StyledTimestamp = styled("div")(() => theme.typography.caption);
 
const StyledBodyWrapper = styled("div")(() => ({
  paddingInlineEnd: theme.spacing(1),
}));
 
const StyledSkeleton = styled(Skeleton)(() => ({
  minWidth: 100,
}));
 
export default function ControlMessageView({
  message,
  onVisible,
  className,
}: MessageProps) {
  const { t } = useTranslation(MESSAGES);
  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)}
    >
      <StyledTimestamp>
        <TimeInterval date={timestamp2Date(message.time!)} />
      </StyledTimestamp>
 
      <StyledBodyWrapper>
        {!isAuthorLoading && !isTargetLoading ? (
          <TextBody>
            {controlMessage({
              message,
              user: authorName,
              target_user: targetName,
              t,
            })}
          </TextBody>
        ) : (
          <StyledSkeleton />
        )}
      </StyledBodyWrapper>
    </StyledWrapper>
  );
}