@@ -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 : {
@@ -90,7 +69,6 @@ export default function CommunitySearch() {
9069 } ,
9170 } ) ;
9271 setAllCommunities ( [ ] ) ;
93- setFilteredOptions ( [ ] ) ;
9472 } finally {
9573 setLoading ( false ) ;
9674 }
@@ -99,22 +77,48 @@ export default function CommunitySearch() {
9977 fetchAllCommunities ( ) ;
10078 } , [ ] ) ;
10179
80+ // Sort hierarchically: first by depth (number of parents), then by full
81+ // parent path, then by name. Honors the user's locale and re-sorts on
82+ // language change without re-fetching.
83+ const sortedCommunities = useMemo (
84+ ( ) =>
85+ [ ...allCommunities ] . sort ( ( a , b ) => {
86+ // First, sort by depth in hierarchy (fewer parents = higher in tree)
87+ const depthCompare = a . parentsList . length - b . parentsList . length ;
88+ if ( depthCompare !== 0 ) return depthCompare ;
89+
90+ // Then sort by the full path through the hierarchy
91+ const aPath = a . parentsList
92+ . map ( ( p ) => p . community ?. name || "" )
93+ . join ( "/" ) ;
94+ const bPath = b . parentsList
95+ . map ( ( p ) => p . community ?. name || "" )
96+ . join ( "/" ) ;
97+ const pathCompare = aPath . localeCompare ( bPath , i18n . language ) ;
98+ if ( pathCompare !== 0 ) return pathCompare ;
99+
100+ // Finally sort by community name
101+ return a . name . localeCompare ( b . name , i18n . language ) ;
102+ } ) ,
103+ [ allCommunities , i18n . language ] ,
104+ ) ;
105+
102106 // Filter communities based on input
103107 useEffect ( ( ) => {
104108 if ( ! inputValue ) {
105- setFilteredOptions ( allCommunities ) ;
109+ setFilteredOptions ( sortedCommunities ) ;
106110 return ;
107111 }
108112
109113 const lowercaseInput = inputValue . toLowerCase ( ) ;
110- const filtered = allCommunities . filter (
114+ const filtered = sortedCommunities . filter (
111115 ( community ) =>
112116 community . name . toLowerCase ( ) . includes ( lowercaseInput ) ||
113117 ( community . regionName &&
114118 community . regionName . toLowerCase ( ) . includes ( lowercaseInput ) ) ,
115119 ) ;
116120 setFilteredOptions ( filtered ) ;
117- } , [ inputValue , allCommunities ] ) ;
121+ } , [ inputValue , sortedCommunities ] ) ;
118122
119123 const handleInputChange = ( _event : React . SyntheticEvent , value : string ) => {
120124 setInputValue ( value ) ;
0 commit comments