@@ -12,7 +12,7 @@ import { COMMUNITIES } from "i18n/namespaces";
1212import { useRouter } from "next/router" ;
1313import Sentry from "platform/sentry" ;
1414import { CommunitySummary } from "proto/communities_pb" ;
15- import { useEffect , useState } from "react" ;
15+ import { useEffect , useMemo , useState } from "react" ;
1616import { communityCreationFormURL , routeToCommunity } from "routes" ;
1717import { listAllCommunities } from "service/communities" ;
1818
@@ -31,7 +31,7 @@ const StyledAutocomplete = styled(
3131} ) ) ;
3232
3333export default function CommunitySearch ( ) {
34- const { t } = useTranslation ( COMMUNITIES ) ;
34+ const { t, i18n } = useTranslation ( COMMUNITIES ) ;
3535 const router = useRouter ( ) ;
3636 const { data : accountInfo } = useAccountInfo ( ) ;
3737 const [ inputValue , setInputValue ] = useState ( "" ) ;
@@ -60,28 +60,7 @@ export default function CommunitySearch() {
6060 : "" ,
6161 } ) ) ;
6262
63- // Sort hierarchically: first by depth (number of parents), then by full parent path, then by name
64- const sortedCommunities = communitiesWithRegion . sort ( ( a , b ) => {
65- // First, sort by depth in hierarchy (fewer parents = higher in tree)
66- const depthCompare = a . parentsList . length - b . parentsList . length ;
67- if ( depthCompare !== 0 ) return depthCompare ;
68-
69- // Then sort by the full path through the hierarchy
70- const aPath = a . parentsList
71- . map ( ( p ) => p . community ?. name || "" )
72- . join ( "/" ) ;
73- const bPath = b . parentsList
74- . map ( ( p ) => p . community ?. name || "" )
75- . join ( "/" ) ;
76- const pathCompare = aPath . localeCompare ( bPath ) ;
77- if ( pathCompare !== 0 ) return pathCompare ;
78-
79- // Finally sort by community name
80- return a . name . localeCompare ( b . name ) ;
81- } ) ;
82-
83- setAllCommunities ( sortedCommunities ) ;
84- setFilteredOptions ( sortedCommunities ) ;
63+ setAllCommunities ( communitiesWithRegion ) ;
8564 } catch ( error ) {
8665 Sentry . captureException ( error , {
8766 tags : {
@@ -99,22 +78,42 @@ export default function CommunitySearch() {
9978 fetchAllCommunities ( ) ;
10079 } , [ ] ) ;
10180
81+ // Sort hierarchically: depth, then full parent path, then name.
82+ // Recomputes on locale change without refetching.
83+ const sortedCommunities = useMemo (
84+ ( ) =>
85+ [ ...allCommunities ] . sort ( ( a , b ) => {
86+ const depthCompare = a . parentsList . length - b . parentsList . length ;
87+ if ( depthCompare !== 0 ) return depthCompare ;
88+ const aPath = a . parentsList
89+ . map ( ( p ) => p . community ?. name || "" )
90+ . join ( "/" ) ;
91+ const bPath = b . parentsList
92+ . map ( ( p ) => p . community ?. name || "" )
93+ . join ( "/" ) ;
94+ const pathCompare = aPath . localeCompare ( bPath , i18n . language ) ;
95+ if ( pathCompare !== 0 ) return pathCompare ;
96+ return a . name . localeCompare ( b . name , i18n . language ) ;
97+ } ) ,
98+ [ allCommunities , i18n . language ] ,
99+ ) ;
100+
102101 // Filter communities based on input
103102 useEffect ( ( ) => {
104103 if ( ! inputValue ) {
105- setFilteredOptions ( allCommunities ) ;
104+ setFilteredOptions ( sortedCommunities ) ;
106105 return ;
107106 }
108107
109108 const lowercaseInput = inputValue . toLowerCase ( ) ;
110- const filtered = allCommunities . filter (
109+ const filtered = sortedCommunities . filter (
111110 ( community ) =>
112111 community . name . toLowerCase ( ) . includes ( lowercaseInput ) ||
113112 ( community . regionName &&
114113 community . regionName . toLowerCase ( ) . includes ( lowercaseInput ) ) ,
115114 ) ;
116115 setFilteredOptions ( filtered ) ;
117- } , [ inputValue , allCommunities ] ) ;
116+ } , [ inputValue , sortedCommunities ] ) ;
118117
119118 const handleInputChange = ( _event : React . SyntheticEvent , value : string ) => {
120119 setInputValue ( value ) ;
0 commit comments