mango-ui-v3/components/TradePageGrid.tsx

179 lines
6.0 KiB
TypeScript
Raw Normal View History

2021-04-02 11:26:21 -07:00
import dynamic from 'next/dynamic'
import { Responsive, WidthProvider } from 'react-grid-layout'
2021-08-28 00:49:41 -07:00
import { round, max } from 'lodash'
import MobileTradePage from './mobile/MobileTradePage'
2021-04-02 11:26:21 -07:00
const TVChartContainer = dynamic(
() => import('../components/TradingView/index'),
{ ssr: false }
)
import { useEffect, useState } from 'react'
2021-04-02 11:26:21 -07:00
import FloatingElement from '../components/FloatingElement'
2021-06-17 14:07:10 -07:00
import Orderbook from '../components/Orderbook'
2021-08-16 10:00:43 -07:00
import AccountInfo from './AccountInfo'
import UserMarketInfo from './UserMarketInfo'
2021-09-26 06:20:51 -07:00
import TradeForm from './trade_form/TradeForm'
import UserInfo from './UserInfo'
2021-07-19 10:13:03 -07:00
import RecentMarketTrades from './RecentMarketTrades'
import useMangoStore from '../stores/useMangoStore'
2021-04-16 13:17:33 -07:00
import useLocalStorageState from '../hooks/useLocalStorageState'
import { useViewport } from '../hooks/useViewport'
import MarketDetails from './MarketDetails'
2021-04-02 11:26:21 -07:00
const ResponsiveGridLayout = WidthProvider(Responsive)
export const defaultLayouts = {
xl: [
2021-05-01 21:31:55 -07:00
{ i: 'tvChart', x: 0, y: 0, w: 6, h: 30 },
{ i: 'orderbook', x: 6, y: 0, w: 3, h: 17 },
{ i: 'tradeForm', x: 9, y: 1, w: 3, h: 19 },
2021-05-01 21:31:55 -07:00
{ i: 'marketTrades', x: 6, y: 1, w: 3, h: 13 },
{ i: 'accountInfo', x: 9, y: 3, w: 3, h: 16 },
2021-06-12 10:46:06 -07:00
{ i: 'userInfo', x: 0, y: 2, w: 9, h: 19 },
2021-09-19 22:00:02 -07:00
{ i: 'marketPosition', x: 9, y: 4, w: 3, h: 13 },
],
lg: [
{ i: 'tvChart', x: 0, y: 0, w: 8, h: 29, minW: 2 },
2021-09-19 22:00:02 -07:00
{ i: 'marketPosition', x: 8, y: 0, w: 4, h: 13, minW: 2 },
{ i: 'accountInfo', x: 8, y: 1, w: 4, h: 16, minW: 2 },
2021-05-01 21:31:55 -07:00
{ i: 'orderbook', x: 0, y: 2, w: 4, h: 17, minW: 2 },
{ i: 'tradeForm', x: 4, y: 2, w: 4, h: 17, minW: 3 },
{ i: 'marketTrades', x: 8, y: 2, w: 4, h: 17, minW: 2 },
2021-06-12 10:46:06 -07:00
{ i: 'userInfo', x: 0, y: 3, w: 12, h: 19, minW: 6 },
2021-05-01 21:31:55 -07:00
],
md: [
{ i: 'tvChart', x: 0, y: 0, w: 8, h: 29, minW: 2 },
2021-09-19 22:00:02 -07:00
{ i: 'marketPosition', x: 8, y: 0, w: 4, h: 13, minW: 2 },
{ i: 'accountInfo', x: 8, y: 1, w: 4, h: 16, minW: 2 },
2021-05-01 21:31:55 -07:00
{ i: 'orderbook', x: 0, y: 2, w: 4, h: 17, minW: 2 },
{ i: 'tradeForm', x: 4, y: 2, w: 4, h: 17, minW: 3 },
{ i: 'marketTrades', x: 8, y: 2, w: 4, h: 17, minW: 2 },
2021-06-12 10:46:06 -07:00
{ i: 'userInfo', x: 0, y: 3, w: 12, h: 19, minW: 6 },
2021-05-01 21:31:55 -07:00
],
sm: [
{ i: 'tvChart', x: 0, y: 0, w: 12, h: 25, minW: 6 },
{ i: 'marketPosition', x: 0, y: 1, w: 6, h: 16, minW: 2 },
{ i: 'accountInfo', x: 6, y: 1, w: 6, h: 16, minW: 2 },
{ i: 'tradeForm', x: 0, y: 2, w: 12, h: 18, minW: 3 },
2021-05-01 21:31:55 -07:00
{ i: 'orderbook', x: 0, y: 3, w: 6, h: 17, minW: 3 },
{ i: 'marketTrades', x: 6, y: 3, w: 6, h: 17, minW: 2 },
2021-06-12 10:46:06 -07:00
{ i: 'userInfo', x: 0, y: 4, w: 12, h: 19, minW: 6 },
2021-05-01 21:31:55 -07:00
],
xs: [
2021-11-30 03:51:33 -08:00
{ i: 'tvChart', x: 0, y: 0, w: 12, h: 25, minW: 6 },
2021-09-19 22:00:02 -07:00
{ i: 'marketPosition', x: 0, y: 1, w: 6, h: 13, minW: 2 },
2021-08-24 20:15:57 -07:00
{ i: 'accountInfo', x: 0, y: 2, w: 6, h: 15, minW: 2 },
{ i: 'tradeForm', x: 0, y: 3, w: 12, h: 17, minW: 3 },
2021-05-01 21:31:55 -07:00
{ i: 'orderbook', x: 0, y: 4, w: 6, h: 17, minW: 3 },
{ i: 'marketTrades', x: 0, y: 5, w: 6, h: 17, minW: 2 },
2021-06-12 10:46:06 -07:00
{ i: 'userInfo', x: 0, y: 6, w: 12, h: 19, minW: 6 },
],
}
export const GRID_LAYOUT_KEY = 'mangoSavedLayouts-3.0.10'
2021-11-30 03:51:33 -08:00
export const breakpoints = { xl: 1600, lg: 1280, md: 1024, sm: 768, xs: 0 }
2021-05-01 21:31:55 -07:00
2021-04-02 11:26:21 -07:00
const TradePageGrid = () => {
const { uiLocked } = useMangoStore((s) => s.settings)
2021-04-16 13:17:33 -07:00
const [savedLayouts, setSavedLayouts] = useLocalStorageState(
2021-05-01 21:31:55 -07:00
GRID_LAYOUT_KEY,
2021-04-16 13:17:33 -07:00
defaultLayouts
)
const { width } = useViewport()
const isMobile = width ? width < breakpoints.sm : false
2021-04-16 13:17:33 -07:00
2021-08-28 00:49:41 -07:00
const getCurrentBreakpoint = () => {
return Responsive.utils.getBreakpointFromWidth(
breakpoints,
window.innerWidth - 63
)
}
2021-04-16 14:54:46 -07:00
const onLayoutChange = (layouts) => {
if (layouts) {
setSavedLayouts(layouts)
}
2021-04-16 13:17:33 -07:00
}
2021-08-28 00:49:41 -07:00
const onBreakpointChange = (newBreakpoint: string) => {
setCurrentBreakpoint(newBreakpoint)
}
const [orderbookDepth, setOrderbookDepth] = useState(8)
const [currentBreakpoint, setCurrentBreakpoint] = useState(null)
const [mounted, setMounted] = useState(false)
2021-08-28 00:49:41 -07:00
useEffect(() => {
const adjustOrderBook = (layouts, breakpoint?: string | null) => {
const bp = breakpoint ? breakpoint : getCurrentBreakpoint()
const orderbookLayout = layouts[bp].find((obj) => {
return obj.i === 'orderbook'
})
let depth = orderbookLayout.h * 0.891 - 7.2
depth = round(max([1, depth]))
setOrderbookDepth(depth)
}
adjustOrderBook(savedLayouts, currentBreakpoint)
}, [currentBreakpoint, savedLayouts])
useEffect(() => setMounted(true), [])
if (!mounted) return null
2021-08-16 10:00:43 -07:00
return !isMobile ? (
<>
<MarketDetails />
<ResponsiveGridLayout
className="layout"
layouts={savedLayouts || defaultLayouts}
breakpoints={breakpoints}
cols={{ xl: 12, lg: 12, md: 12, sm: 12, xs: 1 }}
rowHeight={15}
isDraggable={!uiLocked}
isResizable={!uiLocked}
onBreakpointChange={(newBreakpoint) =>
onBreakpointChange(newBreakpoint)
}
onLayoutChange={(layout, layouts) => onLayoutChange(layouts)}
useCSSTransforms={false}
>
<div key="tvChart">
<FloatingElement className="h-full pl-0">
<TVChartContainer />
</FloatingElement>
</div>
<div key="orderbook">
<Orderbook depth={orderbookDepth} />
</div>
<div key="tradeForm">
2021-09-24 05:24:17 -07:00
<TradeForm />
</div>
<div key="accountInfo">
<FloatingElement className="h-full" showConnect>
<AccountInfo />
</FloatingElement>
</div>
<div key="userInfo">
<FloatingElement className="h-full" showConnect>
<UserInfo />
</FloatingElement>
</div>
2021-09-19 22:00:02 -07:00
<div key="marketPosition">
<FloatingElement className="h-full" showConnect>
<UserMarketInfo />
</FloatingElement>
</div>
<div key="marketTrades">
<FloatingElement className="h-full">
<RecentMarketTrades />
</FloatingElement>
</div>
</ResponsiveGridLayout>
</>
) : (
<MobileTradePage />
2021-04-02 11:26:21 -07:00
)
}
export default TradePageGrid