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

100% Statements 40/40
86.66% Branches 39/45
100% Functions 12/12
100% Lines 32/32

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 1971x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   267x   1x 1225x   204x                           100x         1x 613x   204x                                                               100x           204x             100x             204x                 104x                           135x         204x   204x       204x 204x 204x   204x                                                                                                                                  
import { Card, CardContent, Skeleton, styled, Typography } from "@mui/material";
import Avatar from "components/Avatar";
import Linkify from "components/Linkify";
import TextBody from "components/TextBody";
import FlagButton from "features/FlagButton";
import TimeInterval from "features/messages/messagelist/TimeInterval";
import useCurrentUser from "features/userQueries/useCurrentUser";
import { useLiteUser } from "features/userQueries/useLiteUsers";
import { useTranslation } from "i18n";
import { MESSAGES } from "i18n/namespaces";
import { Message } from "proto/conversations_pb";
import { timestamp2Date } from "utils/date";
import useOnVisibleEffect from "utils/useOnVisibleEffect";
 
export const messageElementId = (id: number) => `message-${id}`;
 
const RootContainer = styled("div", {
  shouldForwardProp: (prop) => prop !== "isCurrentUser" && prop !== "isLoading",
})<{ isCurrentUser: boolean; isLoading: boolean }>(
  ({ theme, isCurrentUser, isLoading }) => ({
    "& > :first-of-type": { marginRight: theme.spacing(2) },
    display: "flex",
 
    ...(isLoading && {
      justifyContent: "center",
    }),
 
    ...(isCurrentUser && !isLoading && { justifyContent: "flex-end" }),
 
    ...(!isCurrentUser && !isLoading && { justifyContent: "flex-start" }),
  }),
);
 
const StyledAvatar = styled(Avatar)(({ theme }) => ({
  height: 40,
  width: 40,
}));
 
const StyledCard = styled(Card, {
  shouldForwardProp: (prop) => prop !== "isLoading" && prop !== "isCurrentUser",
})<{ isLoading: boolean; isCurrentUser: boolean }>(
  ({ theme, isCurrentUser, isLoading }) => ({
    [theme.breakpoints.up("xs")]: {
      width: "100%",
    },
    [theme.breakpoints.up("sm")]: {
      width: "80%",
    },
    [theme.breakpoints.up("md")]: {
      width: "70%",
    },
    border: "1px solid",
    borderRadius: theme.shape.borderRadius * 3,
 
    ...(isLoading && {
      borderColor: theme.palette.text.secondary,
    }),
 
    ...(isCurrentUser &&
      !isLoading && {
        borderColor: theme.palette.primary.main,
        backgroundColor: theme.palette.primary.main,
        color: theme.palette.common.white,
      }),
 
    ...(!isCurrentUser &&
      !isLoading && {
        borderColor: theme.palette.grey[300],
        backgroundColor: theme.palette.grey[200],
      }),
  }),
);
 
const StyledLeftOfMessage = styled("div")(({ theme }) => ({
  display: "flex",
  flexDirection: "column",
  alignItems: "center",
}));
 
const StyledHeader = styled("div")(({ theme }) => ({
  alignItems: "center",
  display: "flex",
  padding: theme.spacing(2),
  paddingBottom: theme.spacing(1),
}));
 
const StyledNameTypography = styled(Typography)(({ theme }) => ({
  ...theme.typography.body2,
  flexGrow: 1,
  fontWeight: "bold",
  margin: 0,
}));
 
const StyledMessageBody = styled(CardContent)(({ theme }) => ({
  "&:last-of-type": { paddingBottom: theme.spacing(2) },
 
  paddingBottom: theme.spacing(1),
  paddingTop: 0,
  overflowWrap: "break-word",
  whiteSpace: "pre-wrap",
}));
 
const StyledFooter = styled("div")(({ theme }) => ({
  display: "flex",
  justifyContent: "flex-end",
  paddingBottom: theme.spacing(2),
  paddingInlineEnd: theme.spacing(2),
  paddingInlineStart: theme.spacing(2),
}));
 
export interface MessageProps {
  message: Message.AsObject;
  onVisible?(): void;
  className?: string;
}
 
export default function MessageView({
  className,
  message,
  onVisible,
}: MessageProps) {
  const { t } = useTranslation(MESSAGES);
 
  const { data: author, isLoading: isAuthorLoading } = useLiteUser(
    message.authorUserId,
  );
  const { data: currentUser, isLoading: isCurrentUserLoading } =
    useCurrentUser();
  const isLoading = isAuthorLoading || isCurrentUserLoading;
  const isCurrentUser = author?.userId === currentUser?.userId;
 
  const { ref } = useOnVisibleEffect(onVisible);
 
  return (
    <RootContainer
      className={className}
      data-testid={`message-${message.messageId}`}
      ref={ref}
      id={messageElementId(message.messageId)}
      isCurrentUser={isCurrentUser}
      isLoading={isLoading}
    >
      {author && !isCurrentUser && (
        <StyledLeftOfMessage>
          {author && !isAuthorLoading && <StyledAvatar user={author} />}
          {isAuthorLoading && (
            <Skeleton variant="rounded" width={40} height={40} />
          )}
          {!author && !isAuthorLoading && <StyledAvatar />}
          <FlagButton
            contentRef={`chat/message/${message.messageId}`}
            authorUser={author.userId}
          />
        </StyledLeftOfMessage>
      )}
      <StyledCard isLoading={isLoading} isCurrentUser={isCurrentUser}>
        <StyledHeader>
          {author && !isAuthorLoading && (
            <StyledNameTypography variant="h5">
              {author.name}
            </StyledNameTypography>
          )}
          {isAuthorLoading && <Skeleton width={100} />}
          {!author && !isAuthorLoading && (
            <StyledNameTypography variant="h5">
              {t("unknown_user")}
            </StyledNameTypography>
          )}
          {!isCurrentUser && (
            <TimeInterval date={timestamp2Date(message.time!)} />
          )}
        </StyledHeader>
 
        <StyledMessageBody>
          <TextBody>
            <Linkify
              text={message.text?.text || ""}
              isCurrentUser={isCurrentUser}
            />
          </TextBody>
        </StyledMessageBody>
 
        {isCurrentUser && (
          <StyledFooter>
            <TimeInterval date={timestamp2Date(message.time!)} />
          </StyledFooter>
        )}
      </StyledCard>
      {author && isCurrentUser && <StyledAvatar user={author} />}
      {isCurrentUserLoading && (
        <Skeleton variant="rounded" width={40} height={40} />
      )}
      {!author && !isAuthorLoading && <StyledAvatar />}
    </RootContainer>
  );
}