@@ -4,6 +4,7 @@ import { useShortcutsStore } from "@/stores/shortcutsStore";
44import type { QueryHits , SearchDocument } from "@/types/search" ;
55import platformAdapter from "@/utils/platformAdapter" ;
66import { useSearchStore } from "@/stores/searchStore" ;
7+ import { isNumber } from "lodash-es" ;
78
89interface UseKeyboardNavigationProps {
910 suggests : QueryHits [ ] ;
@@ -16,6 +17,7 @@ interface UseKeyboardNavigationProps {
1617 handleItemAction : ( item : SearchDocument ) => void ;
1718 isChatMode : boolean ;
1819 formatUrl ?: ( item : any ) => string ;
20+ searchData : Record < string , QueryHits [ ] > ;
1921}
2022
2123export function useKeyboardNavigation ( {
@@ -29,6 +31,7 @@ export function useKeyboardNavigation({
2931 handleItemAction,
3032 isChatMode,
3133 formatUrl,
34+ searchData,
3235} : UseKeyboardNavigationProps ) {
3336 const openPopover = useShortcutsStore ( ( state ) => state . openPopover ) ;
3437 const visibleContextMenu = useSearchStore ( ( state ) => {
@@ -47,6 +50,20 @@ export function useKeyboardNavigation({
4750 return metaKeyPressed || ctrlKeyPressed || altKeyPressed ;
4851 } ;
4952
53+ const getGroupContext = ( ) => {
54+ const groupEntries = Object . entries ( searchData ) ;
55+ const groupIndex = groupEntries . findIndex ( ( [ _ , value ] ) => {
56+ return value . some ( ( item ) => {
57+ return item . document . index === selectedIndex ;
58+ } ) ;
59+ } ) ;
60+
61+ return {
62+ groupEntries,
63+ groupIndex,
64+ } ;
65+ } ;
66+
5067 const handleKeyDown = useCallback (
5168 ( e : KeyboardEvent ) => {
5269 if ( isChatMode || ! suggests . length || openPopover || visibleContextMenu ) {
@@ -59,13 +76,28 @@ export function useKeyboardNavigation({
5976
6077 if ( e . key === "ArrowUp" ) {
6178 e . preventDefault ( ) ;
62- // console.log("ArrowUp pressed", selectedIndex, suggests.length);
79+
80+ let nextIndex : number | undefined = void 0 ;
81+
82+ if ( modifierKeyPressed ) {
83+ const { groupEntries, groupIndex } = getGroupContext ( ) ;
84+
85+ const nextGroupIndex =
86+ groupIndex > 0 ? groupIndex - 1 : groupEntries . length - 1 ;
87+
88+ nextIndex = groupEntries [ nextGroupIndex ] [ 1 ] [ 0 ] . document . index ;
89+ }
90+
6391 setSelectedIndex ( ( prev ) => {
6492 if ( prev == null ) {
6593 return Math . min ( ...indexes ) ;
6694 }
6795
68- const nextIndex = prev - 1 ;
96+ if ( isNumber ( nextIndex ) ) {
97+ return nextIndex ;
98+ }
99+
100+ nextIndex = prev - 1 ;
69101
70102 if ( indexes . includes ( nextIndex ) ) {
71103 return nextIndex ;
@@ -75,13 +107,28 @@ export function useKeyboardNavigation({
75107 } ) ;
76108 } else if ( e . key === "ArrowDown" ) {
77109 e . preventDefault ( ) ;
78- //console.log("ArrowDown pressed", selectedIndex, suggests.length);
110+
111+ let nextIndex : number | undefined = void 0 ;
112+
113+ if ( modifierKeyPressed ) {
114+ const { groupEntries, groupIndex } = getGroupContext ( ) ;
115+
116+ const nextGroupIndex =
117+ groupIndex < groupEntries . length - 1 ? groupIndex + 1 : 0 ;
118+
119+ nextIndex = groupEntries [ nextGroupIndex ] [ 1 ] [ 0 ] . document . index ;
120+ }
121+
79122 setSelectedIndex ( ( prev ) => {
80123 if ( prev == null ) {
81124 return Math . min ( ...indexes ) ;
82125 }
83126
84- const nextIndex = prev + 1 ;
127+ if ( isNumber ( nextIndex ) ) {
128+ return nextIndex ;
129+ }
130+
131+ nextIndex = prev + 1 ;
85132
86133 if ( indexes . includes ( nextIndex ) ) {
87134 return nextIndex ;
0 commit comments