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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 37x 9x 110x 37x 110x 2x 22x 22x 22x 22x 22x 22x 9x 5x 5x 5x 4x 2x 1x 1x 2x 2x 2x 24x 4x 13x 5x 37x 72x 9x | import {
Divider,
List,
ListItem,
ListItemProps,
ListItemText,
styled,
} from "@mui/material";
import Alert from "components/Alert";
import Button from "components/Button";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import StyledLink from "components/StyledLink";
import {
useCommunity,
useListSubCommunities,
} from "features/communities/hooks";
import { useTranslation } from "i18n";
import { DASHBOARD } from "i18n/namespaces";
import { Community } from "proto/communities_pb";
import { useState } from "react";
import { routeToCommunity } from "routes";
import { theme } from "theme";
const OuterWrapper = styled("div")(({ theme }) => ({
display: "flex",
flexDirection: "row",
alignItems: "flex-start",
width: "100%",
"& > * + *": {
marginInlineStart: theme.spacing(2),
},
overflow: "hidden",
}));
const InnerWrapper = styled("div")(({ theme }) => ({
overflow: "auto",
maxHeight: "60vh",
display: "flex",
}));
const StyledList = styled(List)(({ theme }) => ({
minWidth: "10rem",
}));
const StyledLoader = styled("div")(({ theme }) => ({
margin: theme.spacing("auto", 2),
}));
const StyledListItem = styled(ListItem)<ListItemProps>(({ theme }) => ({
background: "transparent",
border: "none",
"&:hover": {
background: "#3135390A",
},
"& .selected": {
fontWeight: "bold",
},
}));
const StyledDivider = styled(Divider)(({ theme }) => ({
margin: theme.spacing(0, 1),
}));
const StyledListItemText = styled(ListItemText)(({ theme }) => ({
color: theme.palette.text.primary,
}));
export default function CommunityBrowser() {
const { t } = useTranslation([DASHBOARD]);
const [selected, setSelected] = useState<Community.AsObject[]>([]);
const globalCommunityQuery = useCommunity(1);
//react-query doesn't have useInfiniteQueries
//as a workaround, cache query results
//and only show "Load more" for last column
const query = useListSubCommunities(
selected?.[selected.length - 1]?.communityId || 1,
);
const [cachedQueryResults, setCachedQueryResults] = useState<
{
data: Community.AsObject[];
hasMore?: boolean;
}[]
>([]);
const handleClick = (community: Community.AsObject, level: number) => {
//if the last column is clicked
if (level === selected.length) {
setCachedQueryResults([
...cachedQueryResults,
{
data: query.data!.pages.flatMap((page) => page.communitiesList),
hasMore: query.hasNextPage,
},
]);
setSelected([...selected.slice(0, level), community]);
} else {
if (community.communityId === selected[level].communityId) {
//a previously selected item is clicked, so unselect it
//treat level = 0 as a special case
if (level === 0) {
setSelected([]);
} else {
setSelected([...selected.slice(0, level)]);
}
setCachedQueryResults(cachedQueryResults.slice(0, level));
} else {
//a previously unselected item is clicked
setSelected([...selected.slice(0, Math.max(level, 0)), community]);
setCachedQueryResults(cachedQueryResults.slice(0, level + 1));
}
}
};
return (
<OuterWrapper>
<InnerWrapper>
{cachedQueryResults.map((query, index) => (
<BrowserColumn
key={index}
parent={selected?.[index - 1] ?? globalCommunityQuery.data}
communities={query.data}
handleClick={(community) => handleClick(community, index)}
selected={selected[index]?.communityId}
/>
))}
{query.isLoading ? ( // div prevents overflow scrollbar from spinner
<StyledLoader>
<CenteredSpinner />
</StyledLoader>
) : query.isSuccess && globalCommunityQuery.isSuccess ? (
<div>
<BrowserColumn
parent={
selected?.[selected.length - 1] ?? globalCommunityQuery.data
}
communities={query.data.pages.flatMap(
(page) => page.communitiesList,
)}
handleClick={(community) =>
handleClick(community, selected.length)
}
/>
{query.hasNextPage && (
<Button
onClick={() => query.fetchNextPage()}
loading={query.isFetchingNextPage}
variant="outlined"
>
{t("dashboard:load_more")}
</Button>
)}
</div>
) : (
<Alert severity="error">{query?.error?.message || ""}</Alert>
)}
</InnerWrapper>
</OuterWrapper>
);
}
function BrowserColumn({
parent,
communities,
handleClick,
selected,
}: {
parent?: Community.AsObject;
communities: Community.AsObject[];
handleClick: (community: Community.AsObject) => void;
selected?: number;
}) {
const { t } = useTranslation([DASHBOARD]);
return (
<StyledList>
{parent && (
<>
<StyledListItem>
<StyledLink
href={routeToCommunity(parent.communityId, parent.slug)}
>
<StyledListItemText
primary={parent.name}
sx={{
color: theme.palette.primary.main,
}}
/>
</StyledLink>
</StyledListItem>
<StyledDivider />
</>
)}
{communities.length === 0 ? (
<StyledListItem>
<StyledListItemText
primaryTypographyProps={{
className: "emptyState",
variant: "body2",
}}
>
{t("dashboard:no_sub_communities")}
</StyledListItemText>
</StyledListItem>
) : (
communities.map((community) => (
<StyledListItem
key={community.communityId}
component="button"
onClick={() => handleClick(community)}
aria-selected={community.communityId === selected}
>
<StyledListItemText
primaryTypographyProps={{
className:
community.communityId === selected ? "selected" : undefined,
}}
>
{community.name}
</StyledListItemText>
</StyledListItem>
))
)}
</StyledList>
);
}
|