mango-v4-ui/components/trade/TradeAdvancedPage.tsx

229 lines
6.7 KiB
TypeScript
Raw Normal View History

import { useCallback, useMemo, useState } from 'react'
2022-09-13 23:24:26 -07:00
import dynamic from 'next/dynamic'
import ReactGridLayout, { Responsive, WidthProvider } from 'react-grid-layout'
2022-09-12 08:53:57 -07:00
import mangoStore from '@store/mangoStore'
2022-09-13 23:24:26 -07:00
import { GRID_LAYOUT_KEY } from 'utils/constants'
import useLocalStorageState from 'hooks/useLocalStorageState'
import { breakpoints } from 'utils/theme'
2022-09-16 03:49:10 -07:00
import { useViewport } from 'hooks/useViewport'
2022-09-13 23:24:26 -07:00
import AdvancedMarketHeader from './AdvancedMarketHeader'
import AdvancedTradeForm from './AdvancedTradeForm'
import TradeInfoTabs from './TradeInfoTabs'
2022-09-16 03:49:10 -07:00
import MobileTradeAdvancedPage from './MobileTradeAdvancedPage'
2022-09-20 13:05:50 -07:00
import OrderbookAndTrades from './OrderbookAndTrades'
2022-09-21 21:25:24 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
2022-09-22 21:38:17 -07:00
import TradeOnboardingTour from '@components/tours/TradeOnboardingTour'
2022-09-13 23:24:26 -07:00
const TradingViewChart = dynamic(() => import('./TradingViewChart'), {
ssr: false,
})
2022-09-19 22:01:23 -07:00
const ResponsiveGridLayout = WidthProvider(Responsive)
const getCurrentBreakpoint = () => {
// @ts-ignore
return Responsive.utils.getBreakpointFromWidth(
gridBreakpoints,
window.innerWidth - 63
)
}
2022-09-17 06:03:46 -07:00
2022-09-19 22:01:23 -07:00
const sidebarWidth = 65
const totalCols = 24
2022-09-13 23:24:26 -07:00
const gridBreakpoints = {
2022-09-17 06:03:46 -07:00
md: breakpoints.md - sidebarWidth,
lg: breakpoints.lg - sidebarWidth,
xl: breakpoints.xl - sidebarWidth,
xxl: breakpoints['2xl'] - sidebarWidth,
xxxl: breakpoints['3xl'] - sidebarWidth,
2022-09-13 23:24:26 -07:00
}
2022-09-16 03:49:10 -07:00
2022-09-19 22:01:23 -07:00
const getHeight = (
pageHeight: number,
minHeight: number,
remainingRowHeight: number
) => {
return Math.max(minHeight, pageHeight - remainingRowHeight)
2022-09-16 03:49:10 -07:00
}
2022-09-12 08:53:57 -07:00
const TradeAdvancedPage = () => {
2022-09-16 03:49:10 -07:00
const { height, width } = useViewport()
2022-09-13 23:24:26 -07:00
const [currentBreakpoint, setCurrentBreakpoint] = useState<string>()
const { uiLocked } = mangoStore((s) => s.settings)
2022-09-16 03:49:10 -07:00
const showMobileView = width <= breakpoints.md
2022-09-22 21:00:42 -07:00
const tourSettings = mangoStore((s) => s.settings.tours)
2022-09-21 21:25:24 -07:00
const { connected } = useWallet()
2022-09-13 23:24:26 -07:00
2022-09-19 22:01:23 -07:00
const defaultLayouts: ReactGridLayout.Layouts = useMemo(() => {
const topnavbarHeight = 67
const innerHeight = Math.max(height - topnavbarHeight, 800)
const marketHeaderHeight = 48
return {
xxxl: [
{ i: 'market-header', x: 0, y: 0, w: 16, h: marketHeaderHeight },
2022-09-29 13:27:11 -07:00
{ i: 'tv-chart', x: 0, y: 1, w: 16, h: 640 },
2022-09-19 22:01:23 -07:00
{
i: 'balances',
x: 0,
y: 2,
w: 16,
2022-09-29 13:27:11 -07:00
h: getHeight(innerHeight, 300, 640 + marketHeaderHeight),
2022-09-19 22:01:23 -07:00
},
{
i: 'orderbook',
x: 16,
y: 0,
w: 4,
h: getHeight(innerHeight, 976, marketHeaderHeight),
},
{ i: 'trade-form', x: 20, y: 0, w: 4, h: getHeight(innerHeight, 0, 0) },
],
xxl: [
{ i: 'market-header', x: 0, y: 0, w: 15, h: marketHeaderHeight },
2022-09-29 13:27:11 -07:00
{ i: 'tv-chart', x: 0, y: 1, w: 15, h: 536 },
2022-09-19 22:01:23 -07:00
{
i: 'balances',
x: 0,
y: 2,
w: 15,
2022-09-29 13:27:11 -07:00
h: getHeight(innerHeight, 300, 536 + marketHeaderHeight),
2022-09-19 22:01:23 -07:00
},
{
i: 'orderbook',
x: 15,
y: 0,
w: 4,
h: getHeight(innerHeight, 876, marketHeaderHeight),
},
{ i: 'trade-form', x: 19, y: 0, w: 5, h: getHeight(innerHeight, 0, 0) },
],
xl: [
{ i: 'market-header', x: 0, y: 0, w: 14, h: marketHeaderHeight },
2022-09-29 13:27:11 -07:00
{ i: 'tv-chart', x: 0, y: 1, w: 14, h: 488 },
2022-09-19 22:01:23 -07:00
{
i: 'balances',
x: 0,
y: 2,
w: 14,
2022-09-29 13:27:11 -07:00
h: getHeight(innerHeight, 300, 488 + marketHeaderHeight),
2022-09-19 22:01:23 -07:00
},
{
i: 'orderbook',
x: 14,
y: 0,
2022-09-21 21:46:30 -07:00
w: 5,
2022-09-19 22:01:23 -07:00
h: getHeight(innerHeight, 820, marketHeaderHeight),
},
2022-09-21 21:46:30 -07:00
{ i: 'trade-form', x: 19, y: 0, w: 5, h: getHeight(innerHeight, 0, 0) },
2022-09-19 22:01:23 -07:00
],
lg: [
{ i: 'market-header', x: 0, y: 0, w: 14, h: marketHeaderHeight },
2022-09-29 13:27:11 -07:00
{ i: 'tv-chart', x: 0, y: 1, w: 14, h: 488 },
2022-09-19 22:01:23 -07:00
{
i: 'balances',
x: 0,
y: 2,
w: 14,
2022-09-29 13:27:11 -07:00
h: getHeight(innerHeight, 300, 488 + marketHeaderHeight),
2022-09-19 22:01:23 -07:00
},
{
i: 'orderbook',
x: 14,
y: 0,
2022-09-21 21:46:30 -07:00
w: 5,
2022-09-19 22:01:23 -07:00
h: getHeight(innerHeight, 820, marketHeaderHeight),
},
2022-09-21 21:46:30 -07:00
{ i: 'trade-form', x: 19, y: 0, w: 5, h: getHeight(innerHeight, 0, 0) },
2022-09-19 22:01:23 -07:00
],
md: [
{ i: 'market-header', x: 0, y: 0, w: 18, h: marketHeaderHeight },
2022-09-29 13:27:11 -07:00
{ i: 'tv-chart', x: 0, y: 1, w: 18, h: 488 },
2022-09-19 22:01:23 -07:00
{ i: 'balances', x: 0, y: 2, w: 18, h: 488 },
2022-09-29 13:27:11 -07:00
{ i: 'orderbook', x: 18, y: 2, w: 6, h: 488 },
{ i: 'trade-form', x: 18, y: 1, w: 6, h: 488 },
2022-09-19 22:01:23 -07:00
],
}
}, [height])
2022-09-13 23:24:26 -07:00
const [savedLayouts, setSavedLayouts] = useLocalStorageState(
GRID_LAYOUT_KEY,
defaultLayouts
)
const onLayoutChange = useCallback((layouts: ReactGridLayout.Layouts) => {
if (layouts) {
setSavedLayouts(layouts)
}
}, [])
const onBreakpointChange = useCallback((newBreakpoint: string) => {
2022-09-28 09:45:53 -07:00
console.log('newBreakpoints', newBreakpoint)
2022-09-13 23:24:26 -07:00
setCurrentBreakpoint(newBreakpoint)
}, [])
2022-09-12 08:53:57 -07:00
2022-09-16 03:49:10 -07:00
return showMobileView ? (
<MobileTradeAdvancedPage />
) : (
2022-09-21 21:25:24 -07:00
<>
<ResponsiveGridLayout
// layouts={savedLayouts ? savedLayouts : defaultLayouts}
layouts={defaultLayouts}
breakpoints={gridBreakpoints}
cols={{
xxxl: totalCols,
xxl: totalCols,
xl: totalCols,
lg: totalCols,
md: totalCols,
sm: totalCols,
}}
rowHeight={1}
isDraggable={!uiLocked}
isResizable={!uiLocked}
onBreakpointChange={(newBreakpoint) =>
onBreakpointChange(newBreakpoint)
}
onLayoutChange={(layout, layouts) => onLayoutChange(layouts)}
measureBeforeMount
containerPadding={[0, 0]}
margin={[0, 0]}
2022-09-22 21:08:56 -07:00
useCSSTransforms
2022-09-16 03:49:10 -07:00
>
2022-09-21 21:25:24 -07:00
<div key="market-header" className="z-10">
<AdvancedMarketHeader />
</div>
<div
key="tv-chart"
className="h-full border border-x-0 border-th-bkg-3"
>
<div className={`relative h-full overflow-auto`}>
<TradingViewChart />
</div>
</div>
<div key="balances">
2022-09-30 05:55:28 -07:00
<TradeInfoTabs />
2022-09-21 21:25:24 -07:00
</div>
<div
key="trade-form"
className="border border-t-0 border-r-0 border-th-bkg-3 md:border-b lg:border-b-0"
>
<AdvancedTradeForm />
</div>
<div
key="orderbook"
className="border border-y-0 border-r-0 border-th-bkg-3"
>
<OrderbookAndTrades />
</div>
</ResponsiveGridLayout>
2022-09-22 21:00:42 -07:00
{!tourSettings?.trade_tour_seen && connected ? (
2022-09-21 21:25:24 -07:00
<TradeOnboardingTour />
) : null}
</>
2022-09-12 08:53:57 -07:00
)
}
export default TradeAdvancedPage