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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 63x 8x 63x 63x 63x 63x 63x 63x 63x 63x 8x 8x 8x 8x 21x 7x 1x 1x 1x 8x 8x 63x 13x 16x 16x 16x 16x 16x 16x 16x 16x 16x 63x 32x 13x 13x 19x 19x 57x 19x 63x 19x 63x 1x 1x 144x 38x | import { SearchOutlined } from "@mui/icons-material";
import {
Autocomplete as MuiAutocomplete,
InputAdornment,
styled,
} from "@mui/material";
import StyledLink from "components/StyledLink";
import TextField from "components/TextField";
import useAccountInfo from "features/auth/useAccountInfo";
import { Trans, useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import { useRouter } from "next/router";
import Sentry from "platform/sentry";
import { CommunitySummary } from "proto/communities_pb";
import { useEffect, useMemo, useState } from "react";
import { communityCreationFormURL, routeToCommunity } from "routes";
import { listAllCommunities } from "service/communities";
interface GroupedCommunity extends CommunitySummary.AsObject {
regionName?: string;
}
const StyledAutocomplete = styled(
MuiAutocomplete<GroupedCommunity, false, false, false>,
)(({ theme }) => ({
marginBottom: theme.spacing(3),
maxWidth: 600,
"& .MuiOutlinedInput-root": {
borderRadius: theme.spacing(3),
},
}));
export default function CommunitySearch() {
const { t, i18n } = useTranslation(COMMUNITIES);
const router = useRouter();
const { data: accountInfo } = useAccountInfo();
const [inputValue, setInputValue] = useState("");
const [allCommunities, setAllCommunities] = useState<GroupedCommunity[]>([]);
const [filteredOptions, setFilteredOptions] = useState<GroupedCommunity[]>(
[],
);
const [loading, setLoading] = useState(true);
// Fetch all communities on mount
useEffect(() => {
const fetchAllCommunities = async () => {
try {
setLoading(true);
// Use the new ListAllCommunities API - single call returns everything!
const response = await listAllCommunities();
// Map communities and extract immediate parent name from parents (last parent is the immediate parent)
const communitiesWithRegion: GroupedCommunity[] =
response.communitiesList.map((community) => ({
...community,
regionName:
community.parentsList && community.parentsList.length > 0
? community.parentsList[community.parentsList.length - 1]
.community?.name || ""
: "",
}));
setAllCommunities(communitiesWithRegion);
} catch (error) {
Sentry.captureException(error, {
tags: {
component: "CommunitySearch",
action: "fetchAllCommunities",
},
});
setAllCommunities([]);
setFilteredOptions([]);
} finally {
setLoading(false);
}
};
fetchAllCommunities();
}, []);
// Sort hierarchically: depth, then full parent path, then name.
// Recomputes on locale change without refetching.
const sortedCommunities = useMemo(
() =>
[...allCommunities].sort((a, b) => {
const depthCompare = a.parentsList.length - b.parentsList.length;
Iif (depthCompare !== 0) return depthCompare;
const aPath = a.parentsList
.map((p) => p.community?.name || "")
.join("/");
const bPath = b.parentsList
.map((p) => p.community?.name || "")
.join("/");
const pathCompare = aPath.localeCompare(bPath, i18n.language);
Iif (pathCompare !== 0) return pathCompare;
return a.name.localeCompare(b.name, i18n.language);
}),
[allCommunities, i18n.language],
);
// Filter communities based on input
useEffect(() => {
if (!inputValue) {
setFilteredOptions(sortedCommunities);
return;
}
const lowercaseInput = inputValue.toLowerCase();
const filtered = sortedCommunities.filter(
(community) =>
community.name.toLowerCase().includes(lowercaseInput) ||
(community.regionName &&
community.regionName.toLowerCase().includes(lowercaseInput)),
);
setFilteredOptions(filtered);
}, [inputValue, sortedCommunities]);
const handleInputChange = (_event: React.SyntheticEvent, value: string) => {
setInputValue(value);
};
const handleChange = (
_event: React.SyntheticEvent,
value: GroupedCommunity | null,
) => {
if (value) {
router.push(routeToCommunity(value.communityId, value.slug));
}
};
return (
<StyledAutocomplete
options={filteredOptions}
loading={loading}
inputValue={inputValue}
onInputChange={handleInputChange}
onChange={handleChange}
getOptionLabel={(option) => option.name}
groupBy={(option) => option.regionName || ""}
noOptionsText={
<Trans
t={t}
i18nKey="communities:no_results_found_with_link"
components={[
<StyledLink
href={communityCreationFormURL(accountInfo?.username)}
target="_blank"
rel="noreferrer noopener"
key="request-link"
/>,
]}
/>
}
renderInput={(params) => (
<TextField
{...params}
label={t("communities:search_communities")}
variant="outlined"
placeholder={t("communities:search_communities_placeholder")}
helperText={t("communities:search_communities_helper")}
slotProps={{
input: {
...params.InputProps,
startAdornment: (
<>
<InputAdornment position="start">
<SearchOutlined color="action" />
</InputAdornment>
{params.InputProps.startAdornment}
</>
),
},
}}
/>
)}
/>
);
}
|