vega-frontend-monorepo/apps/trading/pages/_app.page.tsx

141 lines
3.9 KiB
TypeScript
Raw Normal View History

import { AppProps } from 'next/app';
import Head from 'next/head';
2022-02-17 05:08:17 +00:00
import { Navbar } from '../components/navbar';
import {
useVegaWallet,
VegaConnectDialog,
VegaWalletProvider,
WALLET_CONFIG,
} from '@vegaprotocol/react-helpers';
import { Connectors } from '../lib/connectors';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { LocalStorage } from '@vegaprotocol/storage';
2022-02-28 23:43:36 +00:00
import { createClient } from '../lib/apollo-client';
import { ThemeSwitcher } from '@vegaprotocol/ui-toolkit';
2022-02-23 17:57:44 +00:00
import './styles.css';
import { ApolloProvider } from '@apollo/client';
2022-02-24 02:34:04 +00:00
import './styles.css';
2022-02-17 05:03:46 +00:00
function VegaTradingApp({ Component, pageProps }: AppProps) {
2022-02-28 23:43:36 +00:00
const client = useMemo(() => createClient(process.env['NX_VEGA_URL']), []);
const [dialogOpen, setDialogOpen] = useState(false);
const setConnectDialog = useCallback((isOpen?: boolean) => {
setDialogOpen((curr) => {
if (isOpen === undefined) return !curr;
return isOpen;
});
}, []);
useCallback(() => {
if (
localStorage.theme === 'dark' ||
(!('theme' in localStorage) &&
window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
}, []);
const setTheme = () => {
localStorage.theme = document.documentElement.classList.toggle('dark')
? 'dark'
: undefined;
};
return (
2022-02-28 23:43:36 +00:00
<ApolloProvider client={client}>
<VegaWalletProvider>
<Head>
<title>Welcome to trading!</title>
<link
rel="icon"
href="https://vega.xyz/favicon-32x32.png"
type="image/png"
/>
</Head>
<div className="h-full dark:bg-black dark:text-white-60 bg-white text-black-60">
<div className="flex items-center border-b-[7px] border-vega-yellow">
<Navbar />
2022-03-10 00:39:59 +00:00
<div className="flex items-center ml-auto mr-8">
<VegaWalletButton setConnectDialog={setConnectDialog} />
<ThemeSwitcher onToggle={setTheme} />
</div>
</div>
<main>
<Component {...pageProps} />
</main>
<VegaConnectDialog
connectors={Connectors}
dialogOpen={dialogOpen}
setDialogOpen={setDialogOpen}
/>
</div>
<VegaWalletManager />
</VegaWalletProvider>
2022-02-28 23:43:36 +00:00
</ApolloProvider>
);
}
interface VegaWalletButtonProps {
setConnectDialog: (isOpen: boolean) => void;
}
const VegaWalletButton = ({ setConnectDialog }: VegaWalletButtonProps) => {
const { disconnect, keypairs } = useVegaWallet();
const isConnected = keypairs !== null;
const handleClick = () => {
if (isConnected) {
disconnect();
} else {
setConnectDialog(true);
}
};
return (
2022-03-10 00:39:59 +00:00
<button onClick={handleClick} className="ml-auto inline-block p-8">
{isConnected ? 'Disconnect' : 'Connect Vega wallet'}
</button>
);
};
2022-02-17 05:03:46 +00:00
export default VegaTradingApp;
/**
* Wrapper to interact with the vega wallet out side of the provider itself.
*/
function VegaWalletManager() {
// Get keys from vega wallet immediately
useEagerConnect();
// Do other global stuff with vega wallet here
return null;
}
function useEagerConnect() {
const { connect } = useVegaWallet();
useEffect(() => {
const cfg = LocalStorage.getItem(WALLET_CONFIG);
const cfgObj = JSON.parse(cfg);
// No stored config, user has never connected or manually cleared storage
if (!cfgObj || !cfgObj.connector) {
return;
}
2022-02-24 00:10:31 +00:00
// Use the connector string in local storage to find the right connector to auto
// connect to
const connector = Connectors[cfgObj.connector];
// Developer hasn't provided this connector
if (!connector) {
throw new Error(`Connector ${cfgObj?.connector} not configured`);
}
connect(Connectors[cfgObj.connector]);
}, [connect]);
}