-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow node selection after connection error
- Loading branch information
1 parent
b01aa28
commit bb477dd
Showing
8 changed files
with
259 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Combobox as HeadlessComboBox, Transition } from '@headlessui/react'; | ||
import { Fragment } from 'react'; | ||
import { FiChevronDown } from 'react-icons/fi'; | ||
|
||
const Combobox = ({ | ||
options, | ||
value, | ||
onValueChange, | ||
}: { | ||
options?: string[]; | ||
value?: string; | ||
onValueChange: (newValue: string) => void; | ||
}) => { | ||
return ( | ||
<HeadlessComboBox value={value ?? ''} onChange={onValueChange}> | ||
<div className="relative mt-1"> | ||
<div className="relative w-full cursor-default overflow-hidden rounded-lg border-solid border-[#d8d8d8] border-[1px] text-left"> | ||
<HeadlessComboBox.Input | ||
className="w-full border-none py-2 pl-3 pr-10 text-sm leading-5 text-gray-900 focus:ring-0 focus-visible:outline-none" | ||
onChange={event => onValueChange(event.target.value)} | ||
/> | ||
{options && ( | ||
<HeadlessComboBox.Button className="absolute inset-y-0 right-0 flex items-center pr-2"> | ||
<FiChevronDown | ||
className="h-5 w-5 text-gray-400" | ||
aria-hidden="true" | ||
/> | ||
</HeadlessComboBox.Button> | ||
)} | ||
</div> | ||
{options && ( | ||
<Transition | ||
as={Fragment} | ||
leave="transition ease-in duration-100" | ||
leaveFrom="opacity-100" | ||
leaveTo="opacity-0" | ||
> | ||
<HeadlessComboBox.Options className="z-50 absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black/5 focus:outline-none sm:text-sm"> | ||
{options?.map(option => ( | ||
<HeadlessComboBox.Option | ||
key={option} | ||
value={option} | ||
className="relative cursor-pointer select-none py-2 pl-4 pr-4 text-gray-900 hover:bg-gray-100" | ||
> | ||
{option} | ||
</HeadlessComboBox.Option> | ||
))} | ||
</HeadlessComboBox.Options> | ||
</Transition> | ||
)} | ||
</div> | ||
</HeadlessComboBox> | ||
); | ||
}; | ||
|
||
export default Combobox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { | ||
chainStorageWatcherAtom, | ||
isNodeSelectorOpenAtom, | ||
networkConfigAtom, | ||
rpcNodeAtom, | ||
savedApiNodeAtom, | ||
savedRpcNodeAtom, | ||
} from 'store/app'; | ||
import { useAtom, useAtomValue, useSetAtom } from 'jotai'; | ||
import ActionsDialog from './ActionsDialog'; | ||
import Combobox from 'components/Combobox'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
Check warning on line 12 in src/components/NodeSelectorDialog.tsx GitHub Actions / build (18.x)
Check warning on line 12 in src/components/NodeSelectorDialog.tsx GitHub Actions / build (18.x)
|
||
import { fetchAllAddrs } from 'utils/rpc'; | ||
|
||
const useRpcAddrs = () => { | ||
const [rpcAddrs, setRpcAddrs] = useState([]); | ||
const [apiAddrs, setApiAddrs] = useState([]); | ||
const networkConfig = useAtomValue(networkConfigAtom); | ||
|
||
useEffect(() => { | ||
const fetchAddrs = async () => { | ||
const { rpcAddrs, apiAddrs } = await fetchAllAddrs(networkConfig.url); | ||
setRpcAddrs(rpcAddrs); | ||
setApiAddrs(apiAddrs); | ||
}; | ||
|
||
try { | ||
fetchAddrs(); | ||
} catch (e) { | ||
console.error('Error loading RPC Addrs', e); | ||
} | ||
}, [networkConfig]); | ||
|
||
return { rpcAddrs, apiAddrs }; | ||
}; | ||
const NodeSelectorDialog = () => { | ||
const [isOpen, setIsOpen] = useAtom(isNodeSelectorOpenAtom); | ||
const { rpcAddrs, apiAddrs } = useRpcAddrs(); | ||
const watcher = useAtomValue(chainStorageWatcherAtom); | ||
const rpcAddr = useAtomValue(rpcNodeAtom); | ||
const [api, setApi] = useState(watcher?.apiAddr); | ||
const [rpc, setRpc] = useState(rpcAddr ?? undefined); | ||
const [initialApi, setInitialApi] = useState(api); | ||
const [initialRpc, setInitialRpc] = useState(rpc); | ||
const setSavedRpc = useSetAtom(savedRpcNodeAtom); | ||
const setSavedApi = useSetAtom(savedApiNodeAtom); | ||
|
||
const save = () => { | ||
assert(api && rpc); | ||
setSavedApi(api); | ||
setSavedRpc(rpc); | ||
setIsOpen(false); | ||
window.location.reload(); | ||
}; | ||
|
||
useEffect(() => { | ||
if (isOpen) { | ||
const currentRpc = rpcAddr ?? undefined; | ||
const currentApi = watcher?.apiAddr; | ||
setRpc(rpcAddr ?? undefined); | ||
setApi(watcher?.apiAddr); | ||
setInitialRpc(currentRpc); | ||
setInitialApi(currentApi); | ||
} | ||
}, [isOpen, rpcAddr, watcher?.apiAddr]); | ||
|
||
const body = ( | ||
<div className="mt-2 p-1 max-h-96"> | ||
<p>RPC Endpoint:</p> | ||
<Combobox | ||
onValueChange={(value: string) => { | ||
setRpc(value); | ||
}} | ||
value={rpc} | ||
options={rpcAddrs} | ||
/> | ||
<p className="mt-4">API Endpoint:</p> | ||
<Combobox | ||
onValueChange={(value: string) => { | ||
setApi(value); | ||
}} | ||
value={api} | ||
options={apiAddrs} | ||
/> | ||
</div> | ||
); | ||
|
||
return ( | ||
<ActionsDialog | ||
title="Connection Settings" | ||
body={body} | ||
isOpen={isOpen} | ||
primaryAction={{ | ||
action: save, | ||
label: 'Save', | ||
}} | ||
secondaryAction={{ | ||
action: () => { | ||
setIsOpen(false); | ||
}, | ||
label: 'Cancel', | ||
}} | ||
onClose={() => { | ||
setIsOpen(false); | ||
}} | ||
initialFocusPrimary | ||
overflow | ||
primaryActionDisabled={ | ||
!(api && rpc) || (initialApi === api && initialRpc === rpc) | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
export default NodeSelectorDialog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters