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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 32x 32x 32x 32x 32x 32x 32x 32x 11x 10x 32x 9x 5x 5x 5x 4x 2x 1x 1x 2x 2x 2x 32x 35x 4x 13x 5x 48x 48x 48x 94x 9x | import { Divider, List, ListItem, ListItemText } from "@material-ui/core"; import Alert from "components/Alert"; import Button from "components/Button"; import CircularProgress from "components/CircularProgress"; 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 { useEffect, useRef, useState } from "react"; import { routeToCommunity } from "routes"; import makeStyles from "utils/makeStyles"; const useStyles = makeStyles((theme) => ({ root: { display: "flex", flexDirection: "row", alignItems: "flex-start", "& > * + *": { marginInlineStart: theme.spacing(2), }, }, list: { minWidth: "10rem", }, loader: { margin: theme.spacing("auto", 2), }, selected: { fontWeight: "bold", }, emptyState: { color: theme.palette.grey[600], }, })); export default function CommunityBrowser() { const { t } = useTranslation([DASHBOARD]); const classes = useStyles(); 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 lastColumnRef = useRef<HTMLDivElement>(null); useEffect(() => { setTimeout( () => lastColumnRef.current?.scrollIntoView({ behavior: "smooth", inline: "end", }), 50 ); }, [selected]); 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 ( <div className={classes.root}> {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 <div className={classes.loader}> <CircularProgress /> </div> ) : query.isSuccess && globalCommunityQuery.isSuccess ? ( <div ref={lastColumnRef}> <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> )} </div> ); } function BrowserColumn({ parent, communities, handleClick, selected, }: { parent?: Community.AsObject; communities: Community.AsObject[]; handleClick: (community: Community.AsObject) => void; selected?: number; }) { const { t } = useTranslation([DASHBOARD]); const classes = useStyles(); return ( <List className={classes.list}> {parent && ( <> <ListItem component={StyledLink} href={routeToCommunity(parent.communityId, parent.slug)} > {parent.name} </ListItem> <Divider /> </> )} {communities.length === 0 ? ( <ListItem> <ListItemText primaryTypographyProps={{ className: classes.emptyState, variant: "body2", }} > {t("dashboard:no_sub_communities")} </ListItemText> </ListItem> ) : ( communities.map((community) => ( <ListItem key={community.communityId} button onClick={() => handleClick(community)} aria-selected={community.communityId === selected} > <ListItemText primaryTypographyProps={{ className: community.communityId === selected ? classes.selected : undefined, }} > {community.name} </ListItemText> </ListItem> )) )} </List> ); } |