Skip to content

Commit 2b0017e

Browse files
committed
fix: remove requestAnimationFrame and improve search priority
- Replace requestAnimationFrame with direct state updates for better clarity - Implement two-tier search: prioritize mode names/slugs over descriptions - Mode names now always appear before description matches in search results
1 parent be8a560 commit 2b0017e

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

webview-ui/src/components/chat/ModeSelector.tsx

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,55 @@ export const ModeSelector = ({
6464
// Find the selected mode
6565
const selectedMode = React.useMemo(() => modes.find((mode) => mode.slug === value), [modes, value])
6666

67-
// Memoize searchable items for fuzzy search
68-
const searchableItems = React.useMemo(() => {
67+
// Memoize searchable items for fuzzy search with separate name and description search
68+
const nameSearchItems = React.useMemo(() => {
6969
return modes.map((mode) => ({
7070
original: mode,
71-
searchStr: [mode.name, mode.slug, mode.description].filter(Boolean).join(" "),
71+
searchStr: [mode.name, mode.slug].filter(Boolean).join(" "),
7272
}))
7373
}, [modes])
7474

75-
// Create a memoized Fzf instance
76-
const fzfInstance = React.useMemo(() => {
77-
return new Fzf(searchableItems, {
75+
const descriptionSearchItems = React.useMemo(() => {
76+
return modes.map((mode) => ({
77+
original: mode,
78+
searchStr: mode.description || "",
79+
}))
80+
}, [modes])
81+
82+
// Create memoized Fzf instances for name and description searches
83+
const nameFzfInstance = React.useMemo(() => {
84+
return new Fzf(nameSearchItems, {
7885
selector: (item) => item.searchStr,
7986
})
80-
}, [searchableItems])
87+
}, [nameSearchItems])
8188

82-
// Filter modes based on search value using fuzzy search
89+
const descriptionFzfInstance = React.useMemo(() => {
90+
return new Fzf(descriptionSearchItems, {
91+
selector: (item) => item.searchStr,
92+
})
93+
}, [descriptionSearchItems])
94+
95+
// Filter modes based on search value using fuzzy search with priority
8396
const filteredModes = React.useMemo(() => {
8497
if (!searchValue) return modes
8598

86-
const matchingItems = fzfInstance.find(searchValue).map((result) => result.item.original)
87-
return matchingItems
88-
}, [modes, searchValue, fzfInstance])
99+
// First search in names/slugs
100+
const nameMatches = nameFzfInstance.find(searchValue)
101+
const nameMatchedModes = new Set(nameMatches.map((result) => result.item.original.slug))
102+
103+
// Then search in descriptions
104+
const descriptionMatches = descriptionFzfInstance.find(searchValue)
105+
106+
// Combine results: name matches first, then description matches
107+
const combinedResults = [
108+
...nameMatches.map((result) => result.item.original),
109+
...descriptionMatches
110+
.filter((result) => !nameMatchedModes.has(result.item.original.slug))
111+
.map((result) => result.item.original),
112+
]
113+
114+
return combinedResults
115+
}, [modes, searchValue, nameFzfInstance, descriptionFzfInstance])
89116

90117
const onClearSearch = React.useCallback(() => {
91118
setSearchValue("")
@@ -97,7 +124,7 @@ export const ModeSelector = ({
97124
onChange(modeSlug as Mode)
98125
setOpen(false)
99126
// Clear search after selection
100-
requestAnimationFrame(() => setSearchValue(""))
127+
setSearchValue("")
101128
},
102129
[onChange],
103130
)
@@ -108,7 +135,7 @@ export const ModeSelector = ({
108135
setOpen(isOpen)
109136
// Clear search when closing
110137
if (!isOpen) {
111-
requestAnimationFrame(() => setSearchValue(""))
138+
setSearchValue("")
112139
}
113140
},
114141
[trackModeSelectorOpened],

0 commit comments

Comments
 (0)