import React, { useMemo, useState } from 'react' import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/solid' import useMangoGroupConfig from '../hooks/useMangoGroupConfig' import Modal from './Modal' import { ElementTitle } from './styles' import Button, { LinkButton } from './Button' import Input, { Label } from './Input' import useMangoStore from '../stores/useMangoStore' import useLocalStorageState from '../hooks/useLocalStorageState' import Select from './Select' import { useTranslation } from 'next-i18next' import Switch from './Switch' import { MarketKind } from '@blockworks-foundation/mango-client' import { useTheme } from 'next-themes' import { useRouter } from 'next/router' import ButtonGroup from './ButtonGroup' import dayjs from 'dayjs' require('dayjs/locale/en') require('dayjs/locale/es') require('dayjs/locale/zh') require('dayjs/locale/zh-tw') const NODE_URLS = [ { label: 'Triton (RPC Pool)', value: 'https://mango.rpcpool.com' }, { label: 'Genesys Go', value: 'https://mango.genesysgo.net/', }, { label: 'Project Serum', value: 'https://solana-api.projectserum.com/', }, { label: 'Custom', value: '' }, ] const THEMES = ['Light', 'Dark', 'Mango'] export const LANGS = [ { locale: 'en', name: 'english', description: 'english' }, { locale: 'es', name: 'spanish', description: 'spanish' }, { locale: 'zh_tw', name: 'chinese-traditional', description: 'traditional chinese', }, { locale: 'zh', name: 'chinese', description: 'simplified chinese' }, ] const CUSTOM_NODE = NODE_URLS.find((n) => n.label === 'Custom') export const NODE_URL_KEY = 'node-url-key-0.6' export const DEFAULT_MARKET_KEY = 'defaultMarket-0.3' export const ORDERBOOK_FLASH_KEY = 'showOrderbookFlash' export const DEFAULT_SPOT_MARGIN_KEY = 'defaultSpotMargin' export const initialMarket = { base: 'SOL', kind: 'perp' as MarketKind, name: 'SOL-PERP', path: '/?name=SOL-PERP', } const SettingsModal = ({ isOpen, onClose }) => { const { t } = useTranslation('common') const [settingsView, setSettingsView] = useState('') const { theme } = useTheme() const [savedLanguage] = useLocalStorageState('language', '') const [rpcEndpointUrl] = useLocalStorageState( NODE_URL_KEY, NODE_URLS[0].value ) const [defaultMarket] = useLocalStorageState( DEFAULT_MARKET_KEY, initialMarket ) const [showOrderbookFlash, setShowOrderbookFlash] = useLocalStorageState( ORDERBOOK_FLASH_KEY, true ) const [defaultSpotMargin, setDefaultSpotMargin] = useLocalStorageState( DEFAULT_SPOT_MARGIN_KEY, false ) const rpcEndpoint = NODE_URLS.find((node) => node.value === rpcEndpointUrl) || CUSTOM_NODE const savedLanguageName = useMemo( () => LANGS.find((l) => l.locale === savedLanguage).name, [savedLanguage] ) return ( {settingsView !== '' ? ( setSettingsView('')} > {t('back')} ) : null} {t('settings')} {!settingsView ? (
{t('orderbook-animation')} setShowOrderbookFlash(checked)} />
{t('default-spot-margin')} setDefaultSpotMargin(checked)} />
) : null} {!settingsView ? (
) : null}
) } export default React.memo(SettingsModal) const SettingsContent = ({ settingsView, setSettingsView }) => { switch (settingsView) { case 'Default Market': return case 'RPC Endpoint': return case 'Theme': return case 'Language': return case '': return null } } const DefaultMarketSettings = ({ setSettingsView }) => { const { t } = useTranslation('common') 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', path: '/?name=BTC-PERP', } ) const handleSetDefaultMarket = (market) => { const base = market.slice(0, -5) const kind = market.includes('PERP') ? 'perp' : 'spot' setDefaultMarket({ base: base, kind: kind, name: market, path: `/?name=${market}`, }) } const parsedDefaultMarket = defaultMarket return (
) } const RpcEndpointSettings = ({ setSettingsView }) => { const { t } = useTranslation('common') const actions = useMangoStore((s) => s.actions) const [rpcEndpointUrl, setRpcEndpointUrl] = useLocalStorageState( NODE_URL_KEY, NODE_URLS[0].value ) const rpcEndpoint = NODE_URLS.find((node) => node.value === rpcEndpointUrl) || CUSTOM_NODE const handleSetEndpointUrl = (endpointUrl) => { setRpcEndpointUrl(endpointUrl) actions.updateConnection(endpointUrl) setSettingsView('') } const handleSelectEndpointUrl = (url) => { setRpcEndpointUrl(url) } return (
{rpcEndpoint.label === 'Custom' ? (
setRpcEndpointUrl(e.target.value)} />
) : null}
) } const ThemeSettings = ({ setSettingsView }) => { const { theme, setTheme } = useTheme() const { t } = useTranslation('common') return ( <> setTheme(t)} values={THEMES} /> ) } const LanguageSettings = () => { const [savedLanguage, setSavedLanguage] = useLocalStorageState('language', '') const router = useRouter() const { pathname, asPath, query } = router const { t } = useTranslation('common') const handleLangChange = () => { router.push({ pathname, query }, asPath, { locale: savedLanguage }) dayjs.locale(savedLanguage == 'zh_tw' ? 'zh-tw' : savedLanguage) } return ( <> setSavedLanguage(l)} values={LANGS.map((val) => val.locale)} names={LANGS.map((val) => t(val.name))} /> ) }