mango-v4-ui/components/settings/RpcSettings.tsx

173 lines
5.3 KiB
TypeScript
Raw Normal View History

2022-12-15 04:28:17 -08:00
import ButtonGroup from '@components/forms/ButtonGroup'
import Input from '@components/forms/Input'
import Button from '@components/shared/Button'
2023-01-19 12:31:45 -08:00
import { useWallet } from '@solana/wallet-adapter-react'
2022-12-15 18:19:11 -08:00
import mangoStore from '@store/mangoStore'
2022-12-15 04:28:17 -08:00
import useLocalStorageState from 'hooks/useLocalStorageState'
import { useTranslation } from 'next-i18next'
2023-01-19 12:31:45 -08:00
import { ChangeEvent, useCallback, useEffect, useMemo, useState } from 'react'
import { PRIORITY_FEE_KEY, RPC_PROVIDER_KEY } from 'utils/constants'
2022-12-15 04:28:17 -08:00
const RPC_URLS = [
2022-12-15 18:19:11 -08:00
{
label: 'Triton',
value: 'https://mango.rpcpool.com/0f9acc0d45173b51bf7d7e09c1e5',
},
2022-12-27 13:35:26 -08:00
// {
// label: 'Genesys Go',
// value: 'https://mango.genesysgo.net',
// },
2022-12-15 04:28:17 -08:00
{ label: 'Custom', value: '' },
]
2023-01-19 12:31:45 -08:00
export const PRIORITY_FEES = [
{ label: 'None', value: 0 },
{ label: 'Low', value: 50000 },
{ label: 'High', value: 100000 },
]
2023-02-03 13:23:36 -08:00
export const DEFAULT_PRIORITY_FEE = PRIORITY_FEES[1]
2022-12-15 04:28:17 -08:00
const RpcSettings = () => {
const { t } = useTranslation('settings')
2023-01-03 14:20:00 -08:00
const actions = mangoStore.getState().actions
2023-01-19 12:31:45 -08:00
const { wallet } = useWallet()
2022-12-15 04:28:17 -08:00
const [customUrl, setCustomUrl] = useState('')
const [showCustomForm, setShowCustomForm] = useState(false)
const [rpcEndpointProvider, setRpcEndpointProvider] = useLocalStorageState(
RPC_PROVIDER_KEY,
2022-12-27 13:13:15 -08:00
RPC_URLS[0].value
2022-12-15 04:28:17 -08:00
)
2023-01-19 12:31:45 -08:00
const [storedPriorityFee, setStoredPriorityFee] = useLocalStorageState(
PRIORITY_FEE_KEY,
2023-02-03 13:23:36 -08:00
DEFAULT_PRIORITY_FEE.value
2023-01-19 12:31:45 -08:00
)
2022-12-27 13:34:05 -08:00
const rpcEndpoint = useMemo(() => {
return (
RPC_URLS.find((node) => node.value === rpcEndpointProvider) || {
label: 'Custom',
value: rpcEndpointProvider,
}
)
}, [rpcEndpointProvider])
2022-12-15 04:28:17 -08:00
2023-01-19 12:31:45 -08:00
const priorityFee = useMemo(() => {
return (
PRIORITY_FEES.find((node) => node.value === storedPriorityFee) ||
2023-02-03 13:23:36 -08:00
DEFAULT_PRIORITY_FEE
2023-01-19 12:31:45 -08:00
)
}, [storedPriorityFee])
2022-12-15 04:28:17 -08:00
const handleSetEndpointProvider = (provider: string) => {
2022-12-27 13:34:05 -08:00
const endpointProvider = RPC_URLS.find(
(node) => node.label === provider
) || { label: 'Custom', value: rpcEndpointProvider }
2022-12-27 13:13:15 -08:00
setRpcEndpointProvider(endpointProvider.value)
2022-12-15 04:28:17 -08:00
if (provider !== 'Custom') {
setShowCustomForm(false)
2022-12-27 13:13:15 -08:00
actions.updateConnection(endpointProvider.value)
2022-12-15 04:28:17 -08:00
}
}
2023-01-19 12:31:45 -08:00
const handlePriorityFee = useCallback(
(label: string) => {
const fee = PRIORITY_FEES.find((fee) => fee.label === label)
setStoredPriorityFee(fee?.value)
if (wallet) {
actions.connectMangoClientWithWallet(wallet)
}
},
[setStoredPriorityFee, actions, wallet]
)
2022-12-15 04:28:17 -08:00
useEffect(() => {
2022-12-27 13:34:05 -08:00
if (rpcEndpoint.label === 'Custom') {
2022-12-15 04:28:17 -08:00
setShowCustomForm(true)
2022-12-27 13:34:05 -08:00
setCustomUrl(rpcEndpoint.value)
2022-12-15 04:28:17 -08:00
}
2022-12-27 13:34:05 -08:00
}, [rpcEndpoint])
2022-12-15 04:28:17 -08:00
const handleSaveCustomEndpoint = () => {
if (!customUrl) return
2022-12-27 13:34:05 -08:00
setRpcEndpointProvider(customUrl)
2022-12-15 18:19:11 -08:00
actions.updateConnection(customUrl)
2022-12-15 04:28:17 -08:00
}
return (
<>
<h2 className="mb-4 text-base">{t('rpc')}</h2>
<div className="flex flex-col border-t border-th-bkg-3 py-4 md:flex-row md:items-center md:justify-between md:px-4">
<p className="mb-2 md:mb-0">{t('rpc-provider')}</p>
<div className="w-full min-w-[160px] md:w-auto md:pl-4">
2022-12-15 04:28:17 -08:00
<ButtonGroup
2022-12-27 13:13:15 -08:00
activeValue={rpcEndpoint.label}
2022-12-15 04:28:17 -08:00
onChange={(v) => handleSetEndpointProvider(v)}
values={RPC_URLS.map((val) => val.label)}
/>
{showCustomForm ? (
<div className="mt-2">
<div className="flex space-x-2">
<Input
type="text"
name="url"
id="url"
2022-12-27 13:13:15 -08:00
className="!h-10"
2022-12-15 04:28:17 -08:00
placeholder={t('rpc-url')}
value={customUrl}
onChange={(e: ChangeEvent<HTMLInputElement>) =>
setCustomUrl(e.target.value)
}
/>
<Button
className="h-12"
disabled={!customUrl}
onClick={handleSaveCustomEndpoint}
>
{t('save')}
</Button>
</div>
</div>
) : null}
</div>
</div>
2023-01-19 12:31:45 -08:00
<div className="flex flex-col border-t border-th-bkg-3 py-4 md:flex-row md:items-center md:justify-between md:px-4">
<p className="mb-2 md:mb-0">Priority Fee</p>
<div className="w-full min-w-[160px] md:w-auto md:pl-4">
<ButtonGroup
activeValue={priorityFee.label}
onChange={(v) => handlePriorityFee(v)}
values={PRIORITY_FEES.map((val) => val.label)}
/>
{/* {showCustomForm ? (
<div className="mt-2">
<div className="flex space-x-2">
<Input
type="text"
name="url"
id="url"
className="!h-10"
placeholder={t('rpc-url')}
value={customUrl}
onChange={(e: ChangeEvent<HTMLInputElement>) =>
setCustomUrl(e.target.value)
}
/>
<Button
className="h-12"
disabled={!customUrl}
onClick={handleSaveCustomEndpoint}
>
{t('save')}
</Button>
</div>
</div>
) : null} */}
</div>
</div>
2022-12-15 04:28:17 -08:00
</>
)
}
export default RpcSettings