2023-01-02 16:01:06 +00:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { useNavigate } from 'react-router-dom';
|
2023-05-18 11:22:54 +00:00
|
|
|
import { marketsWithDataProvider } from '@vegaprotocol/markets';
|
2023-05-08 01:01:07 +00:00
|
|
|
import { useDataProvider } from '@vegaprotocol/data-provider';
|
2022-11-08 07:23:38 +00:00
|
|
|
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
|
2022-12-22 14:24:20 +00:00
|
|
|
import { Links, Routes } from '../../pages/client-router';
|
2023-01-02 16:01:06 +00:00
|
|
|
import { useGlobalStore } from '../../stores';
|
2022-11-08 07:23:38 +00:00
|
|
|
|
|
|
|
export const Home = () => {
|
|
|
|
const navigate = useNavigate();
|
|
|
|
// The default market selected in the platform behind the overlay
|
2023-05-23 04:33:16 +00:00
|
|
|
// should be the oldest market that is currently trading in continuous mode(i.e. not in auction).
|
2022-11-08 07:23:38 +00:00
|
|
|
const { data, error, loading } = useDataProvider({
|
|
|
|
dataProvider: marketsWithDataProvider,
|
2023-03-09 10:03:50 +00:00
|
|
|
variables: undefined,
|
2022-11-08 07:23:38 +00:00
|
|
|
});
|
2023-01-02 16:01:06 +00:00
|
|
|
const update = useGlobalStore((store) => store.update);
|
|
|
|
const marketId = useGlobalStore((store) => store.marketId);
|
2022-11-08 07:23:38 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-01-02 16:01:06 +00:00
|
|
|
if (marketId) {
|
|
|
|
navigate(Links[Routes.MARKET](marketId), {
|
|
|
|
replace: true,
|
|
|
|
});
|
|
|
|
} else if (data) {
|
|
|
|
const marketDataId = data[0]?.id;
|
|
|
|
if (marketDataId) {
|
|
|
|
navigate(Links[Routes.MARKET](marketDataId), {
|
2022-12-22 14:24:20 +00:00
|
|
|
replace: true,
|
|
|
|
});
|
2022-11-18 17:08:48 +00:00
|
|
|
} else {
|
2023-05-23 05:02:55 +00:00
|
|
|
navigate(Links[Routes.MARKETS]());
|
2022-11-08 07:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-02 16:01:06 +00:00
|
|
|
}, [marketId, data, navigate, update]);
|
2022-11-08 07:23:38 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<AsyncRenderer data={data} loading={loading} error={error}>
|
|
|
|
{/* Render a loading and error state but we will redirect if markets are found */}
|
|
|
|
{null}
|
|
|
|
</AsyncRenderer>
|
|
|
|
);
|
|
|
|
};
|