All files / app/features/communities/discussions DiscussionCard.tsx

100% Statements 33/33
66.66% Branches 6/9
100% Functions 6/6
100% Lines 28/28

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 1265x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x   5x   27x             5x         5x           27x               27x             27x           5x   19x             27x 27x   27x     27x 27x   9x               27x                                                                                        
import { Card, CardContent, Skeleton, styled, Typography } from "@mui/material";
import Avatar from "components/Avatar";
import FlagButton from "features/FlagButton";
import { useLiteUser } from "features/userQueries/useLiteUsers";
import { useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import Link from "next/link";
import { Discussion } from "proto/discussions_pb";
import { useMemo } from "react";
import { routeToDiscussion } from "routes";
import { theme } from "theme";
import { timestamp2Date } from "utils/date";
import { timeAgo } from "utils/timeAgo";
 
import getContentSummary from "../getContentSummary";
 
const StyledCard = styled(Card)(({ theme }) => ({
  "&:hover": {
    backgroundColor: theme.palette.grey[50],
  },
  width: "100%",
}));
 
const StyledAvatar = styled(Avatar)({
  height: "3rem",
  width: "3rem",
});
 
const StyledAvatarFlagContainer = styled("div")({
  display: "flex",
  flexDirection: "column",
  alignItems: "center",
});
 
const StyledCardContent = styled(CardContent)(({ theme }) => ({
  display: "flex",
  "&&": {
    padding: theme.spacing(2),
  },
  width: "100%",
}));
 
const StyledDiscussionSummary = styled("div")(({ theme }) => ({
  display: "flex",
  flexDirection: "column",
  flexGrow: 1,
  marginInlineStart: theme.spacing(2),
}));
 
const StyledCommentsCount = styled(Typography)(({ theme }) => ({
  alignSelf: "flex-end",
  flexShrink: 0,
  color: theme.palette.primary.main,
}));
 
export const DISCUSSION_CARD_TEST_ID = "discussion-card";
 
export default function DiscussionCard({
  discussion,
  className,
}: {
  discussion: Discussion.AsObject;
  className?: string;
}) {
  const { t } = useTranslation([COMMUNITIES]);
  const { data: creator } = useLiteUser(discussion.creatorUserId);
 
  const date = discussion.created
    ? timestamp2Date(discussion.created)
    : undefined;
  const postedTime = date ? timeAgo(date) : null;
  const truncatedContent = useMemo(
    () =>
      getContentSummary({
        originalContent: discussion.content,
        maxLength: 300,
      }),
    [discussion.content],
  );
 
  const contentRef =
    (discussion.ownerCommunityId != 0
      ? `community/${discussion.ownerCommunityId}`
      : `group/${discussion.ownerGroupId}`) +
    `/discussion/${discussion.discussionId}`;
 
  return (
    <StyledCard className={className} data-testid={DISCUSSION_CARD_TEST_ID}>
      <Link href={routeToDiscussion(discussion.discussionId, discussion.slug)}>
        <StyledCardContent>
          <StyledAvatarFlagContainer>
            <StyledAvatar user={creator} isProfileLink={false} />
            <FlagButton
              contentRef={contentRef}
              authorUser={discussion.creatorUserId}
            />
          </StyledAvatarFlagContainer>
          <StyledDiscussionSummary>
            <Typography
              variant="body2"
              component="p"
              sx={{ marginBottom: theme.spacing(0.5) }}
            >
              {creator ? (
                t("communities:by_creator", { name: creator.name })
              ) : (
                <Skeleton sx={{ display: "inline-block", width: 80 }} />
              )}{" "}
              {postedTime && `• ${postedTime}`}
            </Typography>
            <Typography variant="h2" component="h3">
              {discussion.title}
            </Typography>
            <Typography variant="body1">{truncatedContent}</Typography>
            <StyledCommentsCount variant="body1">
              {t("communities:comments_count", {
                count: discussion.thread?.numResponses,
              })}
            </StyledCommentsCount>
          </StyledDiscussionSummary>
        </StyledCardContent>
      </Link>
    </StyledCard>
  );
}