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

161 lines
5.3 KiB
TypeScript
Raw Normal View History

import KeyboardIcon from '@components/icons/KeyboardIcon'
import HotKeyModal from '@components/modals/HotKeyModal'
2023-06-24 05:44:15 -07:00
import Button, { IconButton } from '@components/shared/Button'
2023-06-22 20:37:34 -07:00
import InlineNotification from '@components/shared/InlineNotification'
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
import { TrashIcon } from '@heroicons/react/20/solid'
2023-06-20 16:16:58 -07:00
import useLocalStorageState from 'hooks/useLocalStorageState'
import { useTranslation } from 'next-i18next'
2023-06-22 20:37:34 -07:00
import { useState } from 'react'
2023-06-20 16:16:58 -07:00
import { HOT_KEYS_KEY } from 'utils/constants'
2023-06-21 17:48:15 -07:00
export type HotKey = {
ioc: boolean
keySequence: string
margin: boolean
orderSide: 'buy' | 'sell'
orderSizeType: 'percentage' | 'notional'
orderSize: string
orderType: 'limit' | 'market'
orderPrice: string
postOnly: boolean
reduceOnly: boolean
}
2023-06-20 16:16:58 -07:00
const HotKeysSettings = () => {
2023-06-24 05:44:15 -07:00
const { t } = useTranslation(['common', 'settings', 'trade'])
2023-06-22 20:37:34 -07:00
const [hotKeys, setHotKeys] = useLocalStorageState(HOT_KEYS_KEY, [])
2023-06-20 16:16:58 -07:00
const [showHotKeyModal, setShowHotKeyModal] = useState(false)
2023-06-22 20:37:34 -07:00
const handleDeleteKey = (key: string) => {
const newKeys = hotKeys.filter((hk: HotKey) => hk.keySequence !== key)
setHotKeys([...newKeys])
}
2023-06-20 16:16:58 -07:00
return (
<>
2023-06-24 05:44:15 -07:00
<div className="mb-4 flex items-center justify-between">
2023-06-24 06:06:19 -07:00
<div className="pr-6">
2023-06-24 05:44:15 -07:00
<p>{t('settings:hot-keys-desc')}</p>
</div>
2023-06-22 20:37:34 -07:00
{hotKeys.length ? (
2023-06-24 06:06:19 -07:00
<Button
className="whitespace-nowrap"
2023-06-25 18:11:31 -07:00
disabled={hotKeys.length >= 20}
2023-06-24 06:06:19 -07:00
onClick={() => setShowHotKeyModal(true)}
secondary
>
2023-06-24 05:44:15 -07:00
{t('settings:new-hot-key')}
</Button>
2023-06-22 20:37:34 -07:00
) : null}
2023-06-21 17:48:15 -07:00
</div>
2023-06-25 18:11:31 -07:00
{hotKeys.length === 20 ? (
<div className="mb-4">
<InlineNotification
type="warning"
desc={t('settings:error-key-limit-reached')}
/>
</div>
) : null}
2023-06-20 16:16:58 -07:00
{hotKeys.length ? (
2023-06-22 20:37:34 -07:00
<Table>
<thead>
<TrHead>
2023-06-24 05:44:15 -07:00
<Th className="text-left">{t('settings:key-sequence')}</Th>
<Th className="text-right">{t('trade:order-type')}</Th>
<Th className="text-right">{t('trade:side')}</Th>
<Th className="text-right">{t('trade:size')}</Th>
2023-06-22 20:37:34 -07:00
<Th className="text-right">{t('price')}</Th>
2023-06-24 05:44:15 -07:00
<Th className="text-right">{t('settings:options')}</Th>
2023-06-22 20:37:34 -07:00
<Th />
</TrHead>
</thead>
<tbody>
{hotKeys.map((hk: HotKey) => {
const {
keySequence,
orderSide,
orderPrice,
orderSize,
orderSizeType,
orderType,
ioc,
margin,
reduceOnly,
postOnly,
} = hk
const size =
orderSizeType === 'percentage'
2023-06-24 05:44:15 -07:00
? t('settings:percentage-of-max', { size: orderSize })
2023-06-22 20:37:34 -07:00
: `$${orderSize}`
2023-06-24 05:44:15 -07:00
const price = orderPrice
? `${orderPrice}% ${
orderSide === 'buy'
? t('settings:below')
: t('settings:above')
} oracle`
: t('trade:market')
2023-06-22 20:37:34 -07:00
const options = {
margin: margin,
IOC: ioc,
post: postOnly,
reduce: reduceOnly,
}
return (
<TrBody key={keySequence} className="text-right text-th-fgd-2">
2023-06-22 20:37:34 -07:00
<Td className="text-left">{keySequence}</Td>
2023-06-24 05:44:15 -07:00
<Td className="text-right">{t(`trade:${orderType}`)}</Td>
<Td className="text-right">{t(orderSide)}</Td>
2023-06-22 20:37:34 -07:00
<Td className="text-right">{size}</Td>
<Td className="text-right">{price}</Td>
<Td className="text-right">
{Object.entries(options).map((e) => {
return e[1]
2023-06-24 05:44:15 -07:00
? `${e[0] !== 'margin' ? ', ' : ''}${t(
2023-07-21 11:47:53 -07:00
`trade:${e[0]}`,
2023-06-24 05:44:15 -07:00
)}`
2023-06-22 20:37:34 -07:00
: ''
})}
</Td>
<Td>
<div className="flex justify-end">
<IconButton
onClick={() => handleDeleteKey(keySequence)}
size="small"
>
<TrashIcon className="h-4 w-4" />
</IconButton>
</div>
</Td>
</TrBody>
)
})}
</tbody>
</Table>
2023-06-20 16:16:58 -07:00
) : (
2023-06-22 20:37:34 -07:00
<div className="rounded-lg border border-th-bkg-3 p-6">
2023-06-20 16:16:58 -07:00
<div className="flex flex-col items-center">
<KeyboardIcon className="mb-2 h-8 w-8 text-th-fgd-4" />
2023-06-24 05:44:15 -07:00
<p className="mb-4">{t('settings:no-hot-keys')}</p>
2023-06-20 16:16:58 -07:00
<Button onClick={() => setShowHotKeyModal(true)}>
2023-06-24 05:44:15 -07:00
<div className="flex items-center">
{t('settings:new-hot-key')}
</div>
2023-06-20 16:16:58 -07:00
</Button>
</div>
</div>
)}
{showHotKeyModal ? (
<HotKeyModal
isOpen={showHotKeyModal}
onClose={() => setShowHotKeyModal(false)}
/>
) : null}
</>
)
}
export default HotKeysSettings