Skip to content

Commit 7af0e4b

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

2 files changed

Lines changed: 71 additions & 52 deletions

File tree

components/ChainSelector.tsx

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,6 @@ import { toKeyIndex } from 'util/string'
1111

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

14-
interface ForkOption {
15-
label: string
16-
}
17-
18-
type HandleForkChange = (option: ForkOption) => void
19-
20-
const useBuildForkActions = (
21-
forkOptions: ForkOption[],
22-
handleForkChange: HandleForkChange,
23-
) => {
24-
return useMemo(() => {
25-
const forkIds = forkOptions.map((option, index) =>
26-
toKeyIndex('fork', index),
27-
)
28-
29-
const forkActions = forkOptions.map((option, index) => ({
30-
id: toKeyIndex('fork', index),
31-
name: option.label,
32-
shortcut: [],
33-
keywords: option.label,
34-
section: '',
35-
perform: () => handleForkChange(option),
36-
parent: 'fork',
37-
}))
38-
39-
return [
40-
{
41-
id: 'fork',
42-
name: 'Select hardfork…',
43-
shortcut: ['f'],
44-
keywords: 'fork network evm',
45-
section: 'Preferences',
46-
children: forkIds,
47-
},
48-
...forkActions,
49-
]
50-
}, [forkOptions, handleForkChange])
51-
}
52-
5314
const ChainOption = (props: any) => {
5415
const { data, children } = props
5516
const isCurrent = data.value === CURRENT_FORK
@@ -64,14 +25,21 @@ const ChainOption = (props: any) => {
6425

6526
const ChainSelector = () => {
6627
const { forks, selectedFork, onForkChange } = useContext(EthereumContext)
67-
const [forkValue, setForkValue] = useState(null)
28+
29+
const [forkValue, setForkValue] = useState()
30+
const [actions, setActions] = useState<Action[]>([])
6831
const router = useRouter()
6932

7033
const forkOptions = useMemo(
7134
() => forks.map((fork) => ({ value: fork.name, label: fork.name })),
7235
[forks],
7336
)
7437

38+
const defaultForkOption = useMemo(
39+
() => forkOptions.find((fork) => fork.value === selectedFork?.name),
40+
[forkOptions, selectedFork],
41+
)
42+
7543
const handleForkChange = useCallback(
7644
(option: OnChangeValue<any, any>) => {
7745
setForkValue(option)
@@ -80,10 +48,50 @@ const ChainSelector = () => {
8048
router.query.fork = option.value
8149
router.push(router)
8250
},
83-
[onForkChange, router],
51+
// eslint-disable-next-line react-hooks/exhaustive-deps
52+
[onForkChange],
8453
)
8554

86-
const actions = useBuildForkActions(forkOptions, handleForkChange)
55+
useEffect(() => {
56+
if (defaultForkOption) {
57+
handleForkChange(defaultForkOption)
58+
}
59+
// eslint-disable-next-line react-hooks/exhaustive-deps
60+
}, [defaultForkOption])
61+
62+
useEffect(() => {
63+
const forkIds: string[] = []
64+
65+
const forkActions = forkOptions.map(
66+
(option: OnChangeValue<any, any>, index) => {
67+
const keyId = toKeyIndex('fork', index)
68+
forkIds.push(keyId)
69+
70+
return {
71+
id: keyId,
72+
name: option.label,
73+
shortcut: [],
74+
keywords: option.label,
75+
section: '',
76+
perform: () => handleForkChange(option),
77+
parent: 'fork',
78+
}
79+
},
80+
)
81+
82+
if (forkIds.length > 0) {
83+
setActions([
84+
...forkActions,
85+
{
86+
id: 'fork',
87+
name: 'Select hardfork…',
88+
shortcut: ['f'],
89+
keywords: 'fork network evm',
90+
section: 'Preferences',
91+
},
92+
])
93+
}
94+
}, [forkOptions, handleForkChange])
8795

8896
useRegisterActions(actions, [actions])
8997

@@ -92,6 +100,7 @@ const ChainSelector = () => {
92100
{forks.length > 0 && (
93101
<div className="flex items-center mr-2">
94102
<Icon name="git-branch-line" className="text-indigo-500 mr-2" />
103+
95104
<Select
96105
onChange={handleForkChange}
97106
options={forkOptions}

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)