2023-08-24 08:03:56 +00:00
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
2022-09-02 14:53:38 +00:00
|
|
|
import Head from 'next/head';
|
2022-11-08 07:23:38 +00:00
|
|
|
import type { AppProps } from 'next/app';
|
2023-02-28 18:56:29 +00:00
|
|
|
import { t } from '@vegaprotocol/i18n';
|
2022-12-07 09:24:40 +00:00
|
|
|
import {
|
|
|
|
useEagerConnect as useVegaEagerConnect,
|
2022-12-21 09:29:32 +00:00
|
|
|
useVegaTransactionManager,
|
|
|
|
useVegaTransactionUpdater,
|
2023-01-19 11:16:21 +00:00
|
|
|
useVegaWallet,
|
2022-12-07 09:24:40 +00:00
|
|
|
} from '@vegaprotocol/wallet';
|
2022-12-21 09:29:32 +00:00
|
|
|
import {
|
|
|
|
useEagerConnect as useEthereumEagerConnect,
|
|
|
|
useEthTransactionManager,
|
|
|
|
useEthTransactionUpdater,
|
|
|
|
useEthWithdrawApprovalsManager,
|
|
|
|
} from '@vegaprotocol/web3';
|
2022-09-23 14:29:35 +00:00
|
|
|
import {
|
|
|
|
envTriggerMapping,
|
2023-07-31 16:08:55 +00:00
|
|
|
Networks,
|
2023-02-16 03:52:54 +00:00
|
|
|
NodeSwitcherDialog,
|
2022-09-23 14:29:35 +00:00
|
|
|
useEnvironment,
|
2023-02-16 03:52:54 +00:00
|
|
|
useInitializeEnv,
|
2023-05-09 19:58:09 +00:00
|
|
|
useNodeSwitcherStore,
|
2022-09-23 14:29:35 +00:00
|
|
|
} from '@vegaprotocol/environment';
|
2022-03-14 13:18:11 +00:00
|
|
|
import './styles.css';
|
2023-08-24 08:03:56 +00:00
|
|
|
import { useGlobalStore, usePageTitleStore } from '../stores';
|
2022-10-14 15:42:53 +00:00
|
|
|
import DialogsContainer from './dialogs-container';
|
2022-12-21 09:29:32 +00:00
|
|
|
import ToastsManager from './toasts-manager';
|
2023-07-31 16:08:55 +00:00
|
|
|
import {
|
|
|
|
HashRouter,
|
|
|
|
useLocation,
|
|
|
|
Route,
|
|
|
|
Routes,
|
|
|
|
useSearchParams,
|
|
|
|
} from 'react-router-dom';
|
2022-12-07 09:24:40 +00:00
|
|
|
import { Connectors } from '../lib/vega-connectors';
|
2023-02-24 14:38:34 +00:00
|
|
|
import { AppLoader, DynamicLoader } from '../components/app-loader';
|
2023-05-08 01:01:07 +00:00
|
|
|
import { useDataProvider } from '@vegaprotocol/data-provider';
|
2023-05-31 15:38:49 +00:00
|
|
|
import { activeOrdersProvider } from '@vegaprotocol/orders';
|
2023-04-26 15:17:23 +00:00
|
|
|
import { useTelemetryApproval } from '../lib/hooks/use-telemetry-approval';
|
2023-09-25 21:27:01 +00:00
|
|
|
import { AnnouncementBanner } from '../components/banner';
|
2023-07-24 08:37:18 +00:00
|
|
|
import { Navbar } from '../components/navbar';
|
|
|
|
import classNames from 'classnames';
|
2023-05-04 07:51:24 +00:00
|
|
|
import {
|
|
|
|
ProtocolUpgradeCountdownMode,
|
2023-08-16 14:50:38 +00:00
|
|
|
ProtocolUpgradeInProgressNotification,
|
2023-05-04 07:51:24 +00:00
|
|
|
ProtocolUpgradeProposalNotification,
|
|
|
|
} from '@vegaprotocol/proposals';
|
2023-07-24 08:37:18 +00:00
|
|
|
import { ViewingBanner } from '../components/viewing-banner';
|
2023-07-31 16:08:55 +00:00
|
|
|
import { NavHeader } from '../components/navbar/nav-header';
|
|
|
|
import { Routes as AppRoutes } from './client-router';
|
2023-09-14 19:10:33 +00:00
|
|
|
import { Telemetry } from '../components/telemetry';
|
2022-09-23 14:29:35 +00:00
|
|
|
|
|
|
|
const DEFAULT_TITLE = t('Welcome to Vega trading!');
|
2022-03-14 13:18:11 +00:00
|
|
|
|
2022-10-14 15:42:53 +00:00
|
|
|
const Title = () => {
|
|
|
|
const { pageTitle } = usePageTitleStore((store) => ({
|
2022-09-23 14:29:35 +00:00
|
|
|
pageTitle: store.pageTitle,
|
2022-09-05 14:25:33 +00:00
|
|
|
}));
|
2022-06-10 09:15:38 +00:00
|
|
|
|
2022-09-23 14:29:35 +00:00
|
|
|
const { VEGA_ENV } = useEnvironment();
|
|
|
|
const networkName = envTriggerMapping[VEGA_ENV];
|
|
|
|
|
|
|
|
const title = useMemo(() => {
|
|
|
|
if (!pageTitle) return DEFAULT_TITLE;
|
|
|
|
if (networkName) return `${pageTitle} [${networkName}]`;
|
|
|
|
return pageTitle;
|
|
|
|
}, [pageTitle, networkName]);
|
2022-11-08 07:23:38 +00:00
|
|
|
|
2022-10-14 15:42:53 +00:00
|
|
|
return (
|
|
|
|
<Head>
|
|
|
|
<title>{title}</title>
|
|
|
|
</Head>
|
|
|
|
);
|
|
|
|
};
|
2022-09-23 14:29:35 +00:00
|
|
|
|
2023-03-31 13:23:44 +00:00
|
|
|
const InitializeHandlers = () => {
|
2022-12-21 09:29:32 +00:00
|
|
|
useVegaTransactionManager();
|
|
|
|
useVegaTransactionUpdater();
|
|
|
|
useEthTransactionManager();
|
|
|
|
useEthTransactionUpdater();
|
|
|
|
useEthWithdrawApprovalsManager();
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-11-08 07:23:38 +00:00
|
|
|
function AppBody({ Component }: AppProps) {
|
|
|
|
const location = useLocation();
|
2023-07-31 16:08:55 +00:00
|
|
|
const { VEGA_ENV } = useEnvironment();
|
2023-02-03 10:34:32 +00:00
|
|
|
const gridClasses = classNames(
|
|
|
|
'h-full relative z-0 grid',
|
2023-05-16 16:57:36 +00:00
|
|
|
'grid-rows-[repeat(3,min-content),minmax(0,1fr)]'
|
2023-02-03 10:34:32 +00:00
|
|
|
);
|
2022-06-10 09:15:38 +00:00
|
|
|
return (
|
2023-07-24 08:37:18 +00:00
|
|
|
<div className="h-full overflow-hidden">
|
2022-12-02 23:24:15 +00:00
|
|
|
<Head>
|
|
|
|
{/* Cannot use meta tags in _document.page.tsx see https://nextjs.org/docs/messages/no-document-viewport-meta */}
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
|
|
</Head>
|
2022-10-14 15:42:53 +00:00
|
|
|
<Title />
|
2023-02-16 03:52:54 +00:00
|
|
|
<div className={gridClasses}>
|
2023-04-21 16:31:52 +00:00
|
|
|
<AnnouncementBanner />
|
2023-07-31 16:08:55 +00:00
|
|
|
<Navbar theme={VEGA_ENV === Networks.TESTNET ? 'yellow' : 'system'}>
|
|
|
|
<Routes>
|
|
|
|
<Route
|
|
|
|
path={AppRoutes.MARKETS}
|
|
|
|
// render nothing for markets/all, otherwise markets/:marketId will match with markets/all
|
|
|
|
element={null}
|
|
|
|
/>
|
|
|
|
<Route path={AppRoutes.MARKET} element={<NavHeader />} />
|
|
|
|
</Routes>
|
|
|
|
</Navbar>
|
2023-05-04 07:51:24 +00:00
|
|
|
<div data-testid="banners">
|
|
|
|
<ProtocolUpgradeProposalNotification
|
|
|
|
mode={ProtocolUpgradeCountdownMode.IN_ESTIMATED_TIME_REMAINING}
|
|
|
|
/>
|
2023-08-16 14:50:38 +00:00
|
|
|
<ProtocolUpgradeInProgressNotification />
|
2023-05-04 07:51:24 +00:00
|
|
|
<ViewingBanner />
|
|
|
|
</div>
|
2023-07-24 08:37:18 +00:00
|
|
|
<div data-testid={`pathname-${location.pathname}`}>
|
2023-02-16 03:52:54 +00:00
|
|
|
<Component />
|
2023-07-24 08:37:18 +00:00
|
|
|
</div>
|
2023-02-16 03:52:54 +00:00
|
|
|
</div>
|
|
|
|
<DialogsContainer />
|
|
|
|
<ToastsManager />
|
2023-03-31 13:23:44 +00:00
|
|
|
<InitializeHandlers />
|
2023-02-16 03:52:54 +00:00
|
|
|
<MaybeConnectEagerly />
|
2023-04-18 10:49:42 +00:00
|
|
|
<PartyData />
|
2023-09-14 19:10:33 +00:00
|
|
|
<Telemetry />
|
2022-12-27 15:46:44 +00:00
|
|
|
</div>
|
2022-06-10 09:15:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function VegaTradingApp(props: AppProps) {
|
2023-02-16 03:52:54 +00:00
|
|
|
const status = useEnvironment((store) => store.status);
|
2023-05-09 19:58:09 +00:00
|
|
|
const [nodeSwitcherOpen, setNodeSwitcher] = useNodeSwitcherStore((store) => [
|
|
|
|
store.dialogOpen,
|
|
|
|
store.setDialogOpen,
|
|
|
|
]);
|
2022-11-08 07:23:38 +00:00
|
|
|
|
2023-02-16 03:52:54 +00:00
|
|
|
useInitializeEnv();
|
2022-11-08 07:23:38 +00:00
|
|
|
|
2023-02-16 03:52:54 +00:00
|
|
|
// Prevent HashRouter from being server side rendered as it
|
|
|
|
// relies on presence of document object
|
|
|
|
if (status === 'default') {
|
2023-02-24 14:38:34 +00:00
|
|
|
return <DynamicLoader />;
|
2022-12-27 15:46:44 +00:00
|
|
|
}
|
2022-11-08 07:23:38 +00:00
|
|
|
|
2022-02-11 13:56:28 +00:00
|
|
|
return (
|
2022-11-08 07:23:38 +00:00
|
|
|
<HashRouter>
|
2023-02-16 03:52:54 +00:00
|
|
|
<AppLoader>
|
2022-12-07 09:24:40 +00:00
|
|
|
<AppBody {...props} />
|
2023-02-16 03:52:54 +00:00
|
|
|
</AppLoader>
|
|
|
|
<NodeSwitcherDialog open={nodeSwitcherOpen} setOpen={setNodeSwitcher} />
|
2022-11-08 07:23:38 +00:00
|
|
|
</HashRouter>
|
2022-02-11 13:56:28 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-17 05:03:46 +00:00
|
|
|
export default VegaTradingApp;
|
2022-12-07 09:24:40 +00:00
|
|
|
|
2023-04-18 10:49:42 +00:00
|
|
|
const PartyData = () => {
|
|
|
|
const { pubKey } = useVegaWallet();
|
|
|
|
const variables = { partyId: pubKey || '' };
|
|
|
|
const skip = !pubKey;
|
|
|
|
useDataProvider({
|
|
|
|
dataProvider: activeOrdersProvider,
|
|
|
|
variables,
|
|
|
|
skip,
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-12-07 09:24:40 +00:00
|
|
|
const MaybeConnectEagerly = () => {
|
2023-06-27 06:40:42 +00:00
|
|
|
const { VEGA_ENV, SENTRY_DSN } = useEnvironment();
|
2023-08-24 08:03:56 +00:00
|
|
|
const update = useGlobalStore((store) => store.update);
|
|
|
|
const eagerConnecting = useVegaEagerConnect(Connectors);
|
2023-04-26 15:17:23 +00:00
|
|
|
const [isTelemetryApproved] = useTelemetryApproval();
|
|
|
|
useEthereumEagerConnect(
|
2023-06-27 06:40:42 +00:00
|
|
|
isTelemetryApproved ? { dsn: SENTRY_DSN, env: VEGA_ENV } : {}
|
2023-04-26 15:17:23 +00:00
|
|
|
);
|
2023-01-19 11:16:21 +00:00
|
|
|
|
|
|
|
const { pubKey, connect } = useVegaWallet();
|
|
|
|
const [searchParams] = useSearchParams();
|
|
|
|
const [query] = useState(searchParams.get('address'));
|
|
|
|
if (query && !pubKey) {
|
|
|
|
connect(Connectors['view']);
|
|
|
|
}
|
2023-08-24 08:03:56 +00:00
|
|
|
useEffect(() => {
|
|
|
|
update({ eagerConnecting });
|
|
|
|
}, [update, eagerConnecting]);
|
2022-12-07 09:24:40 +00:00
|
|
|
return null;
|
|
|
|
};
|