From 7af0e4b6a45b113598729acdcf1b062553796bf8 Mon Sep 17 00:00:00 2001 From: codingsh Date: Mon, 29 Jan 2024 14:24:09 +0000 Subject: [PATCH] chore(Kbar): results fix --- components/ChainSelector.tsx | 93 ++++++++++++++++++++---------------- components/KBar/Results.tsx | 30 ++++++++---- 2 files changed, 71 insertions(+), 52 deletions(-) diff --git a/components/ChainSelector.tsx b/components/ChainSelector.tsx index 830e6c69..d8fdf29b 100644 --- a/components/ChainSelector.tsx +++ b/components/ChainSelector.tsx @@ -11,45 +11,6 @@ import { toKeyIndex } from 'util/string' import { Icon, Label } from 'components/ui' -interface ForkOption { - label: string -} - -type HandleForkChange = (option: ForkOption) => void - -const useBuildForkActions = ( - forkOptions: ForkOption[], - handleForkChange: HandleForkChange, -) => { - return useMemo(() => { - const forkIds = forkOptions.map((option, index) => - toKeyIndex('fork', index), - ) - - const forkActions = forkOptions.map((option, index) => ({ - id: toKeyIndex('fork', index), - name: option.label, - shortcut: [], - keywords: option.label, - section: '', - perform: () => handleForkChange(option), - parent: 'fork', - })) - - return [ - { - id: 'fork', - name: 'Select hardfork…', - shortcut: ['f'], - keywords: 'fork network evm', - section: 'Preferences', - children: forkIds, - }, - ...forkActions, - ] - }, [forkOptions, handleForkChange]) -} - const ChainOption = (props: any) => { const { data, children } = props const isCurrent = data.value === CURRENT_FORK @@ -64,7 +25,9 @@ const ChainOption = (props: any) => { const ChainSelector = () => { const { forks, selectedFork, onForkChange } = useContext(EthereumContext) - const [forkValue, setForkValue] = useState(null) + + const [forkValue, setForkValue] = useState() + const [actions, setActions] = useState([]) const router = useRouter() const forkOptions = useMemo( @@ -72,6 +35,11 @@ const ChainSelector = () => { [forks], ) + const defaultForkOption = useMemo( + () => forkOptions.find((fork) => fork.value === selectedFork?.name), + [forkOptions, selectedFork], + ) + const handleForkChange = useCallback( (option: OnChangeValue) => { setForkValue(option) @@ -80,10 +48,50 @@ const ChainSelector = () => { router.query.fork = option.value router.push(router) }, - [onForkChange, router], + // eslint-disable-next-line react-hooks/exhaustive-deps + [onForkChange], ) - const actions = useBuildForkActions(forkOptions, handleForkChange) + useEffect(() => { + if (defaultForkOption) { + handleForkChange(defaultForkOption) + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [defaultForkOption]) + + useEffect(() => { + const forkIds: string[] = [] + + const forkActions = forkOptions.map( + (option: OnChangeValue, index) => { + const keyId = toKeyIndex('fork', index) + forkIds.push(keyId) + + return { + id: keyId, + name: option.label, + shortcut: [], + keywords: option.label, + section: '', + perform: () => handleForkChange(option), + parent: 'fork', + } + }, + ) + + if (forkIds.length > 0) { + setActions([ + ...forkActions, + { + id: 'fork', + name: 'Select hardfork…', + shortcut: ['f'], + keywords: 'fork network evm', + section: 'Preferences', + }, + ]) + } + }, [forkOptions, handleForkChange]) useRegisterActions(actions, [actions]) @@ -92,6 +100,7 @@ const ChainSelector = () => { {forks.length > 0 && (
+