@@ -11,45 +11,6 @@ import { toKeyIndex } from 'util/string'
1111
1212import { 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-
5314const ChainOption = ( props : any ) => {
5415 const { data, children } = props
5516 const isCurrent = data . value === CURRENT_FORK
@@ -64,14 +25,21 @@ const ChainOption = (props: any) => {
6425
6526const 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 }
0 commit comments