Skip to content

Commit

Permalink
fix(CommandPalette): improve performances and avoid multiple recompute
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed Dec 8, 2023
1 parent ad33b26 commit db508b2
Showing 1 changed file with 44 additions and 36 deletions.
80 changes: 44 additions & 36 deletions src/runtime/components/navigation/CommandPalette.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
</template>

<script lang="ts">
import { ref, computed, watch, toRef, onMounted, defineComponent } from 'vue'
import { ref, computed, watch, toRef, onMounted, defineComponent, toRaw } from 'vue'
import { Combobox as HCombobox, ComboboxInput as HComboboxInput, ComboboxOptions as HComboboxOptions } from '@headlessui/vue'
import type { ComputedRef, PropType, ComponentPublicInstance } from 'vue'
import { useDebounceFn } from '@vueuse/core'
Expand Down Expand Up @@ -227,52 +227,60 @@ export default defineComponent({
const { results } = useFuse(query, commands, options)
const groups = computed(() => {
const groups: Group[] = []
const groupedCommands: Record<string, typeof results['value']> = {}
for (const command of results.value) {
if (!command.item.group) {
continue
}
groupedCommands[command.item.group] ||= []
groupedCommands[command.item.group].push(command)
function getGroupWithCommands (group: Group, commands: Command[]) {
if (!group) {
return
}
for (const key in groupedCommands) {
const group = props.groups.find(group => group.key === key)
if (!group) {
continue
}
let commands = groupedCommands[key].map((result) => {
const { item, ...data } = result
const q = toRaw(query)
let newCommands = [...commands]
return {
...item,
...data
} as Command
})
if (group.filter && typeof group.filter === 'function') {
newCommands = group.filter(q, newCommands)
}
if (group.filter && typeof group.filter === 'function') {
commands = group.filter(query.value, commands)
}
return {
...group,
commands: newCommands.slice(0, options.value.resultLimit)
}
}
groups.push({ ...group, commands: commands.slice(0, options.value.resultLimit) })
const groups = computed(() => {
if (!results.value) {
return []
}
for (const group of props.groups) {
if (group.search && searchResults.value[group.key]?.length) {
let commands = (searchResults.value[group.key] || [])
const groupedCommands: Record<string, Command[]> = results.value.reduce((acc, command) => {
const { item, ...data } = command
if (!item.group) {
return acc
}
if (group.filter && typeof group.filter === 'function') {
commands = group.filter(query.value, commands)
}
acc[command.item.group] ||= []
acc[command.item.group].push({ ...item, ...data })
groups.push({ ...group, commands: commands.slice(0, options.value.resultLimit) })
return acc
}, {})
const groups: Group[] = Object.entries(groupedCommands).map(([key, commands]) => {
const group = props.groups.find(group => group.key === key)
if (!group) {
return null
}
}
return groups
return getGroupWithCommands(group, commands)
}).filter(Boolean)
const searchGroups = props.groups.filter(group => !!group.search && searchResults.value[group.key]?.length).map(group => {
const commands = (searchResults.value[group.key] || [])
return getGroupWithCommands(group, commands)
})
return [
...groups,
...searchGroups
]
})
const debouncedSearch = useDebounceFn(async () => {
Expand Down

1 comment on commit db508b2

@vercel
Copy link

@vercel vercel bot commented on db508b2 Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui-nuxt-js.vercel.app
ui-git-dev-nuxt-js.vercel.app
ui.nuxt.com

Please sign in to comment.