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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | import { ArrowBack, ArrowForward, Groups } from "@mui/icons-material";
import { Box, IconButton, styled, Typography, TypographyProps, useMediaQuery, useTheme } from "@mui/material";
import Alert from "components/Alert";
import FadingScrollTrack from "components/FadingScrollTrack";
import StyledLink from "components/StyledLink";
import TextBody from "components/TextBody";
import { useListUserCommunities } from "features/communities/hooks";
import { Trans, useTranslation } from "i18n";
import { DASHBOARD } from "i18n/namespaces";
import { useEffect, useRef, useState } from "react";
import { routeToCommunity } from "routes";
const CARD_GAP = 12;
const CARD_WIDTH = 200;
const SCROLL_END_TOL = 6;
const SectionHeader = styled("div")({
display: "flex",
alignItems: "center",
justifyContent: "space-between",
marginBottom: "8px",
});
const CardSlot = styled(Box)({
flex: `0 0 ${CARD_WIDTH}px`,
minWidth: 0,
scrollSnapAlign: "start",
});
const CommunityCard = styled(StyledLink)(({ theme }) => ({
display: "flex",
flexDirection: "column",
height: "100%",
padding: theme.spacing(1.5),
border: "1px solid var(--mui-palette-divider)",
borderRadius: 10,
background: "var(--mui-palette-background-paper)",
transition: "border-color 0.2s, box-shadow 0.2s",
"&:hover": {
borderColor: "var(--mui-palette-primary-main)",
boxShadow: "0 2px 8px rgba(0,0,0,0.08)",
},
}));
const CommunityName = styled(Typography)<TypographyProps>({
fontWeight: 600,
display: "-webkit-box",
WebkitLineClamp: 2,
WebkitBoxOrient: "vertical",
overflow: "hidden",
textOverflow: "ellipsis",
overflowWrap: "anywhere",
});
const SkeletonCard = styled("div")(({ theme }) => ({
height: "100%",
padding: theme.spacing(1.5),
border: "1px solid var(--mui-palette-divider)",
borderRadius: 10,
background: "var(--mui-palette-background-paper)",
}));
const StyledBrowseCommunitiesLink = styled(StyledLink)(() => ({
verticalAlign: "baseline",
}));
export default function CommunitiesList() {
const { t } = useTranslation([DASHBOARD]);
const theme = useTheme();
const cardsPerView = useMediaQuery(theme.breakpoints.down("sm")) ? 2 : 3;
const scrollerRef = useRef<HTMLDivElement>(null);
const [canScrollLeft, setCanScrollLeft] = useState(false);
const [canScrollRight, setCanScrollRight] = useState(false);
const { data, isPending, error } = useListUserCommunities();
const communities = (data?.pages ?? []).flatMap((page) => page.communitiesList);
const updateScrollState = () => {
const el = scrollerRef.current;
if (!el) return;
setCanScrollLeft(el.scrollLeft > 0);
setCanScrollRight(Math.round(el.scrollLeft) < el.scrollWidth - el.clientWidth - SCROLL_END_TOL);
};
useEffect(() => {
updateScrollState();
}, [communities.length, isPending, cardsPerView]);
const scroll = (dir: 1 | -1) => {
const el = scrollerRef.current;
if (!el) return;
el.scrollBy({
left: dir * (CARD_WIDTH + CARD_GAP) * cardsPerView,
behavior: "smooth",
});
};
return (
<div>
<SectionHeader>
<Typography variant="h2" sx={{ display: "inline-flex", alignItems: "center", gap: 1 }}>
<Groups sx={{ fontSize: 20, color: "var(--mui-palette-primary-main)" }} />
{t("dashboard:your_communities_heading")}
</Typography>
<div>
<IconButton
size="small"
onClick={() => scroll(-1)}
disabled={!canScrollLeft}
color={canScrollLeft ? "primary" : "default"}
aria-label={t("dashboard:prev_page_button_a11y")}
>
<ArrowBack fontSize="small" />
</IconButton>
<IconButton
size="small"
onClick={() => scroll(1)}
disabled={!canScrollRight}
color={canScrollRight ? "primary" : "default"}
aria-label={t("dashboard:next_page_button_a11y")}
>
<ArrowForward fontSize="small" />
</IconButton>
</div>
</SectionHeader>
<Typography
variant="body1"
sx={{
marginBottom: "16px",
}}
>
<Trans
i18nKey="dashboard:your_communities_helper_text"
components={{
browseCommunitiesLink: <StyledBrowseCommunitiesLink href="/communities" underline="hover" />,
}}
/>
</Typography>
{error?.message && <Alert severity="error">{error.message}</Alert>}
{isPending ? (
<FadingScrollTrack $gap={CARD_GAP} $snapType="x proximity">
{Array.from({ length: cardsPerView }).map((_, i) => (
<CardSlot key={i}>
<SkeletonCard />
</CardSlot>
))}
</FadingScrollTrack>
) : communities.length > 0 ? (
<FadingScrollTrack
ref={scrollerRef}
onScroll={updateScrollState}
$gap={CARD_GAP}
$snapType="x proximity"
$canScrollLeft={canScrollLeft}
$canScrollRight={canScrollRight}
>
{communities.map((community) => (
<CardSlot key={`community-${community.communityId}`}>
<CommunityCard href={routeToCommunity(community.communityId, community.slug)}>
<CommunityName variant="subtitle2" component="span">
{community.name}
</CommunityName>
<Typography variant="body2" color="textSecondary">
{t("dashboard:member_count", {
count: community.memberCount,
})}
</Typography>
</CommunityCard>
</CardSlot>
))}
</FadingScrollTrack>
) : (
<TextBody>{t("dashboard:no_community")}</TextBody>
)}
</div>
);
}
|