2022-03-01 00:59:18 +00:00
|
|
|
import { gql, useQuery } from '@apollo/client';
|
|
|
|
import AutoSizer from 'react-virtualized-auto-sizer';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { useRouter } from 'next/router';
|
2022-03-01 22:14:33 +00:00
|
|
|
import React, { Children, isValidElement, ReactNode } from 'react';
|
2022-03-01 00:59:18 +00:00
|
|
|
import { Market, MarketVariables } from './__generated__/Market';
|
|
|
|
|
|
|
|
// Top level page query
|
|
|
|
const MARKET_QUERY = gql`
|
|
|
|
query Market($marketId: ID!) {
|
|
|
|
market(id: $marketId) {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
trades {
|
|
|
|
id
|
|
|
|
price
|
|
|
|
size
|
|
|
|
createdAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const MarketPage = () => {
|
|
|
|
const { query } = useRouter();
|
|
|
|
const { data, loading, error } = useQuery<Market, MarketVariables>(
|
|
|
|
MARKET_QUERY,
|
|
|
|
{
|
|
|
|
variables: { marketId: query.marketId as string },
|
2022-03-01 21:01:52 +00:00
|
|
|
skip: !query.marketId,
|
2022-03-01 00:59:18 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (loading || !data) {
|
|
|
|
return <div>Loading...</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{error ? (
|
|
|
|
<div>Something went wrong: {error.message}</div>
|
|
|
|
) : (
|
2022-03-01 21:35:31 +00:00
|
|
|
<div className="h-full max-h-full grid gap-[1px] bg-black grid-cols-[1fr_400px_400px] grid-rows-[min-content_1fr_200px]">
|
2022-03-01 00:59:18 +00:00
|
|
|
<header className="bg-white col-span-4 p-8">
|
|
|
|
<h1>Market: {query.marketId}</h1>
|
|
|
|
</header>
|
2022-03-01 21:35:31 +00:00
|
|
|
<GridChild>TODO: Chart</GridChild>
|
|
|
|
<GridChild>TODO: Ticket</GridChild>
|
|
|
|
<GridChild>
|
2022-03-01 22:14:33 +00:00
|
|
|
<GridTabs group="trade">
|
|
|
|
<GridTab name="trades">
|
2022-03-01 21:35:31 +00:00
|
|
|
<pre>{JSON.stringify(data, null, 2)}</pre>
|
|
|
|
</GridTab>
|
2022-03-01 22:14:33 +00:00
|
|
|
<GridTab name="orderbook">Orderbook TODO:</GridTab>
|
2022-03-01 21:35:31 +00:00
|
|
|
</GridTabs>
|
2022-03-01 00:59:18 +00:00
|
|
|
</GridChild>
|
2022-03-01 21:35:31 +00:00
|
|
|
<GridChild className="col-span-4">
|
2022-03-01 22:14:33 +00:00
|
|
|
<GridTabs group="portfolio">
|
|
|
|
<GridTab name="orders">TODO: Orders</GridTab>
|
|
|
|
<GridTab name="positions">TODO: Positions</GridTab>
|
|
|
|
<GridTab name="collateral">TODO: Collateral</GridTab>
|
2022-03-01 21:35:31 +00:00
|
|
|
</GridTabs>
|
2022-03-01 00:59:18 +00:00
|
|
|
</GridChild>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MarketPage;
|
|
|
|
|
|
|
|
interface GridChildProps {
|
|
|
|
children: ReactNode;
|
|
|
|
className?: string;
|
|
|
|
}
|
|
|
|
|
2022-03-01 21:35:31 +00:00
|
|
|
const GridChild = ({ children, className }: GridChildProps) => {
|
2022-03-01 00:59:18 +00:00
|
|
|
const gridChildClasses = classNames('bg-white', className);
|
|
|
|
return (
|
|
|
|
<section className={gridChildClasses}>
|
2022-03-01 21:35:31 +00:00
|
|
|
<AutoSizer>
|
|
|
|
{({ width, height }) => (
|
|
|
|
<div style={{ width, height }} className="overflow-auto">
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</AutoSizer>
|
2022-03-01 00:59:18 +00:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
};
|
2022-03-01 21:35:31 +00:00
|
|
|
|
|
|
|
interface GridTabsProps {
|
|
|
|
children: ReactNode;
|
2022-03-01 22:14:33 +00:00
|
|
|
group: string;
|
2022-03-01 21:35:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 22:14:33 +00:00
|
|
|
const GridTabs = ({ children, group }: GridTabsProps) => {
|
|
|
|
const { query, asPath, replace } = useRouter();
|
2022-03-01 21:35:31 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="h-full grid grid-rows-[min-content_1fr]">
|
|
|
|
{/* the tabs */}
|
|
|
|
<div className="flex gap-[2px] bg-[#ededed]">
|
|
|
|
{Children.map(children, (child) => {
|
|
|
|
if (!isValidElement(child)) return null;
|
|
|
|
const buttonClass = classNames('p-8', {
|
2022-03-01 22:14:33 +00:00
|
|
|
'text-vega-pink': query[group] === child.props.name,
|
2022-03-01 21:35:31 +00:00
|
|
|
});
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className={buttonClass}
|
2022-03-01 22:14:33 +00:00
|
|
|
onClick={() => {
|
|
|
|
const [url, queryString] = asPath.split('?');
|
|
|
|
const searchParams = new URLSearchParams(queryString);
|
|
|
|
searchParams.set(group, child.props.name);
|
|
|
|
replace(`${url}?${searchParams.toString()}`);
|
|
|
|
}}
|
2022-03-01 21:35:31 +00:00
|
|
|
>
|
|
|
|
{child.props.name}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
{/* the content */}
|
|
|
|
<div>
|
|
|
|
{Children.map(children, (child) => {
|
2022-03-01 22:14:33 +00:00
|
|
|
if (isValidElement(child) && query[group] === child.props.name) {
|
2022-03-01 21:35:31 +00:00
|
|
|
return <div>{child.props.children}</div>;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
interface GridTabProps {
|
|
|
|
children: ReactNode;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GridTab = ({ children }: GridTabProps) => {
|
|
|
|
return <div>{children}</div>;
|
|
|
|
};
|