All files / app/features/dashboard MyCommunitiesDiscussions.tsx

0% Statements 0/31
0% Branches 0/12
0% Functions 0/7
0% Lines 0/30

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                                                                                                                                                                                                                         
import { ArrowBack, ArrowForward } from "@mui/icons-material";
import { IconButton, styled, Typography } from "@mui/material";
import TextBody from "components/TextBody";
import { useTranslation } from "i18n";
import { DASHBOARD } from "i18n/namespaces";
import { useState } from "react";
 
import { useListMyCommunitiesDiscussions } from "../communities/hooks";
import DiscussionListRow, {
  DiscussionListContainer,
  DiscussionListRowSkeleton,
} from "./DiscussionListRow";
 
const PAGE_SIZE = 3;
 
const SectionHeader = styled("div")({
  display: "flex",
  alignItems: "center",
  justifyContent: "space-between",
  marginBottom: "8px",
});
 
export default function MyCommunitiesDiscussions() {
  const { t } = useTranslation([DASHBOARD]);
 
  const [pageToken, setPageToken] = useState<string | undefined>(undefined);
  const [history, setHistory] = useState<(string | undefined)[]>([]);
 
  const currentPage = history.length;
 
  const { data, isPending } = useListMyCommunitiesDiscussions({
    pageSize: PAGE_SIZE,
    pageToken,
  });
 
  const nextPageToken = data?.nextPageToken;
  const discussions =
    data?.discussionsList.slice(
      currentPage * PAGE_SIZE,
      (currentPage + 1) * PAGE_SIZE,
    ) || [];
 
  const hasNext =
    Boolean(nextPageToken) ||
    (data?.discussionsList.length ?? 0) > (currentPage + 1) * PAGE_SIZE;
  const hasPrev = currentPage > 0;
 
  const goNext = () => {
    setHistory((h) => [...h, pageToken]);
    setPageToken(String(currentPage + 1));
  };
 
  const goPrev = () => {
    setHistory((h) => {
      const prev = [...h];
      const token = prev.pop();
      setPageToken(token);
      return prev;
    });
  };
 
  return (
    <div>
      <SectionHeader>
        <Typography variant="h2">
          {t("dashboard:discussions.community_header")}
        </Typography>
        <div>
          <IconButton
            size="small"
            onClick={goPrev}
            disabled={!hasPrev}
            color={hasPrev ? "primary" : "default"}
            aria-label={t("dashboard:discussions.prev_page_label")}
          >
            <ArrowBack fontSize="small" />
          </IconButton>
          <IconButton
            size="small"
            onClick={goNext}
            disabled={!hasNext}
            color={hasNext ? "primary" : "default"}
            aria-label={t("dashboard:discussions.next_page_label")}
          >
            <ArrowForward fontSize="small" />
          </IconButton>
        </div>
      </SectionHeader>
      {isPending ? (
        <DiscussionListContainer>
          {[0, 1, 2].map((i) => (
            <DiscussionListRowSkeleton key={i} />
          ))}
        </DiscussionListContainer>
      ) : discussions.length > 0 ? (
        <DiscussionListContainer>
          {discussions.map((d) => (
            <DiscussionListRow key={d.discussionId} discussion={d} />
          ))}
        </DiscussionListContainer>
      ) : (
        <TextBody>
          {t("dashboard:discussions.community_empty_message")}
        </TextBody>
      )}
    </div>
  );
}