-
Notifications
You must be signed in to change notification settings - Fork 6
/
[market_id].tsx
304 lines (284 loc) · 10.6 KB
/
[market_id].tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import type { GetStaticPaths, GetStaticProps } from "next";
import dynamic from "next/dynamic";
import Head from "next/head";
import { useRouter } from "next/router";
import Script from "next/script";
import React, { useEffect, useMemo, useState } from "react";
import { DepthChart } from "@/components/DepthChart";
import { Header } from "@/components/Header";
import { DepositWithdrawFlowModal } from "@/components/modals/flows/DepositWithdrawFlowModal";
import { WalletButtonFlowModal } from "@/components/modals/flows/WalletButtonFlowModal";
import { OrderbookTable } from "@/components/OrderbookTable";
import { StatsBar } from "@/components/StatsBar";
import MobileOrderEntry from "@/components/trade/MobileOrderEntry";
import { OrderEntry } from "@/components/trade/OrderEntry";
import { OrdersTable } from "@/components/trade/OrdersTable";
import { TradeHistoryTable } from "@/components/trade/TradeHistoryTable";
import { OrderEntryContextProvider } from "@/contexts/OrderEntryContext";
import { useOrderBook } from "@/hooks/useOrderbook";
import type { ApiMarket } from "@/types/api";
import { getAllMarket } from "@/utils/helpers";
type Props = {
marketData: ApiMarket;
allMarketData: ApiMarket[];
};
type PathParams = {
market_id: string;
};
export interface ChartContainerProps {
symbol: string;
}
export interface TVChartContainerProps {
selectedMarket: ApiMarket;
allMarketData: ApiMarket[];
}
let ChartContainer = dynamic(
() => {
try {
// We call `require` here for the private charting library before
// to facilitate SSR with a fallback to the lightweight library.
// If the import fails, the `catch` block is executed and we
// load the `LightweightChartsContainer` instead.
// If the library is present, the TVChartContainer component will
// be used, and it will load the `charting_library` module from the
// cache with its `require(...)`.
//
// NOTE: We must use `require` here instead of a dynamic `import`
// to circumvent the SSR build process failing due to an invalid path.
// With `import` the provided path is statically resolved at build time
// whereas with `require`, path resolution is deferred until runtime and can
// thus be conditionally resolved within a `try/catch` block.
require("../../../public/static/charting_library");
return import("@/components/trade/TVChartContainer").then(
(mod) => mod.TVChartContainer,
);
} catch (e) {
console.warn("\nFailed to load `charting_library`.");
console.warn("Using `lightweight-charts` instead...");
return import("@/components/trade/LightweightChartsContainer").then(
(mod) => mod.LightweightChartsContainer,
);
}
},
{ ssr: true },
);
export default function Market({ allMarketData, marketData }: Props) {
const router = useRouter();
const [tab, setTab] = useState<"orders" | "order-book" | "trade-histories">(
"orders",
);
const [depositWithdrawModalOpen, setDepositWithdrawModalOpen] =
useState<boolean>(false);
const [walletButtonModalOpen, setWalletButtonModalOpen] =
useState<boolean>(false);
const [isScriptReady, setIsScriptReady] = useState(false);
useEffect(() => {
if (router.query.lwc === "true") {
// NOTE: We may have already loaded the private charting library, but this is a feature
// for testing and debugging, so we don't need to check if the library is already loaded.
console.warn(
"Force loading LightweightChartsContainer. Avoid loading both libraries in production.",
);
// In a production app, you should load one or the other.
ChartContainer = dynamic(async () =>
import("@/components/trade/LightweightChartsContainer").then(
(mod) => mod.LightweightChartsContainer,
),
);
}
}, [router.query]);
useEffect(() => {
const f = () => {
const LG = 768;
if (window.innerWidth > LG) {
setTab("orders");
}
};
window.addEventListener("resize", f);
return () => window.removeEventListener("resize", f);
}, []);
const {
data: orderbookData,
isFetching: orderbookIsFetching,
isLoading: orderbookIsLoading,
} = useOrderBook(marketData.market_id);
const defaultTVChartProps = useMemo(() => {
return {
symbol: `${marketData?.name ?? ""}`,
selectedMarket: marketData as ApiMarket,
allMarketData: allMarketData as ApiMarket[],
};
}, [marketData, allMarketData]);
if (!marketData)
return (
<>
<Head>
<title>Not Found</title>
</Head>
<div className="flex min-h-screen flex-col">
<Header logoHref={`${allMarketData[0]?.market_id}`} />
Market not found.
</div>
</>
);
return (
<OrderEntryContextProvider>
<Head>
<title>{`${marketData.name} | Econia`}</title>
</Head>
<div className="scrollbar-none flex h-screen flex-col">
<Header
logoHref={`${allMarketData[0].market_id}`}
onDepositWithdrawClick={() => setDepositWithdrawModalOpen(true)}
onWalletButtonClick={() => setWalletButtonModalOpen(true)}
/>
{isScriptReady && (
<StatsBar allMarketData={allMarketData} selectedMarket={marketData} />
)}
<main className="flex h-full min-h-[680px] w-full grow flex-col gap-3 p-3 md:flex-row">
<div className="flex flex-col gap-3 pb-0 md:w-[calc(100%-296px)] lg:w-[calc(100%-564px)]">
<div className=" flex grow flex-col border border-neutral-600">
<div className="flex h-full min-h-[400px] md:min-h-[unset]">
{isScriptReady && <ChartContainer {...defaultTVChartProps} />}
</div>
<div className="hidden h-[140px] tall:block">
<DepthChart marketData={marketData} />
</div>
</div>
<div className="flex h-[260px] max-w-full flex-col border border-neutral-600">
<div className="flex h-[30px] items-center gap-4 pl-4 pt-2 lg:pl-0 lg:pt-[6px]">
<div className="flex gap-4 py-1 text-base lg:py-3 lg:pl-[17.19px]">
<p
onClick={() => setTab("orders")}
className={`cursor-pointer font-jost font-bold ${
tab === "orders" ? "text-white" : "text-neutral-600"
}`}
>
Orders
</p>
<p
onClick={() => setTab("order-book")}
className={`cursor-pointer font-jost font-bold lg:hidden ${
tab === "order-book" ? "text-white" : "text-neutral-600"
}`}
>
Order Book
</p>
<p
onClick={() => setTab("trade-histories")}
className={`cursor-pointer font-jost font-bold md:hidden ${
tab === "trade-histories"
? "text-white"
: "text-neutral-600"
}`}
>
Trade History
</p>
</div>
</div>
{tab === "orders" && (
<OrdersTable
market_id={marketData.market_id}
marketData={marketData}
/>
)}
{tab === "trade-histories" && (
<div className="h-full overflow-hidden">
<TradeHistoryTable
marketData={marketData}
marketId={marketData?.market_id}
/>
</div>
)}
{tab === "order-book" && (
<OrderbookTable
marketData={marketData}
data={orderbookData}
isFetching={orderbookIsFetching}
isLoading={orderbookIsLoading}
/>
)}
</div>
</div>
<div className="hidden w-[254px] lg:flex">
<div className="flex w-full flex-col border border-neutral-600">
<OrderbookTable
marketData={marketData}
data={orderbookData}
isFetching={orderbookIsFetching}
isLoading={orderbookIsLoading}
/>
</div>
</div>
<div className="hidden w-[284px] flex-col md:flex">
<div className="border border-neutral-600">
<OrderEntry
marketData={marketData}
onDepositWithdrawClick={() => setDepositWithdrawModalOpen(true)}
/>
</div>
<div className="scrollbar-none mt-3 h-full max-h-full grid-rows-none overflow-hidden border border-neutral-600">
<p className=" top-0 flex h-[30px] items-end bg-neutral-800 bg-noise pl-[17.03px] font-jost font-bold text-white">
Trade History
</p>
<TradeHistoryTable
marketData={marketData}
marketId={marketData?.market_id}
/>
</div>
</div>
<MobileOrderEntry
marketData={marketData}
onDepositWithdrawClick={() => setDepositWithdrawModalOpen(true)}
/>
</main>
</div>
{/* temp */}
<DepositWithdrawFlowModal
selectedMarket={marketData}
isOpen={depositWithdrawModalOpen}
onClose={() => {
setDepositWithdrawModalOpen(false);
}}
allMarketData={allMarketData}
/>
<WalletButtonFlowModal
selectedMarket={marketData}
isOpen={walletButtonModalOpen}
onClose={() => {
setWalletButtonModalOpen(false);
}}
allMarketData={allMarketData}
/>
<Script
src="/static/datafeeds/udf/dist/bundle.js"
strategy="lazyOnload"
onReady={() => {
setIsScriptReady(true);
}}
/>
</OrderEntryContextProvider>
);
}
export const getStaticPaths: GetStaticPaths<PathParams> = async () => {
const allMarketData = await getAllMarket();
const paths = allMarketData.map((market: ApiMarket) => ({
params: { market_id: `${market.market_id}` },
}));
return { paths, fallback: false };
};
export const getStaticProps: GetStaticProps = async ({ params }) => {
if (!params) throw new Error("No params");
const allMarketData = await getAllMarket();
const marketData =
allMarketData?.find(
(market) => `${market?.market_id}` === params.market_id,
) || null;
return {
props: {
marketData,
allMarketData,
},
revalidate: 600,
};
};