2022-10-03 17:28:32 +00:00
|
|
|
import { marketsWithDataProvider } from '@vegaprotocol/market-list';
|
|
|
|
import {
|
|
|
|
addDecimalsFormatNumber,
|
|
|
|
titlefy,
|
|
|
|
useDataProvider,
|
|
|
|
} from '@vegaprotocol/react-helpers';
|
2022-05-23 12:21:54 +00:00
|
|
|
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
|
2022-06-01 14:21:36 +00:00
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { useGlobalStore } from '../stores';
|
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
|
2022-10-03 17:28:32 +00:00
|
|
|
// should be the oldest market that is currently trading in us mode(i.e. not in auction).
|
|
|
|
const { data, error, loading } = useDataProvider({
|
|
|
|
dataProvider: marketsWithDataProvider,
|
|
|
|
});
|
2022-09-05 14:25:33 +00:00
|
|
|
const { riskNoticeDialog, update } = useGlobalStore((store) => ({
|
|
|
|
riskNoticeDialog: store.riskNoticeDialog,
|
|
|
|
update: store.update,
|
|
|
|
}));
|
2022-06-01 14:21:36 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-09-05 14:25:33 +00:00
|
|
|
update({ landingDialog: true });
|
2022-09-02 14:53:38 +00:00
|
|
|
|
2022-06-01 14:21:36 +00:00
|
|
|
if (data) {
|
2022-10-03 17:28:32 +00:00
|
|
|
const marketId = data[0]?.id;
|
|
|
|
const marketName = data[0]?.tradableInstrument.instrument.name;
|
|
|
|
const marketPrice = data[0]?.data?.markPrice
|
2022-09-23 14:29:35 +00:00
|
|
|
? addDecimalsFormatNumber(
|
2022-10-03 17:28:32 +00:00
|
|
|
data[0]?.data?.markPrice,
|
|
|
|
data[0]?.decimalPlaces
|
2022-09-23 14:29:35 +00:00
|
|
|
)
|
|
|
|
: null;
|
|
|
|
const pageTitle = titlefy([marketName, marketPrice]);
|
2022-06-01 14:21:36 +00:00
|
|
|
|
|
|
|
if (marketId) {
|
|
|
|
replace(`/markets/${marketId}`);
|
2022-09-23 14:29:35 +00:00
|
|
|
update({ marketId, pageTitle });
|
2022-06-01 14:21:36 +00:00
|
|
|
}
|
|
|
|
// Fallback to the markets list page
|
|
|
|
else {
|
|
|
|
replace('/markets');
|
|
|
|
}
|
|
|
|
}
|
2022-09-05 14:25:33 +00:00
|
|
|
}, [data, replace, riskNoticeDialog, update]);
|
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;
|