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 f95cb6f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
9 changes: 5 additions & 4 deletions components/ChainSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useContext, useEffect, useMemo, useState, useCallback } from 'react'
import { useContext, useMemo, useState, useCallback } from 'react'

import { useRegisterActions, Action } from 'kbar'
import { useRegisterActions } from 'kbar'
import { useRouter } from 'next/router'
import Select, { OnChangeValue, components } from 'react-select'

import { EthereumContext } from 'context/ethereumContext'

import { CURRENT_FORK } from 'util/constants'
import { toKeyIndex } from 'util/string'

import { Icon, Label } from 'components/ui'

Expand Down Expand Up @@ -64,7 +63,9 @@ const ChainOption = (props: any) => {

const ChainSelector = () => {
const { forks, selectedFork, onForkChange } = useContext(EthereumContext)
const [forkValue, setForkValue] = useState(null)
const [forkValue, setForkValue] = useState(() => {
return forks.find((fork) => fork.name === CURRENT_FORK) || null
})
const router = useRouter()

const forkOptions = useMemo(
Expand Down
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 f95cb6f

Please sign in to comment.