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

100% Statements 45/45
88.57% Branches 31/35
100% Functions 14/14
100% Lines 35/35

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 1824x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x   273x     4x   4x 1174x 195x         99x         4x 784x 195x                                                             99x               159x           99x             195x             195x                         195x                 99x                                   3x 195x   195x 195x 195x 195x   195x                                                                                  
import { Card, CardContent, Skeleton, styled, Typography } from "@mui/material";
import Avatar from "components/Avatar";
import Linkify from "components/Linkify";
import RelativeTime from "components/RelativeTime";
import TextBody from "components/TextBody";
import { contentRefs } from "features/contentRefs";
import FlagButton from "features/FlagButton";
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/messages_pb";
import useOnVisibleEffect from "utils/useOnVisibleEffect";
 
export const messageElementId = (id: number) => `message-${id}`;
 
// Shared so the alignment cap can offset for it
const AVATAR_SIZE = 40;
 
const RootContainer = styled("div", {
  shouldForwardProp: (prop) => prop !== "isCurrentUser" && prop !== "isLoading",
})<{ isCurrentUser: boolean; isLoading: boolean }>(({ isCurrentUser }) => ({
  display: "flex",
  justifyContent: isCurrentUser ? "flex-end" : "flex-start",
}));
 
const StyledAvatar = styled(Avatar)(() => ({
  height: AVATAR_SIZE,
  width: AVATAR_SIZE,
}));
 
const StyledCard = styled(Card, {
  shouldForwardProp: (prop) => prop !== "isLoading" && prop !== "isCurrentUser" && prop !== "isDm",
})<{ isLoading: boolean; isCurrentUser: boolean; isDm: boolean }>(({ theme, isCurrentUser, isDm, isLoading }) => ({
  width: "fit-content",
  minWidth: 150,
  [theme.breakpoints.up("xs")]: {
    // Only group-chat received messages have a left (avatar) column to offset.
    maxWidth: !isCurrentUser && !isDm ? `calc(85% - ${AVATAR_SIZE}px - ${theme.spacing(1)})` : "85%",
  },
  [theme.breakpoints.up("md")]: {
    maxWidth: "min(70%, 75rem)",
  },
  border: "1px solid",
  borderRadius: theme.shape.borderRadius * 3,
 
  ...(isLoading && {
    borderColor: "var(--mui-palette-text-secondary)",
  }),
 
  ...(isCurrentUser &&
    !isLoading && {
      borderColor: "var(--mui-palette-primary-main)",
      backgroundColor: "var(--mui-palette-primary-main)",
      color: "var(--mui-palette-common-white)",
    }),
 
  ...(!isCurrentUser &&
    !isLoading && {
      borderColor: "var(--mui-palette-grey-300)",
      backgroundColor: "var(--mui-palette-grey-200)",
    }),
}));
 
const StyledLeftOfMessage = styled("div")(({ theme }) => ({
  display: "flex",
  flexDirection: "column",
  alignItems: "center",
  width: AVATAR_SIZE,
  marginRight: theme.spacing(1),
}));
 
const StyledHeader = styled("div")(({ theme }) => ({
  display: "flex",
  padding: theme.spacing(1),
  paddingBottom: theme.spacing(0.5),
}));
 
const StyledNameTypography = styled(Typography)(({ theme }) => ({
  ...theme.typography.body2,
  minWidth: 0,
  fontWeight: "bold",
  margin: 0,
}));
 
const StyledRelativeTime = styled(RelativeTime)(({ theme }) => ({
  ...theme.typography.caption,
  fontSize: "0.75rem", // 12px
  flexShrink: 0,
  whiteSpace: "nowrap",
}));
 
const StyledMessageBody = styled(CardContent)(({ theme }) => ({
  paddingTop: 0,
  paddingInline: theme.spacing(1),
  paddingBottom: theme.spacing(0.5),
  overflowWrap: "break-word",
  whiteSpace: "pre-wrap",
 
  // No header (own + 1:1 messages): restore top padding so text isn't flush to the top.
  "&:first-of-type": {
    paddingTop: theme.spacing(1),
  },
}));
 
const StyledFooter = styled("div")(({ theme }) => ({
  display: "flex",
  alignItems: "center",
  justifyContent: "flex-end",
  gap: theme.spacing(0.25),
  paddingInline: theme.spacing(1),
  paddingBottom: theme.spacing(0.5),
}));
 
const StyledFlagButton = styled(FlagButton)(({ theme }) => ({
  padding: theme.spacing(0.25),
  color: "var(--mui-palette-primary-main)",
  "& svg": {
    fontSize: "1rem",
  },
  "&:hover, &:focus-visible": {
    color: "var(--mui-palette-primary-dark)",
  },
}));
 
export interface MessageProps {
  message: Message.AsObject;
  onVisible?(): void;
  className?: string;
  isDm?: boolean;
}
 
export default function MessageView({ className, message, onVisible, isDm = false }: 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 = message.authorUserId === 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 && !isDm && (
        <StyledLeftOfMessage>
          {isAuthorLoading ? <Skeleton variant="rounded" width={40} height={40} /> : <StyledAvatar user={author} />}
        </StyledLeftOfMessage>
      )}
      <StyledCard isLoading={isLoading} isCurrentUser={isCurrentUser} isDm={isDm}>
        {!isCurrentUser && !isDm && (
          <StyledHeader>
            {isAuthorLoading ? (
              <Skeleton width={100} />
            ) : (
              <StyledNameTypography variant="h5">{author ? author.name : t("unknown_user")}</StyledNameTypography>
            )}
          </StyledHeader>
        )}
        <StyledMessageBody>
          <TextBody>
            <Linkify text={message.text?.text || ""} isCurrentUser={isCurrentUser} />
          </TextBody>
        </StyledMessageBody>
        <StyledFooter>
          <StyledRelativeTime instant={message.time!} />
          {author && !isCurrentUser && (
            <StyledFlagButton contentRef={contentRefs.chatMessage(message)} authorUser={author.userId} size="small" />
          )}
        </StyledFooter>
      </StyledCard>
    </RootContainer>
  );
}