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

356 lines
10 KiB
TypeScript
Raw Normal View History

2023-07-20 03:40:27 -07:00
import { useCallback, useLayoutEffect, useMemo, useState } from 'react'
2022-09-13 23:24:26 -07:00
import dynamic from 'next/dynamic'
2023-07-19 22:30:35 -07:00
import ReactGridLayout, {
Layout,
Layouts,
Responsive,
WidthProvider,
} from 'react-grid-layout'
2022-09-12 08:53:57 -07:00
import mangoStore from '@store/mangoStore'
2022-12-20 01:13:24 -08:00
// import { IS_ONBOARDED_KEY } from 'utils/constants'
// import useLocalStorageState from 'hooks/useLocalStorageState'
2022-09-13 23:24:26 -07:00
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-12-20 01:13:24 -08:00
// import { useWallet } from '@solana/wallet-adapter-react'
// import TradeOnboardingTour from '@components/tours/TradeOnboardingTour'
2022-11-29 20:01:55 -08:00
import FavoriteMarketsBar from './FavoriteMarketsBar'
import useLocalStorageState from 'hooks/useLocalStorageState'
2023-07-20 03:40:27 -07:00
import {
DEPTH_CHART_KEY,
SIDEBAR_COLLAPSE_KEY,
TRADE_LAYOUT_KEY,
} from 'utils/constants'
2023-06-21 17:48:15 -07:00
import TradeHotKeys from './TradeHotKeys'
2023-03-26 20:11:10 -07:00
export type TradeLayout =
| 'chartLeft'
| 'chartMiddleOBRight'
| 'chartMiddleOBLeft'
| 'chartRight'
2022-09-13 23:24:26 -07:00
2022-12-06 09:35:15 -08:00
const TradingChartContainer = dynamic(() => import('./TradingChartContainer'), {
ssr: false,
})
2022-09-13 23:24:26 -07:00
2022-09-19 22:01:23 -07:00
const getHeight = (
pageHeight: number,
minHeight: number,
2023-07-21 11:47:53 -07:00
remainingRowHeight: number,
2022-09-19 22:01:23 -07:00
) => {
return Math.max(minHeight, pageHeight - remainingRowHeight)
2022-09-16 03:49:10 -07:00
}
2023-07-20 03:40:27 -07:00
const ResponsiveGridLayout = WidthProvider(Responsive)
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 { uiLocked } = mangoStore((s) => s.settings)
2022-09-16 03:49:10 -07:00
const showMobileView = width <= breakpoints.md
2022-12-20 01:13:24 -08:00
// const tourSettings = mangoStore((s) => s.settings.tours)
// const { connected } = useWallet()
// const [isOnboarded] = useLocalStorageState(IS_ONBOARDED_KEY)
const [tradeLayout] = useLocalStorageState<TradeLayout>(
TRADE_LAYOUT_KEY,
2023-07-21 11:47:53 -07:00
'chartLeft',
)
2023-07-20 03:40:27 -07:00
const [isCollapsed] = useLocalStorageState(SIDEBAR_COLLAPSE_KEY, false)
2023-07-12 07:37:18 -07:00
const [showDepthChart] = useLocalStorageState<boolean>(DEPTH_CHART_KEY, false)
2022-09-13 23:24:26 -07:00
2023-07-20 03:40:27 -07:00
const totalCols = 24
const gridBreakpoints = useMemo(() => {
const sidebarWidth = isCollapsed ? 64 : 207
return {
md: breakpoints.md - sidebarWidth,
lg: breakpoints.lg - sidebarWidth,
xl: breakpoints.xl - sidebarWidth,
xxl: breakpoints['2xl'] - sidebarWidth,
xxxl: breakpoints['3xl'] - sidebarWidth,
}
}, [isCollapsed])
2023-07-19 22:30:35 -07:00
2022-09-19 22:01:23 -07:00
const defaultLayouts: ReactGridLayout.Layouts = useMemo(() => {
2023-07-13 03:37:41 -07:00
const topnavbarHeight = 64
2022-11-19 17:40:06 -08:00
const innerHeight = Math.max(height - topnavbarHeight, 1000)
2022-09-19 22:01:23 -07:00
const marketHeaderHeight = 48
const balancesXPos = {
chartLeft: { xxxl: 0, xxl: 0, xl: 0, lg: 0 },
chartMiddleOBRight: { xxxl: 4, xxl: 5, xl: 5, lg: 6 },
2023-07-13 03:37:41 -07:00
chartMiddleOBLeft: { xxxl: 0, xxl: 0, xl: 0, lg: 0 },
chartRight: { xxxl: 4, xxl: 5, xl: 5, lg: 5 },
}
const chartXPos = {
chartLeft: { xxxl: 0, xxl: 0, xl: 0, lg: 0 },
2023-07-13 03:37:41 -07:00
chartMiddleOBRight: { xxxl: 4, xxl: 5, xl: 5, lg: 5 },
chartMiddleOBLeft: {
xxxl: showDepthChart ? 7 : 4,
xxl: showDepthChart ? 7 : 4,
xl: showDepthChart ? 7 : 4,
lg: showDepthChart ? 8 : 5,
},
chartRight: {
xxxl: showDepthChart ? 12 : 9,
xxl: showDepthChart ? 12 : 9,
xl: showDepthChart ? 12 : 9,
lg: showDepthChart ? 14 : 11,
},
}
const bookXPos = {
2023-07-13 03:37:41 -07:00
chartLeft: {
xxxl: showDepthChart ? 13 : 16,
xxl: showDepthChart ? 12 : 15,
xl: showDepthChart ? 12 : 15,
lg: showDepthChart ? 11 : 14,
},
chartMiddleOBRight: {
xxxl: showDepthChart ? 17 : 20,
xxl: showDepthChart ? 17 : 20,
xl: showDepthChart ? 17 : 20,
lg: showDepthChart ? 16 : 19,
},
chartMiddleOBLeft: { xxxl: 0, xxl: 0, xl: 0, lg: 0 },
2023-07-20 17:09:34 -07:00
chartRight: { xxxl: 4, xxl: 5, xl: 5, lg: 5 },
}
const formXPos = {
2023-07-12 07:37:18 -07:00
chartLeft: { xxxl: 20, xxl: 19, xl: 19, lg: 19 },
chartMiddleOBRight: { xxxl: 0, xxl: 0, xl: 0, lg: 0 },
2023-07-13 03:37:41 -07:00
chartMiddleOBLeft: { xxxl: 20, xxl: 19, xl: 19, lg: 19 },
chartRight: { xxxl: 0, xxl: 0, xl: 0, lg: 0 },
}
2022-09-19 22:01:23 -07:00
return {
xxxl: [
{ i: 'market-header', x: 0, y: 0, w: 24, h: marketHeaderHeight },
2022-09-19 22:01:23 -07:00
{
2023-07-13 03:37:41 -07:00
i: 'tv-chart',
x: chartXPos[tradeLayout].xxxl,
y: 1,
w: showDepthChart ? 13 : 16,
h: 640,
2022-09-19 22:01:23 -07:00
},
{
i: 'orderbook',
x: bookXPos[tradeLayout].xxxl,
2023-07-12 07:37:18 -07:00
y: 1,
2023-07-13 03:37:41 -07:00
w: showDepthChart ? 7 : 4,
2023-07-12 07:37:18 -07:00
h: 640,
},
{
i: 'trade-form',
x: formXPos[tradeLayout].xxxl,
2023-07-12 07:37:18 -07:00
y: 1,
2022-09-19 22:01:23 -07:00
w: 4,
2022-10-23 04:38:48 -07:00
h: getHeight(innerHeight, 0, 0),
2022-09-19 22:01:23 -07:00
},
2023-07-13 03:37:41 -07:00
{
i: 'balances',
x: balancesXPos[tradeLayout].xxxl,
y: 2,
w: 20,
h: getHeight(innerHeight, 0, 640),
},
2022-09-19 22:01:23 -07:00
],
xxl: [
{ i: 'market-header', x: 0, y: 0, w: 24, h: marketHeaderHeight },
2022-09-19 22:01:23 -07:00
{
2023-07-13 03:37:41 -07:00
i: 'tv-chart',
x: chartXPos[tradeLayout].xxl,
y: 1,
w: showDepthChart ? 12 : 15,
h: 552,
2022-09-19 22:01:23 -07:00
},
{
i: 'orderbook',
x: bookXPos[tradeLayout].xxl,
2023-07-12 07:37:18 -07:00
y: 1,
2023-07-13 03:37:41 -07:00
w: showDepthChart ? 7 : 4,
h: 552,
2022-09-19 22:01:23 -07:00
},
{
i: 'trade-form',
x: formXPos[tradeLayout].xxl,
2023-07-12 07:37:18 -07:00
y: 1,
w: 5,
h: getHeight(innerHeight, 0, 0),
},
2023-07-13 03:37:41 -07:00
{
i: 'balances',
x: balancesXPos[tradeLayout].xxl,
y: 2,
w: 19,
h: getHeight(innerHeight, 0, 552),
},
2022-09-19 22:01:23 -07:00
],
xl: [
{ i: 'market-header', x: 0, y: 0, w: 24, h: marketHeaderHeight },
2023-07-12 07:37:18 -07:00
{
i: 'tv-chart',
x: chartXPos[tradeLayout].xl,
y: 1,
w: showDepthChart ? 12 : 15,
h: 552,
},
2022-09-19 22:01:23 -07:00
{
i: 'orderbook',
x: bookXPos[tradeLayout].xl,
2023-07-12 07:37:18 -07:00
y: 1,
w: showDepthChart ? 7 : 4,
h: 552,
2022-10-23 04:38:48 -07:00
},
{
i: 'trade-form',
x: formXPos[tradeLayout].xl,
2023-07-12 07:37:18 -07:00
y: 1,
2022-10-23 04:38:48 -07:00
w: 5,
h: getHeight(innerHeight, 0, 0),
2022-09-19 22:01:23 -07:00
},
{
i: 'balances',
2023-07-13 03:37:41 -07:00
x: balancesXPos[tradeLayout].xl,
2022-09-19 22:01:23 -07:00
y: 2,
2023-07-12 07:37:18 -07:00
w: 19,
2023-07-13 03:37:41 -07:00
h: getHeight(innerHeight, 0, 552),
},
],
lg: [
{ i: 'market-header', x: 0, y: 0, w: 24, h: marketHeaderHeight },
{
i: 'tv-chart',
x: chartXPos[tradeLayout].lg,
y: 1,
w: showDepthChart ? 11 : 14,
h: 552,
2022-09-19 22:01:23 -07:00
},
{
i: 'orderbook',
x: bookXPos[tradeLayout].lg,
2023-07-12 07:37:18 -07:00
y: 1,
2023-07-13 03:37:41 -07:00
w: showDepthChart ? 8 : 5,
h: 552,
2022-09-19 22:01:23 -07:00
},
{
i: 'trade-form',
x: formXPos[tradeLayout].lg,
2023-07-12 07:37:18 -07:00
y: 1,
w: 5,
h: getHeight(innerHeight, 0, 0),
},
2023-07-13 03:37:41 -07:00
{
i: 'balances',
x: balancesXPos[tradeLayout].lg,
y: 2,
w: 19,
h: getHeight(innerHeight, 0, 552),
},
2022-09-19 22:01:23 -07:00
],
md: [
{ i: 'market-header', x: 0, y: 0, w: 24, h: marketHeaderHeight },
2022-11-14 02:18:38 -08:00
{ i: 'tv-chart', x: 0, y: 1, w: 17, h: 464 },
2023-07-13 03:37:41 -07:00
{ i: 'orderbook', x: 18, y: 2, w: 7, h: 552 },
{ i: 'trade-form', x: 18, y: 1, w: 7, h: 572 },
2022-11-14 02:18:38 -08:00
{ i: 'balances', x: 0, y: 2, w: 17, h: 428 + marketHeaderHeight },
2022-09-19 22:01:23 -07:00
],
}
2023-07-12 07:37:18 -07:00
}, [height, showDepthChart, tradeLayout])
2022-09-13 23:24:26 -07:00
2023-07-19 22:30:35 -07:00
const [layouts, setLayouts] = useState<Layouts>(defaultLayouts)
2023-07-20 03:40:27 -07:00
const [breakpoint, setBreakpoint] = useState('')
2023-07-19 22:30:35 -07:00
const handleLayoutChange = useCallback(
(layout: Layout[] | undefined, layouts: Layouts) => {
setLayouts(layouts)
},
2023-07-21 11:47:53 -07:00
[setLayouts],
2023-07-19 22:30:35 -07:00
)
2023-07-20 03:40:27 -07:00
useLayoutEffect(() => {
2023-07-19 22:30:35 -07:00
handleLayoutChange(undefined, defaultLayouts)
2023-07-20 03:40:27 -07:00
}, [breakpoint, showDepthChart, tradeLayout])
2023-07-19 22:30:35 -07:00
2022-09-16 03:49:10 -07:00
return showMobileView ? (
<MobileTradeAdvancedPage />
) : (
2023-06-21 17:48:15 -07:00
<TradeHotKeys>
2022-11-29 20:01:55 -08:00
<FavoriteMarketsBar />
2022-09-21 21:25:24 -07:00
<ResponsiveGridLayout
2023-07-19 22:30:35 -07:00
layouts={layouts}
2022-09-21 21:25:24 -07:00
breakpoints={gridBreakpoints}
2023-07-20 03:40:27 -07:00
onBreakpointChange={(bp) => setBreakpoint(bp)}
2022-09-21 21:25:24 -07:00
cols={{
xxxl: totalCols,
xxl: totalCols,
xl: totalCols,
lg: totalCols,
md: totalCols,
sm: totalCols,
}}
rowHeight={1}
isDraggable={!uiLocked}
isResizable={!uiLocked}
containerPadding={[0, 0]}
margin={[0, 0]}
2022-09-22 21:08:56 -07:00
useCSSTransforms
2023-07-19 22:30:35 -07:00
onLayoutChange={handleLayoutChange}
measureBeforeMount
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"
2023-07-20 17:09:34 -07:00
className="h-full border border-x-0 border-th-bkg-3"
2022-09-21 21:25:24 -07:00
>
<div className={`relative h-full overflow-auto`}>
2022-12-06 09:35:15 -08:00
<TradingChartContainer />
2022-09-21 21:25:24 -07:00
</div>
</div>
2023-07-20 17:09:34 -07:00
<div
className={`${
tradeLayout === 'chartLeft' ? 'lg:border-r lg:border-th-bkg-3' : ''
}`}
key="balances"
>
2022-09-30 05:55:28 -07:00
<TradeInfoTabs />
2022-09-21 21:25:24 -07:00
</div>
<div
2023-07-20 17:09:34 -07:00
className={`border-y border-l border-th-bkg-3 lg:border-b-0 ${
2023-07-13 03:37:41 -07:00
tradeLayout === 'chartMiddleOBRight'
? 'lg:border-r lg:border-l-0'
: ''
2023-07-20 17:09:34 -07:00
} ${
tradeLayout === 'chartRight' ? 'lg:border-r lg:border-l-0' : ''
} ${tradeLayout === 'chartLeft' ? 'lg:border-l-0' : ''}`}
2022-09-21 21:25:24 -07:00
key="trade-form"
>
<AdvancedTradeForm />
</div>
<div
key="orderbook"
2023-07-20 17:09:34 -07:00
className={`overflow-hidden border-l border-th-bkg-3 lg:border-y ${
2023-07-13 03:37:41 -07:00
tradeLayout === 'chartRight' ? 'lg:border-l-0 lg:border-r' : ''
} ${
tradeLayout === 'chartMiddleOBLeft'
? 'lg:border-l-0 lg:border-r'
: ''
2023-07-20 17:09:34 -07:00
} ${tradeLayout === 'chartLeft' ? 'lg:border-r' : ''}`}
2022-09-21 21:25:24 -07:00
>
<OrderbookAndTrades />
2022-09-21 21:25:24 -07:00
</div>
</ResponsiveGridLayout>
2022-12-20 01:13:24 -08:00
{/* {!tourSettings?.trade_tour_seen && isOnboarded && connected ? (
2022-09-21 21:25:24 -07:00
<TradeOnboardingTour />
2022-12-20 01:13:24 -08:00
) : null} */}
2023-06-21 17:48:15 -07:00
</TradeHotKeys>
2022-09-12 08:53:57 -07:00
)
}
export default TradeAdvancedPage