Skip to content

Commit

Permalink
chore(Kbar): results fix
Browse files Browse the repository at this point in the history
  • Loading branch information
developerfred committed Jan 29, 2024
1 parent 81a1d68 commit 2217337
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions components/KBar/Results.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'react'

import { KBarResults, useMatches } from 'kbar'
import type { ActionImpl } from 'kbar'

import ResultItem from './ResultItem'

Expand All @@ -10,20 +11,29 @@ const Results = () => {
const { results } = useMatches()

const flattened = useMemo(() => {
return results.reduce((acc: any[], curr: any) => {
if (typeof curr === 'string') {
acc.push(curr)
} else {
acc.push(curr.name)
acc.push(...curr.actions)
}
return acc
}, [])
const flattenActions = (actions: (string | ActionImpl)[]) => {
return actions.reduce((acc: any[], curr: string | ActionImpl) => {
if (typeof curr === 'string') {
acc.push(curr)
} else {
acc.push(curr)

if (curr.children && curr.children.length > 0) {
acc.push(...flattenActions(curr.children))
}
}
return acc
}, [])
}

return flattenActions(results)
}, [results])

return (
<KBarResults
items={flattened.filter((i: string) => i !== NO_GROUP)}
items={flattened.filter(
(i: any) => typeof i !== 'string' || i !== NO_GROUP,
)}
onRender={({ item, active }) =>
typeof item === 'string' ? (
<div className="px-4 py-2 text-2xs uppercase text-gray-400 dark:text-gray-600 bg-white dark:bg-black-600">
Expand Down

0 comments on commit 2217337

Please sign in to comment.