2021-10-01 03:05:42 -07:00
|
|
|
import React, { useState } from 'react'
|
|
|
|
import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/solid'
|
|
|
|
import useMangoGroupConfig from '../hooks/useMangoGroupConfig'
|
2021-09-13 14:14:59 -07:00
|
|
|
import Modal from './Modal'
|
|
|
|
import { ElementTitle } from './styles'
|
2022-02-21 16:16:20 -08:00
|
|
|
import Button, { LinkButton } from './Button'
|
2021-09-13 14:14:59 -07:00
|
|
|
import Input from './Input'
|
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
2021-09-13 16:27:02 -07:00
|
|
|
import useLocalStorageState from '../hooks/useLocalStorageState'
|
|
|
|
import Select from './Select'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2021-11-21 00:57:13 -08:00
|
|
|
import Switch from './Switch'
|
2021-09-13 16:27:02 -07:00
|
|
|
|
|
|
|
const NODE_URLS = [
|
2021-11-15 06:25:03 -08:00
|
|
|
{ label: 'Triton (RPC Pool)', value: 'https://mango.rpcpool.com' },
|
2021-09-13 16:27:02 -07:00
|
|
|
{
|
|
|
|
label: 'Genesys Go',
|
2021-11-15 04:41:40 -08:00
|
|
|
value: 'https://mango.genesysgo.net/',
|
2021-09-13 16:27:02 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Project Serum',
|
|
|
|
value: 'https://solana-api.projectserum.com/',
|
|
|
|
},
|
|
|
|
{ label: 'Custom', value: '' },
|
|
|
|
]
|
|
|
|
|
|
|
|
const CUSTOM_NODE = NODE_URLS.find((n) => n.label === 'Custom')
|
|
|
|
|
2021-11-15 06:25:03 -08:00
|
|
|
export const NODE_URL_KEY = 'node-url-key-0.5'
|
2022-02-01 16:58:47 -08:00
|
|
|
export const DEFAULT_MARKET_KEY = 'defaultMarket-0.3'
|
2021-11-21 00:57:13 -08:00
|
|
|
export const ORDERBOOK_FLASH_KEY = 'showOrderbookFlash'
|
2022-01-14 11:44:10 -08:00
|
|
|
export const DEFAULT_SPOT_MARGIN_KEY = 'defaultSpotMargin'
|
2021-10-01 03:05:42 -07:00
|
|
|
export const initialMarket = {
|
2022-02-01 16:58:47 -08:00
|
|
|
base: 'SOL',
|
2021-10-01 03:05:42 -07:00
|
|
|
kind: 'perp',
|
2022-02-01 16:58:47 -08:00
|
|
|
name: 'SOL-PERP',
|
|
|
|
path: '/?name=SOL-PERP',
|
2021-10-01 03:05:42 -07:00
|
|
|
}
|
2021-09-13 14:14:59 -07:00
|
|
|
|
|
|
|
const SettingsModal = ({ isOpen, onClose }) => {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-10-01 03:05:42 -07:00
|
|
|
const [settingsView, setSettingsView] = useState('')
|
|
|
|
const [rpcEndpointUrl] = useLocalStorageState(
|
|
|
|
NODE_URL_KEY,
|
|
|
|
NODE_URLS[0].value
|
|
|
|
)
|
2022-01-14 11:44:10 -08:00
|
|
|
|
2021-10-01 03:05:42 -07:00
|
|
|
const [defaultMarket] = useLocalStorageState(
|
|
|
|
DEFAULT_MARKET_KEY,
|
|
|
|
initialMarket
|
|
|
|
)
|
2021-11-21 00:57:13 -08:00
|
|
|
const [showOrderbookFlash, setShowOrderbookFlash] = useLocalStorageState(
|
|
|
|
ORDERBOOK_FLASH_KEY,
|
|
|
|
true
|
|
|
|
)
|
2022-01-14 11:44:10 -08:00
|
|
|
|
|
|
|
const [defaultSpotMargin, setDefaultSpotMargin] = useLocalStorageState(
|
|
|
|
DEFAULT_SPOT_MARGIN_KEY,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
|
2021-10-01 03:05:42 -07:00
|
|
|
const rpcEndpoint =
|
|
|
|
NODE_URLS.find((node) => node.value === rpcEndpointUrl) || CUSTOM_NODE
|
|
|
|
return (
|
|
|
|
<Modal isOpen={isOpen} onClose={onClose}>
|
|
|
|
{settingsView !== '' ? (
|
2022-02-21 16:16:20 -08:00
|
|
|
<LinkButton
|
|
|
|
className="absolute flex items-center left-2 top-3"
|
2021-10-01 03:05:42 -07:00
|
|
|
onClick={() => setSettingsView('')}
|
|
|
|
>
|
2022-02-21 16:16:20 -08:00
|
|
|
<ChevronLeftIcon className="h-5 w-5" />
|
2021-10-20 05:42:40 -07:00
|
|
|
<span>{t('back')}</span>
|
2022-02-21 16:16:20 -08:00
|
|
|
</LinkButton>
|
2021-10-01 03:05:42 -07:00
|
|
|
) : null}
|
|
|
|
<Modal.Header>
|
2022-01-31 07:52:28 -08:00
|
|
|
<ElementTitle noMarginBottom>{t('settings')}</ElementTitle>
|
2021-10-01 03:05:42 -07:00
|
|
|
</Modal.Header>
|
|
|
|
{!settingsView ? (
|
|
|
|
<div className="border-b border-th-bkg-4">
|
|
|
|
<button
|
|
|
|
className="border-t border-th-bkg-4 default-transition flex font-normal items-center justify-between py-3 text-th-fgd-1 w-full hover:text-th-primary focus:outline-none"
|
|
|
|
onClick={() => setSettingsView('Default Market')}
|
|
|
|
>
|
2021-10-20 05:42:40 -07:00
|
|
|
<span>{t('default-market')}</span>
|
2021-10-01 03:05:42 -07:00
|
|
|
<div className="flex items-center text-th-fgd-3 text-xs">
|
|
|
|
{defaultMarket.name}
|
2022-02-21 16:16:20 -08:00
|
|
|
<ChevronRightIcon className="h-5 ml-1 w-5 text-th-fgd-1" />
|
2021-10-01 03:05:42 -07:00
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className="border-t border-th-bkg-4 default-transition flex font-normal items-center justify-between py-3 text-th-fgd-1 w-full hover:text-th-primary focus:outline-none"
|
|
|
|
onClick={() => setSettingsView('RPC Endpoint')}
|
|
|
|
>
|
2021-10-20 05:42:40 -07:00
|
|
|
<span>{t('rpc-endpoint')}</span>
|
2021-10-01 03:05:42 -07:00
|
|
|
<div className="flex items-center text-th-fgd-3 text-xs">
|
|
|
|
{rpcEndpoint.label}
|
2022-02-21 16:16:20 -08:00
|
|
|
<ChevronRightIcon className="h-5 ml-1 w-5 text-th-fgd-1" />
|
2021-10-01 03:05:42 -07:00
|
|
|
</div>
|
|
|
|
</button>
|
2021-11-21 00:57:13 -08:00
|
|
|
<div className="border-t border-th-bkg-4 flex items-center justify-between py-3 text-th-fgd-1">
|
|
|
|
<span>{t('orderbook-animation')}</span>
|
|
|
|
<Switch
|
|
|
|
checked={showOrderbookFlash}
|
|
|
|
onChange={(checked) => setShowOrderbookFlash(checked)}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-01-14 11:44:10 -08:00
|
|
|
|
|
|
|
<div className="border-t border-th-bkg-4 flex items-center justify-between py-3 text-th-fgd-1">
|
|
|
|
<span>{t('default-spot-margin')}</span>
|
|
|
|
<Switch
|
|
|
|
checked={defaultSpotMargin}
|
|
|
|
onChange={(checked) => setDefaultSpotMargin(checked)}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-10-01 03:05:42 -07:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
<SettingsContent
|
|
|
|
settingsView={settingsView}
|
|
|
|
setSettingsView={setSettingsView}
|
|
|
|
/>
|
|
|
|
{!settingsView ? (
|
|
|
|
<div className="flex justify-center pt-6">
|
2021-10-20 05:42:40 -07:00
|
|
|
<Button onClick={onClose}>{t('done')}</Button>
|
2021-10-01 03:05:42 -07:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default React.memo(SettingsModal)
|
|
|
|
|
|
|
|
const SettingsContent = ({ settingsView, setSettingsView }) => {
|
|
|
|
switch (settingsView) {
|
|
|
|
case 'Default Market':
|
|
|
|
return <DefaultMarketSettings setSettingsView={setSettingsView} />
|
|
|
|
case 'RPC Endpoint':
|
|
|
|
return <RpcEndpointSettings setSettingsView={setSettingsView} />
|
|
|
|
case '':
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultMarketSettings = ({ setSettingsView }) => {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-10-01 03:05:42 -07:00
|
|
|
const groupConfig = useMangoGroupConfig()
|
|
|
|
const allMarkets = [
|
|
|
|
...groupConfig.spotMarkets,
|
|
|
|
...groupConfig.perpMarkets,
|
|
|
|
].sort((a, b) => a.name.localeCompare(b.name))
|
|
|
|
const [defaultMarket, setDefaultMarket] = useLocalStorageState(
|
|
|
|
DEFAULT_MARKET_KEY,
|
|
|
|
{
|
|
|
|
base: 'BTC',
|
|
|
|
kind: 'perp',
|
|
|
|
name: 'BTC-PERP',
|
2022-01-31 09:48:48 -08:00
|
|
|
path: '/?name=BTC-PERP',
|
2021-10-01 03:05:42 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
const handleSetDefaultMarket = (market) => {
|
|
|
|
const base = market.slice(0, -5)
|
|
|
|
const kind = market.includes('PERP') ? 'perp' : 'spot'
|
2021-11-14 10:37:02 -08:00
|
|
|
|
2021-10-01 03:05:42 -07:00
|
|
|
setDefaultMarket({
|
|
|
|
base: base,
|
|
|
|
kind: kind,
|
|
|
|
name: market,
|
2022-01-31 09:48:48 -08:00
|
|
|
path: `/?name=${market}`,
|
2021-10-01 03:05:42 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
const parsedDefaultMarket = defaultMarket
|
|
|
|
return (
|
|
|
|
<div>
|
2022-02-21 16:16:20 -08:00
|
|
|
<label className="block mb-1 text-th-fgd-1 text-xs">
|
2021-10-20 05:42:40 -07:00
|
|
|
{t('default-market')}
|
2021-10-01 03:05:42 -07:00
|
|
|
</label>
|
|
|
|
<Select
|
|
|
|
value={parsedDefaultMarket.name}
|
|
|
|
onChange={(market) => handleSetDefaultMarket(market)}
|
|
|
|
className="w-full"
|
|
|
|
>
|
|
|
|
{allMarkets.map((market) => (
|
2022-02-21 16:16:20 -08:00
|
|
|
<Select.Option key={market.name} value={market.name}>
|
2021-10-01 03:05:42 -07:00
|
|
|
<div className="flex items-center justify-between w-full">
|
|
|
|
{market.name}
|
|
|
|
</div>
|
|
|
|
</Select.Option>
|
|
|
|
))}
|
|
|
|
</Select>
|
2022-02-21 16:16:20 -08:00
|
|
|
<Button onClick={() => setSettingsView('')} className="mt-6 w-full">
|
2021-10-20 05:42:40 -07:00
|
|
|
<div className={`flex items-center justify-center`}>{t('save')}</div>
|
2021-10-01 03:05:42 -07:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const RpcEndpointSettings = ({ setSettingsView }) => {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-09-13 14:14:59 -07:00
|
|
|
const actions = useMangoStore((s) => s.actions)
|
2021-09-13 16:27:02 -07:00
|
|
|
const [rpcEndpointUrl, setRpcEndpointUrl] = useLocalStorageState(
|
|
|
|
NODE_URL_KEY,
|
|
|
|
NODE_URLS[0].value
|
2021-09-13 14:14:59 -07:00
|
|
|
)
|
2021-09-13 16:27:02 -07:00
|
|
|
const rpcEndpoint =
|
|
|
|
NODE_URLS.find((node) => node.value === rpcEndpointUrl) || CUSTOM_NODE
|
|
|
|
|
2021-09-13 14:14:59 -07:00
|
|
|
const handleSetEndpointUrl = (endpointUrl) => {
|
|
|
|
setRpcEndpointUrl(endpointUrl)
|
|
|
|
actions.updateConnection(endpointUrl)
|
2021-10-01 03:05:42 -07:00
|
|
|
setSettingsView('')
|
2021-09-13 14:14:59 -07:00
|
|
|
}
|
|
|
|
|
2021-09-13 16:27:02 -07:00
|
|
|
const handleSelectEndpointUrl = (url) => {
|
|
|
|
setRpcEndpointUrl(url)
|
|
|
|
}
|
2021-09-13 14:14:59 -07:00
|
|
|
return (
|
2021-10-01 03:05:42 -07:00
|
|
|
<div className="flex flex-col text-th-fgd-1">
|
2022-02-21 16:16:20 -08:00
|
|
|
<label className="block mb-1 text-xs">{t('rpc-endpoint')}</label>
|
2021-10-01 03:05:42 -07:00
|
|
|
<Select
|
|
|
|
value={rpcEndpoint.label}
|
|
|
|
onChange={(url) => handleSelectEndpointUrl(url)}
|
|
|
|
className="w-full"
|
|
|
|
>
|
2022-02-21 16:16:20 -08:00
|
|
|
{NODE_URLS.map((node) => (
|
|
|
|
<Select.Option key={node.value} value={node.value}>
|
|
|
|
<span>{node.label}</span>
|
|
|
|
</Select.Option>
|
|
|
|
))}
|
2021-10-01 03:05:42 -07:00
|
|
|
</Select>
|
|
|
|
{rpcEndpoint.label === 'Custom' ? (
|
|
|
|
<div className="pt-4">
|
|
|
|
<label className="block font-semibold mb-1 text-xs">
|
2021-10-20 05:42:40 -07:00
|
|
|
{t('node-url')}
|
2021-10-01 03:05:42 -07:00
|
|
|
</label>
|
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
value={rpcEndpointUrl}
|
|
|
|
onChange={(e) => setRpcEndpointUrl(e.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
<Button
|
|
|
|
onClick={() => handleSetEndpointUrl(rpcEndpointUrl)}
|
2022-02-21 16:16:20 -08:00
|
|
|
className="mt-6 w-full"
|
2021-10-01 03:05:42 -07:00
|
|
|
>
|
2021-10-20 05:42:40 -07:00
|
|
|
<div className={`flex items-center justify-center`}>{t('save')}</div>
|
2021-10-01 03:05:42 -07:00
|
|
|
</Button>
|
|
|
|
</div>
|
2021-09-13 14:14:59 -07:00
|
|
|
)
|
|
|
|
}
|