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

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

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 897x 7x 7x 7x 7x 7x 7x 7x   7x 7x 7x   13x                                               21x 43x                 43x                       1x                 14x     51x                                  
import { styled, 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 { theme } from "theme";
import hasAtLeastOnePage from "utils/hasAtLeastOnePage";
 
import { useThread } from "../hooks";
import Comment from "./Comment";
import CommentForm from "./CommentForm";
 
const StyledCommentsListContainer = styled("div")(({ theme }) => ({
  "& > * + *": {
    marginBlockStart: theme.spacing(2),
  },
  padding: theme.spacing(0, 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",
  },
}));
 
interface CommentTreeProps {
  threadId: number;
}
 
export default function CommentTree({ threadId }: CommentTreeProps) {
  const { t } = useTranslation([COMMUNITIES]);
 
  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") ? (
        <StyledCommentsListContainer>
          {hasNextPage && (
            <Button
              loading={isFetchingNextPage}
              onClick={() => fetchNextPage()}
              sx={{
                alignSelf: "center",
              }}
            >
              {t("communities:load_earlier_comments")}
            </Button>
          )}
          {comments.pages
            .flatMap((page) => page.repliesList)
            .reverse()
            .map((comment) => {
              return (
                <Comment key={comment.threadId} topLevel comment={comment} />
              );
            })}
        </StyledCommentsListContainer>
      ) : (
        comments &&
        !commentsError && (
          <Typography variant="body1" sx={{ marginBlockEnd: theme.spacing(6) }}>
            {t("communities:no_comments")}
          </Typography>
        )
      )}
      <CommentForm shown threadId={threadId} />
    </>
  );
}