import ButtonGroup from '@components/forms/ButtonGroup' import Checkbox from '@components/forms/Checkbox' import Input from '@components/forms/Input' import Label from '@components/forms/Label' import Button, { IconButton } from '@components/shared/Button' import InlineNotification from '@components/shared/InlineNotification' import Modal from '@components/shared/Modal' import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements' import Tooltip from '@components/shared/Tooltip' import { KeyIcon, TrashIcon } from '@heroicons/react/20/solid' import useLocalStorageState from 'hooks/useLocalStorageState' import { useTranslation } from 'next-i18next' import { useState } from 'react' import { ModalProps } from 'types/modal' import { HOT_KEYS_KEY } from 'utils/constants' 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 } const HotKeysSettings = () => { const { t } = useTranslation(['common', 'settings', 'trade']) const [hotKeys, setHotKeys] = useLocalStorageState(HOT_KEYS_KEY, []) const [showHotKeyModal, setShowHotKeyModal] = useState(false) const handleDeleteKey = (key: string) => { const newKeys = hotKeys.filter((hk: HotKey) => hk.keySequence !== key) setHotKeys([...newKeys]) } return ( <>

{t('settings:hot-keys')}

{t('settings:hot-keys-desc')}

{hotKeys.length ? ( ) : null}
{hotKeys.length === 20 ? (
) : null} {hotKeys.length ? ( {hotKeys.map((hk: HotKey) => { const { keySequence, orderSide, orderPrice, orderSize, orderSizeType, orderType, ioc, margin, reduceOnly, postOnly, } = hk const size = orderSizeType === 'percentage' ? t('settings:percentage-of-max', { size: orderSize }) : `$${orderSize}` const price = orderPrice ? `${orderPrice}% ${ orderSide === 'buy' ? t('settings:below') : t('settings:above') } oracle` : t('trade:market') const options = { margin: margin, IOC: ioc, post: postOnly, reduce: reduceOnly, } return ( ) })}
{t('settings:key-sequence')} {t('trade:order-type')} {t('trade:side')} {t('trade:size')} {t('price')} {t('settings:options')}
{keySequence} {t(`trade:${orderType}`)} {t(orderSide)} {size} {price} {Object.entries(options).map((e) => { return e[1] ? `${e[0] !== 'margin' ? ', ' : ''}${t( `trade:${e[0]}`, )}` : '' })}
handleDeleteKey(keySequence)} size="small" >
) : (

{t('settings:no-hot-keys')}

)} {showHotKeyModal ? ( setShowHotKeyModal(false)} /> ) : null} ) } export default HotKeysSettings type FormErrors = Partial> type HotKeyForm = { baseKey: string triggerKey: string price: string side: 'buy' | 'sell' size: string sizeType: 'percentage' | 'notional' orderType: 'limit' | 'market' ioc: boolean post: boolean margin: boolean reduce: boolean } const DEFAULT_FORM_VALUES: HotKeyForm = { baseKey: 'shift', triggerKey: '', price: '', side: 'buy', size: '', sizeType: 'percentage', orderType: 'limit', ioc: false, post: false, margin: false, reduce: false, } const HotKeyModal = ({ isOpen, onClose }: ModalProps) => { const { t } = useTranslation(['common', 'settings', 'trade']) const [hotKeys, setHotKeys] = useLocalStorageState(HOT_KEYS_KEY, []) const [hotKeyForm, setHotKeyForm] = useState({ ...DEFAULT_FORM_VALUES, }) const [formErrors, setFormErrors] = useState({}) const handleSetForm = (propertyName: string, value: string | boolean) => { setFormErrors({}) setHotKeyForm((prevState) => ({ ...prevState, [propertyName]: value })) } const handlePostOnlyChange = (postOnly: boolean) => { if (postOnly) { handleSetForm('ioc', !postOnly) } handleSetForm('post', postOnly) } const handleIocChange = (ioc: boolean) => { if (ioc) { handleSetForm('post', !ioc) } handleSetForm('ioc', ioc) } const isFormValid = (form: HotKeyForm) => { const invalidFields: FormErrors = {} setFormErrors({}) const triggerKey: (keyof HotKeyForm)[] = ['triggerKey'] const requiredFields: (keyof HotKeyForm)[] = ['size', 'price', 'triggerKey'] const numberFields: (keyof HotKeyForm)[] = ['size', 'price'] const alphanumericRegex = /^[a-zA-Z0-9]+$/ for (const key of triggerKey) { const value = form[key] as string if (value.length > 1) { invalidFields[key] = t('settings:error-too-many-characters') } if (!alphanumericRegex.test(value)) { invalidFields[key] = t('settings:error-alphanumeric-only') } } for (const key of requiredFields) { const value = form[key] as string if (!value) { if (hotKeyForm.orderType === 'market') { if (key !== 'price') { invalidFields[key] = t('settings:error-required-field') } } else { invalidFields[key] = t('settings:error-required-field') } } } for (const key of numberFields) { const value = form[key] as string if (value) { if (isNaN(parseFloat(value))) { invalidFields[key] = t('settings:error-must-be-number') } if (parseFloat(value) < 0) { invalidFields[key] = t('settings:error-must-be-above-zero') } if (parseFloat(value) > 100) { if (key === 'price') { invalidFields[key] = t('settings:error-must-be-below-100') } else { if (hotKeyForm.sizeType === 'percentage') { invalidFields[key] = t('settings:error-must-be-below-100') } } } } } const newKeySequence = `${form.baseKey}+${form.triggerKey}` const keyExists = hotKeys.find((k) => k.keySequence === newKeySequence) if (keyExists) { invalidFields.triggerKey = t('settings:error-key-in-use') } if (Object.keys(invalidFields).length) { setFormErrors(invalidFields) } return invalidFields } const handleSave = () => { const invalidFields = isFormValid(hotKeyForm) if (Object.keys(invalidFields).length) { return } const newHotKey = { keySequence: `${hotKeyForm.baseKey}+${hotKeyForm.triggerKey}`, orderSide: hotKeyForm.side, orderSizeType: hotKeyForm.sizeType, orderSize: hotKeyForm.size, orderType: hotKeyForm.orderType, orderPrice: hotKeyForm.price, ioc: hotKeyForm.ioc, margin: hotKeyForm.margin, postOnly: hotKeyForm.post, reduceOnly: hotKeyForm.reduce, } setHotKeys([...hotKeys, newHotKey]) onClose() } return ( <>

{t('settings:new-hot-key')}

handleSetForm('size', e.target.value)} suffix={hotKeyForm.sizeType === 'percentage' ? '%' : 'USD'} /> {formErrors.size ? (
) : null}
{hotKeyForm.orderType === 'limit' ? (
handleSetForm('price', e.target.value)} placeholder="e.g. 1%" suffix="%" /> {formErrors.price ? (
) : null}
) : null}
{hotKeyForm.orderType === 'limit' ? (
handlePostOnlyChange(e.target.checked)} > {t('trade:post')}
handleIocChange(e.target.checked)} > IOC
) : null}
handleSetForm('margin', e.target.checked)} > {t('trade:margin')}
handleSetForm('reduce', e.target.checked)} > {t('trade:reduce-only')}
) }