@@ -55,6 +55,7 @@ import { useKeyDown } from '$hooks/useKeyDown';
5555import { useMediaAuthentication } from '$hooks/useMediaAuthentication' ;
5656import { KeySymbol } from '$utils/key-symbol' ;
5757import { isMacOS } from '$utils/user-agent' ;
58+ import { useSelectedSpace } from '$hooks/router/useSelectedSpace' ;
5859
5960enum SearchRoomType {
6061 Rooms = '#' ,
@@ -167,7 +168,19 @@ export function Search({ requestClose }: SearchProps) {
167168 ) ;
168169
169170 const [ result , search , resetSearch ] = useAsyncSearch ( targetRooms , getTargetStr , SEARCH_OPTIONS ) ;
170- const roomsToRender = result ? result . items : topActiveRooms ;
171+ const selectedSpaceId = useSelectedSpace ( ) ;
172+
173+ const roomsToRender = useMemo ( ( ) => {
174+ const items = result ? result . items : topActiveRooms ;
175+ if ( ! selectedSpaceId ) return items ;
176+
177+ return [ ...items ] . sort ( ( a , b ) => {
178+ const aInSpace = getAllParents ( roomToParents , a ) ?. has ( selectedSpaceId ) ? 1 : 0 ;
179+ const bInSpace = getAllParents ( roomToParents , b ) ?. has ( selectedSpaceId ) ? 1 : 0 ;
180+ return bInSpace - aInSpace ;
181+ } ) ;
182+ } , [ result , topActiveRooms , selectedSpaceId , roomToParents ] ) ;
183+
171184 const listFocus = useListFocusIndex ( roomsToRender . length , 0 ) ;
172185
173186 const queryHighlighRegex = result ?. query
0 commit comments