Skip to content

Commit

Permalink
refactor: improved readability of input handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Kirchhoff committed Dec 7, 2023
1 parent 0499dd9 commit 66d6c03
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions src/useCmdBarState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,15 @@ const useCmdBarState = {

async updateQuery(query: string, fuseOptions?: Partial<UseFuseOptions<Command>>): Promise<void> {
state.query = query

if (query === '') {
this.filterGroupedCommands()
return
}

if (state.selectedGroups.size === 0) {
// Search all groups
await searchGroups(query, state.groupedCommands, fuseOptions)
} else if (state.selectedGroups.size === 1) {
// Search a single selected group
const group = state.groupedCommands.find((group) => state.selectedGroups.has(group.key))
if (group) {
await searchGroups(query, [group], fuseOptions)
} else {
console.warn(`Group ${state.selectedGroups.values().next()} not found`)
}
} else {
// Search multiple selected groups
const groups = state.groupedCommands.filter((group) => state.selectedGroups.has(group.key))
await searchGroups(query, groups, fuseOptions)
let groupsToSearch = getGroupsToSearch()

if (groupsToSearch) {
await searchGroups(query, groupsToSearch, fuseOptions)
}
},

Expand All @@ -139,8 +127,15 @@ const searchGroups = async (
groups: Group[],
fuseOptions?: Partial<UseFuseOptions<Command>>
) => {
const fuzzySearchableGroups = groups.filter((group) => !group.search)
const asyncSearchableGroups = groups.filter((group) => !!group.search)
const fuzzySearchableGroups: Group[] = []
const asyncSearchableGroups: Group[] = []
groups.forEach((group) => {
if (group.search) {
asyncSearchableGroups.push(group)
} else {
fuzzySearchableGroups.push(group)
}
})

if (fuzzySearchableGroups.length > 0) {
fuzzySearch(query, fuzzySearchableGroups, fuseOptions)
Expand Down Expand Up @@ -213,6 +208,33 @@ function selectFirstCommand(): void {
}
}

function getGroupsToSearch(): Group[] | undefined {
let groupsToSearch

if (state.selectedGroups.size === 0) {
groupsToSearch = state.groupedCommands
} else if (state.selectedGroups.size === 1) {
const group = findSingleSelectedGroup()
if (group) {
groupsToSearch = [group]
} else {
console.warn(`Group ${state.selectedGroups.values().next()} not found`)
}
} else {
groupsToSearch = findMultipleSelectedGroups()
}

return groupsToSearch
}

function findSingleSelectedGroup(): Group | undefined {
return state.groupedCommands.find((group) => state.selectedGroups.has(group.key))
}

function findMultipleSelectedGroups(): Group[] {
return state.groupedCommands.filter((group) => state.selectedGroups.has(group.key))
}

/**
* watcher to assign new flatMap to filteredCommands when filteredGroupedCommands changes
* and select the first command
Expand Down

0 comments on commit 66d6c03

Please sign in to comment.