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

100% Statements 34/34
87.5% Branches 7/8
100% Functions 4/4
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 1656x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x   6x 6x 6x   6x 6x 6x 6x   6x 6x 6x 6x   13x                                                               6x   13x               38x 38x 38x           38x   13x     38x                                                       1x                                                                                                              
import { Skeleton, Typography } from "@mui/material";
import Alert from "components/Alert";
import Avatar from "components/Avatar";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import HeaderButton from "components/HeaderButton";
import HtmlMeta from "components/HtmlMeta";
import { BackIcon } from "components/Icons";
import Markdown from "components/Markdown";
import PageTitle from "components/PageTitle";
import { discussionKey } from "features/queryKeys";
import { useLiteUser } from "features/userQueries/useLiteUsers";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { COMMUNITIES, GLOBAL } from "i18n/namespaces";
import { useRouter } from "next/router";
import { Discussion } from "proto/discussions_pb";
import { useQuery } from "react-query";
import { service } from "service";
import { dateFormatter, timestamp2Date } from "utils/date";
import makeStyles from "utils/makeStyles";
 
import CommunityBase from "../CommunityBase";
import CommunityPageSubHeader from "../CommunityPage/CommunityPageSubHeader";
import PageHeader from "../PageHeader";
import CommentTree from "./CommentTree";
 
const useStyles = makeStyles((theme) => ({
  root: {
    paddingBlockEnd: theme.spacing(5),
  },
  header: {
    alignItems: "center",
    display: "flex",
  },
  discussionTitle: {
    marginInlineStart: theme.spacing(2),
  },
  discussionContent: {
    marginBlockEnd: theme.spacing(3),
  },
  creatorContainer: {
    "& > * + *": {
      marginInlineStart: theme.spacing(2),
    },
    alignItems: "center",
    display: "flex",
    marginBlockEnd: theme.spacing(3),
  },
  creatorDetailsContainer: {
    display: "flex",
    flexDirection: "column",
  },
  avatar: {
    height: "3rem",
    width: "3rem",
  },
}));
 
export const CREATOR_TEST_ID = "creator";
 
export default function DiscussionPage({
  discussionId,
}: {
  discussionId: number;
}) {
  const {
    t,
    i18n: { language: locale },
  } = useTranslation([GLOBAL, COMMUNITIES]);
  const classes = useStyles();
  const router = useRouter();
 
  const {
    data: discussion,
    error,
    isLoading: isDiscussionLoading,
  } = useQuery<Discussion.AsObject, RpcError>({
    queryKey: discussionKey(discussionId),
    queryFn: () => service.discussions.getDiscussion(discussionId),
  });
 
  const { data: discussionCreator, isLoading: isCreatorLoading } = useLiteUser(
    discussion?.creatorUserId
  );
 
  return (
    <>
      <HtmlMeta title={discussion?.title} />
      {error && <Alert severity="error">{error.message}</Alert>}
      {isDiscussionLoading ? (
        <CenteredSpinner />
      ) : (
        discussion && (
          <CommunityBase communityId={discussion.ownerCommunityId}>
            {({ community }) => (
              <>
                {community.mainPage && (
                  <PageHeader
                    page={community.mainPage}
                    className={classes.header}
                  />
                )}
                <CommunityPageSubHeader
                  community={community}
                  tab="discussions"
                />
                <div className={classes.root}>
                  <div className={classes.header}>
                    <HeaderButton
                      onClick={() => router.back()}
                      aria-label={t("communities:previous_page")}
                    >
                      <BackIcon />
                    </HeaderButton>
                    <PageTitle className={classes.discussionTitle}>
                      {discussion.title}
                    </PageTitle>
                  </div>
                  <Markdown
                    className={classes.discussionContent}
                    source={discussion.content}
                  />
                  <div
                    className={classes.creatorContainer}
                    data-testid={CREATOR_TEST_ID}
                  >
                    <Avatar
                      user={discussionCreator}
                      className={classes.avatar}
                    />
                    <div className={classes.creatorDetailsContainer}>
                      {isCreatorLoading ? (
                        <Skeleton width={100} />
                      ) : (
                        <Typography variant="body1">
                          {discussionCreator?.name ??
                            t("communities:unknown_user")}
                        </Typography>
                      )}
                      {isCreatorLoading ? (
                        <Skeleton width={100} />
                      ) : (
                        <Typography variant="body2">
                          {t("communities:created_at")}
                          {dateFormatter(locale).format(
                            timestamp2Date(discussion.created!)
                          )}
                        </Typography>
                      )}
                    </div>
                  </div>
                  <Typography variant="h2">
                    {t("communities:comments")}
                  </Typography>
                  <CommentTree threadId={discussion.thread!.threadId} />
                </div>
              </>
            )}
          </CommunityBase>
        )
      )}
    </>
  );
}