Skip to content

Commit f03ad8a

Browse files
authored
feat: support switching groups via keyboard shortcuts (#911)
* feat: support switching groups via keyboard shortcuts * refactor: update * docs: update changelog
1 parent 386ebb6 commit f03ad8a

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

docs/content.en/docs/release-notes/_index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ title: "Release Notes"
88
Information about release notes of Coco App is provided here.
99

1010
## Latest (In development)
11+
1112
### ❌ Breaking changes
13+
1214
### 🚀 Features
15+
16+
feat: support switching groups via keyboard shortcuts #911
17+
1318
### 🐛 Bug fix
19+
1420
### ✈️ Improvements
1521

22+
refactor: improve sorting logic of search results #910
1623

1724
## 0.8.0 (2025-09-28)
1825

src/components/Search/DropdownList.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ function DropdownList({
145145
handleItemAction,
146146
isChatMode,
147147
formatUrl,
148+
searchData,
148149
});
149150

150151
return (

src/hooks/useKeyboardNavigation.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useShortcutsStore } from "@/stores/shortcutsStore";
44
import type { QueryHits, SearchDocument } from "@/types/search";
55
import platformAdapter from "@/utils/platformAdapter";
66
import { useSearchStore } from "@/stores/searchStore";
7+
import { isNumber } from "lodash-es";
78

89
interface 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

2123
export 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

Comments
 (0)