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

100% Statements 20/20
100% Branches 6/6
100% Functions 5/5
100% Lines 19/19

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 1127x 7x 7x 7x 7x 7x 7x 7x   7x 7x 7x   19x                                                                                               21x 43x 43x                 43x                         1x           14x     51x                                  
import { Typography } from "@mui/material";
import Alert from "components/Alert";
import Button from "components/Button";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import { useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import hasAtLeastOnePage from "utils/hasAtLeastOnePage";
import makeStyles from "utils/makeStyles";
 
import { useThread } from "../hooks";
import Comment from "./Comment";
import CommentForm from "./CommentForm";
 
const useStyles = makeStyles((theme) => ({
  commentsListContainer: {
    "& > * + *": {
      marginBlockStart: theme.spacing(2),
    },
    display: "flex",
    flexDirection: "column",
    marginBlockStart: theme.spacing(2),
    marginBlockEnd: theme.spacing(6),
    [theme.breakpoints.down("sm")]: {
      //break out of page padding
      left: "50%",
      marginLeft: "-50vw",
      marginRight: "-50vw",
      position: "relative",
      right: "50%",
      width: "100vw",
    },
  },
  loadEarlierCommentsButton: {
    alignSelf: "center",
  },
  commentContainer: {
    display: "flex",
    padding: theme.spacing(2),
    width: "100%",
  },
  commentContent: {
    "& > * + *": {
      marginBlockStart: theme.spacing(0.5),
    },
    display: "flex",
    flexDirection: "column",
    marginInlineStart: theme.spacing(3),
  },
  noComment: {
    marginBlockEnd: theme.spacing(6),
  },
  avatar: {
    height: "3rem",
    width: "3rem",
  },
}));
 
interface CommentTreeProps {
  threadId: number;
}
 
export default function CommentTree({ threadId }: CommentTreeProps) {
  const { t } = useTranslation([COMMUNITIES]);
  const classes = useStyles();
 
  const {
    data: comments,
    error: commentsError,
    fetchNextPage,
    hasNextPage,
    isFetchingNextPage,
    isLoading: isCommentsLoading,
  } = useThread(threadId);
 
  return (
    <>
      {commentsError && <Alert severity="error">{commentsError.message}</Alert>}
      {isCommentsLoading ? (
        <CenteredSpinner />
      ) : hasAtLeastOnePage(comments, "repliesList") ? (
        <div className={classes.commentsListContainer}>
          {hasNextPage && (
            <Button
              className={classes.loadEarlierCommentsButton}
              loading={isFetchingNextPage}
              onClick={() => fetchNextPage()}
            >
              {t("communities:load_earlier_comments")}
            </Button>
          )}
          {comments.pages
            .flatMap((page) => page.repliesList)
            .reverse()
            .map((comment) => {
              return (
                <Comment key={comment.threadId} topLevel comment={comment} />
              );
            })}
        </div>
      ) : (
        comments &&
        !commentsError && (
          <Typography className={classes.noComment} variant="body1">
            {t("communities:no_comments")}
          </Typography>
        )
      )}
      <CommentForm shown threadId={threadId} />
    </>
  );
}