Skip to content

Commit f95cb6f

Browse files
committed
chore(Kbar): results fix
1 parent 81a1d68 commit f95cb6f

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

components/ChainSelector.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { useContext, useEffect, useMemo, useState, useCallback } from 'react'
1+
import { useContext, useMemo, useState, useCallback } from 'react'
22

3-
import { useRegisterActions, Action } from 'kbar'
3+
import { useRegisterActions } from 'kbar'
44
import { useRouter } from 'next/router'
55
import Select, { OnChangeValue, components } from 'react-select'
66

77
import { EthereumContext } from 'context/ethereumContext'
88

99
import { CURRENT_FORK } from 'util/constants'
10-
import { toKeyIndex } from 'util/string'
1110

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

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

6564
const ChainSelector = () => {
6665
const { forks, selectedFork, onForkChange } = useContext(EthereumContext)
67-
const [forkValue, setForkValue] = useState(null)
66+
const [forkValue, setForkValue] = useState(() => {
67+
return forks.find((fork) => fork.name === CURRENT_FORK) || null
68+
})
6869
const router = useRouter()
6970

7071
const forkOptions = useMemo(

components/KBar/Results.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useMemo } from 'react'
22

33
import { KBarResults, useMatches } from 'kbar'
4+
import type { ActionImpl } from 'kbar'
45

56
import ResultItem from './ResultItem'
67

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

1213
const flattened = useMemo(() => {
13-
return results.reduce((acc: any[], curr: any) => {
14-
if (typeof curr === 'string') {
15-
acc.push(curr)
16-
} else {
17-
acc.push(curr.name)
18-
acc.push(...curr.actions)
19-
}
20-
return acc
21-
}, [])
14+
const flattenActions = (actions: (string | ActionImpl)[]) => {
15+
return actions.reduce((acc: any[], curr: string | ActionImpl) => {
16+
if (typeof curr === 'string') {
17+
acc.push(curr)
18+
} else {
19+
acc.push(curr)
20+
21+
if (curr.children && curr.children.length > 0) {
22+
acc.push(...flattenActions(curr.children))
23+
}
24+
}
25+
return acc
26+
}, [])
27+
}
28+
29+
return flattenActions(results)
2230
}, [results])
2331

2432
return (
2533
<KBarResults
26-
items={flattened.filter((i: string) => i !== NO_GROUP)}
34+
items={flattened.filter(
35+
(i: any) => typeof i !== 'string' || i !== NO_GROUP,
36+
)}
2737
onRender={({ item, active }) =>
2838
typeof item === 'string' ? (
2939
<div className="px-4 py-2 text-2xs uppercase text-gray-400 dark:text-gray-600 bg-white dark:bg-black-600">

0 commit comments

Comments
 (0)