mango-ui-v3/components/trade_form/TradeForm.tsx

73 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-09-25 02:40:35 -07:00
import { useMemo, useState } from 'react'
2021-09-24 05:24:17 -07:00
import { SwitchHorizontalIcon } from '@heroicons/react/outline'
2021-09-25 02:40:35 -07:00
import { getWeights } from '@blockworks-foundation/mango-client'
2021-09-26 06:20:51 -07:00
import useMangoStore from '../../stores/useMangoStore'
2021-09-24 05:24:17 -07:00
import AdvancedTradeForm from './AdvancedTradeForm'
import SimpleTradeForm from './SimpleTradeForm'
import {
2021-09-24 05:24:17 -07:00
FlipCard,
FlipCardBack,
FlipCardFront,
FlipCardInner,
StyledFloatingElement,
2021-09-26 06:20:51 -07:00
} from '../FlipCard'
2021-04-18 03:34:37 -07:00
2021-04-12 20:39:08 -07:00
export default function TradeForm() {
2021-09-24 05:24:17 -07:00
const [showAdvancedFrom, setShowAdvancedForm] = useState(true)
2021-09-25 02:40:35 -07:00
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
2021-09-25 05:41:04 -07:00
const connected = useMangoStore((s) => s.wallet.connected)
2021-04-02 11:26:21 -07:00
2021-09-24 05:24:17 -07:00
const handleFormChange = () => {
setShowAdvancedForm(!showAdvancedFrom)
2021-04-02 11:26:21 -07:00
}
2021-09-25 02:40:35 -07:00
const initLeverage = useMemo(() => {
if (!mangoGroup || !marketConfig) return 1
const ws = getWeights(mangoGroup, marketConfig.marketIndex, 'Init')
const w =
marketConfig.kind === 'perp' ? ws.perpAssetWeight : ws.spotAssetWeight
return Math.round((100 * -1) / (w.toNumber() - 1)) / 100
}, [mangoGroup, marketConfig])
2021-09-24 05:24:17 -07:00
return (
<FlipCard>
<FlipCardInner flip={showAdvancedFrom}>
{showAdvancedFrom ? (
<FlipCardFront>
2021-09-26 06:20:51 -07:00
<StyledFloatingElement
className="h-full px-1 py-0 md:px-4 md:py-4"
showConnect
>
2021-09-25 05:41:04 -07:00
<div className={`${!connected ? 'filter blur-sm' : ''}`}>
{/* <button
2021-09-25 05:41:04 -07:00
onClick={handleFormChange}
2021-09-26 06:20:51 -07:00
className="absolute hidden md:flex items-center justify-center right-4 rounded-full bg-th-bkg-3 w-8 h-8 hover:text-th-primary focus:outline-none"
2021-09-25 05:41:04 -07:00
>
<SwitchHorizontalIcon className="w-5 h-5" />
</button> */}
2021-09-25 05:41:04 -07:00
<AdvancedTradeForm initLeverage={initLeverage} />
</div>
2021-09-24 05:24:17 -07:00
</StyledFloatingElement>
</FlipCardFront>
) : (
2021-09-24 05:24:17 -07:00
<FlipCardBack>
2021-09-26 06:20:51 -07:00
<StyledFloatingElement className="h-full px-1 md:px-4" showConnect>
2021-09-25 05:41:04 -07:00
<div className={`${!connected ? 'filter blur-sm' : ''}`}>
<button
onClick={handleFormChange}
className="absolute flex items-center justify-center right-4 rounded-full bg-th-bkg-3 w-8 h-8 hover:text-th-primary focus:outline-none"
>
<SwitchHorizontalIcon className="w-5 h-5" />
</button>
<SimpleTradeForm initLeverage={initLeverage} />
</div>
2021-09-24 05:24:17 -07:00
</StyledFloatingElement>
</FlipCardBack>
)}
2021-09-24 05:24:17 -07:00
</FlipCardInner>
</FlipCard>
2021-04-02 11:26:21 -07:00
)
}