2022-05-23 12:21:54 +00:00
|
|
|
import { gql, useQuery } from '@apollo/client';
|
2022-06-06 16:19:56 +00:00
|
|
|
import { LocalStorage } from '@vegaprotocol/react-helpers';
|
2022-05-23 12:21:54 +00:00
|
|
|
import { MarketTradingMode } from '@vegaprotocol/types';
|
|
|
|
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import sortBy from 'lodash/sortBy';
|
2022-06-01 14:21:36 +00:00
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { useGlobalStore } from '../stores';
|
2022-05-23 12:21:54 +00:00
|
|
|
import type { MarketsLanding } from './__generated__/MarketsLanding';
|
|
|
|
|
|
|
|
const MARKETS_QUERY = gql`
|
|
|
|
query MarketsLanding {
|
|
|
|
markets {
|
|
|
|
id
|
|
|
|
tradingMode
|
|
|
|
marketTimestamps {
|
|
|
|
open
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const marketList = ({ markets }: MarketsLanding) =>
|
|
|
|
sortBy(
|
|
|
|
markets?.filter(
|
|
|
|
({ marketTimestamps, tradingMode }) =>
|
|
|
|
marketTimestamps.open && tradingMode === MarketTradingMode.Continuous
|
|
|
|
) || [],
|
|
|
|
'marketTimestamps.open',
|
|
|
|
'id'
|
|
|
|
);
|
2022-02-23 17:57:44 +00:00
|
|
|
|
|
|
|
export function Index() {
|
2022-06-01 14:21:36 +00:00
|
|
|
const { replace } = useRouter();
|
2022-05-23 12:21:54 +00:00
|
|
|
// The default market selected in the platform behind the overlay
|
|
|
|
// should be the oldest market that is currently trading in continuous mode(i.e. not in auction).
|
|
|
|
const { data, error, loading } = useQuery<MarketsLanding>(MARKETS_QUERY);
|
2022-06-01 14:21:36 +00:00
|
|
|
const setLandingDialog = useGlobalStore((state) => state.setLandingDialog);
|
2022-06-06 16:19:56 +00:00
|
|
|
const lastSelectedMarketId = LocalStorage.getItem('marketId');
|
2022-06-01 14:21:36 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (data) {
|
2022-06-06 16:19:56 +00:00
|
|
|
const marketId = lastSelectedMarketId
|
|
|
|
? lastSelectedMarketId
|
|
|
|
: marketList(data)[0]?.id;
|
2022-06-01 14:21:36 +00:00
|
|
|
|
|
|
|
// If a default market is found, go to it with the landing dialog open
|
|
|
|
if (marketId) {
|
|
|
|
setLandingDialog(true);
|
|
|
|
replace(`/markets/${marketId}`);
|
|
|
|
}
|
|
|
|
// Fallback to the markets list page
|
|
|
|
else {
|
|
|
|
replace('/markets');
|
|
|
|
}
|
|
|
|
}
|
2022-06-06 16:19:56 +00:00
|
|
|
}, [data, lastSelectedMarketId, replace, setLandingDialog]);
|
2022-06-01 14:21:36 +00:00
|
|
|
|
2022-02-11 13:56:28 +00:00
|
|
|
return (
|
2022-06-01 14:21:36 +00:00
|
|
|
<AsyncRenderer data={data} loading={loading} error={error}>
|
|
|
|
{/* Render a loading and error state but we will redirect if markets are found */}
|
|
|
|
{null}
|
|
|
|
</AsyncRenderer>
|
2022-02-11 13:56:28 +00:00
|
|
|
);
|
|
|
|
}
|
2022-02-23 17:57:44 +00:00
|
|
|
|
2022-04-12 11:04:26 +00:00
|
|
|
Index.getInitialProps = () => ({
|
|
|
|
page: 'home',
|
|
|
|
});
|
|
|
|
|
2022-02-23 17:57:44 +00:00
|
|
|
export default Index;
|