mango-ui-v3/components/TradeForm.tsx

65 lines
2.3 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'
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,
} 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-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>
<StyledFloatingElement className="h-full">
<button
onClick={handleFormChange}
className="absolute flex items-center justify-center right-4 top-4 rounded-full bg-th-bkg-3 w-8 h-8 hover:text-th-primary focus:outline-none"
2021-07-22 04:34:03 -07:00
>
2021-09-24 05:24:17 -07:00
<SwitchHorizontalIcon className="w-5 h-5" />
</button>
2021-09-25 02:40:35 -07:00
<AdvancedTradeForm initLeverage={initLeverage} />
2021-09-24 05:24:17 -07:00
</StyledFloatingElement>
</FlipCardFront>
) : (
2021-09-24 05:24:17 -07:00
<FlipCardBack>
<StyledFloatingElement className="h-full">
<button
onClick={handleFormChange}
className="absolute flex items-center justify-center right-4 top-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>
2021-09-25 02:40:35 -07:00
<SimpleTradeForm initLeverage={initLeverage} />
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
)
}