All files / app/features/dashboard MyCommunitiesDiscussions.tsx

0% Statements 0/35
0% Branches 0/25
0% Functions 0/11
0% Lines 0/31

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                                                                                                                                                                                                                                                                   
import { ArrowBack, ArrowForward, Forum } from "@mui/icons-material";
import { IconButton, styled, Typography } from "@mui/material";
import { useInfiniteQuery } from "@tanstack/react-query";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { DASHBOARD } from "i18n/namespaces";
import { ListMyCommunitiesDiscussionsRes } from "proto/discussions_pb";
import { useState } from "react";
import { service } from "service";
 
import { listMyCommunitiesDiscussionsKey } from "../queryKeys";
import DiscussionListRow, {
  DiscussionListContainer,
  DiscussionListRowSkeleton,
} from "./DiscussionListRow";
 
const SectionHeader = styled("div")({
  display: "flex",
  alignItems: "center",
  justifyContent: "space-between",
  marginBottom: "8px",
});
 
const EmptyStateRow = styled("div")(({ theme }) => ({
  display: "flex",
  alignItems: "center",
  gap: theme.spacing(2),
  padding: theme.spacing(2),
  border: "1px dashed var(--mui-palette-divider)",
  borderRadius: 10,
  background: "var(--mui-palette-grey-50)",
}));
 
export default function MyCommunitiesDiscussions() {
  const { t } = useTranslation([DASHBOARD]);
 
  const [currentPageIndex, setCurrentPageIndex] = useState(0);
 
  const { data, isLoading, fetchNextPage, hasNextPage, isFetchingNextPage } =
    useInfiniteQuery<ListMyCommunitiesDiscussionsRes.AsObject, RpcError>({
      queryKey: [listMyCommunitiesDiscussionsKey],
      queryFn: ({ pageParam: pageToken }) =>
        service.communities.listMyCommunitiesDiscussions({
          pageToken: pageToken as string | undefined,
          pageSize: 3,
        }),
      getNextPageParam: (lastPage) =>
        lastPage.nextPageToken ? lastPage.nextPageToken : undefined,
      initialPageParam: undefined as string | undefined,
    });
 
  const pages = data?.pages ?? [];
  const isLastLoadedPage =
    pages.length === 0 || currentPageIndex === pages.length - 1;
  const currentItems = pages[currentPageIndex]?.discussionsList;
 
  const hasPrev = currentPageIndex > 0;
  const hasForward = !isLastLoadedPage || !!hasNextPage;
 
  const handleNext = () => {
    if (!isLastLoadedPage) {
      setCurrentPageIndex((i) => i + 1);
    } else if (hasNextPage && !isFetchingNextPage) {
      fetchNextPage();
      setCurrentPageIndex((i) => i + 1);
    }
  };
 
  const showingSkeleton =
    isLoading || (isFetchingNextPage && currentItems === undefined);
 
  return (
    <div>
      <SectionHeader>
        <Typography
          variant="h2"
          sx={{ display: "inline-flex", alignItems: "center", gap: 1 }}
        >
          <Forum
            sx={{ fontSize: 20, color: "var(--mui-palette-primary-main)" }}
          />
          {t("dashboard:discussions.community_header")}
        </Typography>
        <div>
          <IconButton
            size="small"
            onClick={() => setCurrentPageIndex((i) => i - 1)}
            disabled={!hasPrev}
            color={hasPrev ? "primary" : "default"}
            aria-label={t("dashboard:prev_page_button_a11y")}
          >
            <ArrowBack fontSize="small" />
          </IconButton>
          <IconButton
            size="small"
            onClick={handleNext}
            disabled={!hasForward || isFetchingNextPage}
            color={hasForward ? "primary" : "default"}
            aria-label={t("dashboard:next_page_button_a11y")}
          >
            <ArrowForward fontSize="small" />
          </IconButton>
        </div>
      </SectionHeader>
      {showingSkeleton ? (
        <DiscussionListContainer>
          {[0, 1, 2].map((i) => (
            <DiscussionListRowSkeleton key={i} />
          ))}
        </DiscussionListContainer>
      ) : currentItems?.length ? (
        <DiscussionListContainer>
          {currentItems.map((d) => (
            <DiscussionListRow key={d.discussionId} discussion={d} />
          ))}
        </DiscussionListContainer>
      ) : (
        <EmptyStateRow>
          <Typography
            variant="body2"
            sx={{ color: "var(--mui-palette-text-secondary)" }}
          >
            {t("dashboard:discussions.community_empty_message")}
          </Typography>
        </EmptyStateRow>
      )}
    </div>
  );
}