@@ -18,14 +18,18 @@ import { roomToUnreadAtom } from '$state/room/roomToUnread';
1818import { useKeyDown } from '$hooks/useKeyDown' ;
1919import { getDirectRoomPath , getHomeRoomPath , getSpaceRoomPath } from '$pages/pathUtils' ;
2020import { HOME_ROOM_PATH , DIRECT_ROOM_PATH , SPACE_ROOM_PATH } from '$pages/paths' ;
21- import { getCanonicalAliasOrRoomId } from '$utils/matrix' ;
21+ import { getCanonicalAliasOrRoomId , getCanonicalAliasRoomId } from '$utils/matrix' ;
2222import { announce } from '$utils/announce' ;
2323import {
2424 roomIdToReplyDraftAtomFamily ,
2525 roomIdToEditNavRequestAtomFamily ,
2626 type IEditNavRequest ,
2727} from '$state/room/roomInputDrafts' ;
2828import type { Room } from '$types/matrix-sdk' ;
29+ import {
30+ getMessageSearchShortcutPath ,
31+ getSelectedSpaceIdOrAliasFromPath ,
32+ } from '$features/search/searchShortcut' ;
2933
3034// Stable fallback atom used when no room is active — prevents atomFamily from
3135// creating a spurious entry under the empty-string key ''.
@@ -41,19 +45,26 @@ export function GlobalKeyboardShortcuts() {
4145 const unreadIndexRef = useRef ( 0 ) ;
4246
4347 // Derive the current room ID from the URL so we know which room is active.
44- const roomMatch =
45- matchPath ( HOME_ROOM_PATH , location . pathname ) ??
46- matchPath ( DIRECT_ROOM_PATH , location . pathname ) ??
47- matchPath ( SPACE_ROOM_PATH , location . pathname ) ;
48+ const homeRoomMatch = matchPath ( HOME_ROOM_PATH , location . pathname ) ;
49+ const directRoomMatch = matchPath ( DIRECT_ROOM_PATH , location . pathname ) ;
50+ const spaceRoomMatch = matchPath ( SPACE_ROOM_PATH , location . pathname ) ;
51+ const roomMatch = homeRoomMatch ?? directRoomMatch ?? spaceRoomMatch ;
4852 const roomIdOrAlias = roomMatch ?. params . roomIdOrAlias
4953 ? decodeURIComponent ( roomMatch . params . roomIdOrAlias )
5054 : undefined ;
55+ const selectedSpaceIdOrAlias = getSelectedSpaceIdOrAliasFromPath ( location . pathname ) ;
56+ const selectedSpaceId =
57+ selectedSpaceIdOrAlias && ! selectedSpaceIdOrAlias . startsWith ( '!' )
58+ ? mx . getRooms ( ) . find ( ( r ) => r . getCanonicalAlias ( ) === selectedSpaceIdOrAlias ) ?. roomId
59+ : selectedSpaceIdOrAlias ;
5160 let currentRoom : Room | null = null ;
61+
5262 if ( roomIdOrAlias ) {
5363 if ( roomIdOrAlias . startsWith ( '!' ) ) {
5464 currentRoom = mx . getRoom ( roomIdOrAlias ) ;
5565 } else {
56- currentRoom = mx . getRooms ( ) . find ( ( r ) => r . getCanonicalAlias ( ) === roomIdOrAlias ) ?? null ;
66+ const aliasedRoomId = getCanonicalAliasRoomId ( mx , roomIdOrAlias ) ;
67+ currentRoom = aliasedRoomId ? mx . getRoom ( aliasedRoomId ) : null ;
5768 }
5869 }
5970 const replyDraftAtomFamily = roomIdToReplyDraftAtomFamily ( currentRoom ?. roomId ?? '' ) ;
@@ -178,10 +189,37 @@ export function GlobalKeyboardShortcuts() {
178189 [ currentRoom , setEditNavRequest ]
179190 ) ;
180191
192+ /** Ctrl+F: Search for messages */
193+ const handleSearchMessageInRoom = useCallback (
194+ ( evt : KeyboardEvent ) => {
195+ if ( ! isKeyHotkey ( 'mod+f' , evt ) ) return ;
196+
197+ const path = getMessageSearchShortcutPath ( {
198+ pathname : location . pathname ,
199+ currentSearch : location . search ,
200+ selectedSpaceId : selectedSpaceId ?? undefined ,
201+ currentRoomId : currentRoom ?. roomId ,
202+ } ) ;
203+ if ( ! path ) return ;
204+
205+ const portalContainer = document . getElementById ( 'portalContainer' ) ;
206+ if ( portalContainer && portalContainer . children . length > 0 ) {
207+ return ;
208+ }
209+
210+ evt . preventDefault ( ) ;
211+ const roomName = mx . getRoom ( currentRoom ?. roomId ) ?. name ;
212+ navigate ( path ) ;
213+ announce ( `Start Searching messages ${ roomName ? `in ${ roomName } ` : '' } ` ) ;
214+ } ,
215+ [ mx , currentRoom , navigate , location . pathname , location . search , selectedSpaceId ]
216+ ) ;
217+
181218 useKeyDown ( window , handleNextUnreadKeyDown ) ;
182219 useKeyDown ( window , handleUnreadNavKeyDown ) ;
183220 useKeyDown ( window , handleReplyKeyDown ) ;
184221 useKeyDown ( window , handleEditKeyDown ) ;
222+ useKeyDown ( window , handleSearchMessageInRoom ) ;
185223
186224 return null ;
187225}
0 commit comments