2022-03-09 13:05:42 +00:00
|
|
|
import { Callout, Button } from '@vegaprotocol/ui-toolkit';
|
2022-02-23 05:39:12 +00:00
|
|
|
import { rest } from '../lib/connectors';
|
2022-02-23 19:24:30 +00:00
|
|
|
import { useVegaWallet, VegaKeyExtended } from '@vegaprotocol/react-helpers';
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
import { Connectors, rest } from '../lib/connectors';
|
2022-02-23 05:39:12 +00:00
|
|
|
import { LocalStorage } from '@vegaprotocol/storage';
|
2022-02-23 17:57:44 +00:00
|
|
|
|
|
|
|
export function Index() {
|
2022-02-23 16:13:37 +00:00
|
|
|
// Get keys from vega wallet immediately
|
2022-02-23 05:39:12 +00:00
|
|
|
useEagerConnect();
|
2022-02-23 16:13:37 +00:00
|
|
|
|
2022-02-23 19:24:30 +00:00
|
|
|
const { publicKeys } = useVegaWallet();
|
|
|
|
const { publicKey, onSelect } = useCurrentVegaKey();
|
2022-02-23 16:13:37 +00:00
|
|
|
|
2022-02-11 13:56:28 +00:00
|
|
|
return (
|
2022-03-09 13:05:42 +00:00
|
|
|
<div className="m-24 ">
|
|
|
|
<Callout
|
|
|
|
intent="help"
|
|
|
|
title="This is what this thing does"
|
|
|
|
iconName="endorsed"
|
|
|
|
headingLevel={1}
|
|
|
|
>
|
|
|
|
<div className="flex flex-col">
|
|
|
|
<div>With a longer explaination</div>
|
|
|
|
<Button className="block mt-8" variant="secondary">
|
|
|
|
Action
|
|
|
|
</Button>
|
|
|
|
</div>
|
2022-02-24 02:06:52 +00:00
|
|
|
</Callout>
|
2022-02-23 05:05:39 +00:00
|
|
|
<h1>Vega wallet</h1>
|
|
|
|
{publicKey && <p>Current: {publicKey.pub}</p>}
|
|
|
|
{publicKeys?.length && (
|
|
|
|
<select
|
|
|
|
name="change-key"
|
|
|
|
value={publicKey?.pub}
|
2022-02-23 19:24:30 +00:00
|
|
|
onChange={(e) => onSelect(e.target.value)}
|
2022-02-23 05:05:39 +00:00
|
|
|
>
|
|
|
|
{publicKeys.map((pk) => (
|
|
|
|
<option key={pk.pub} value={pk.pub}>
|
|
|
|
{pk.name} ({pk.pub})
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
)}
|
2022-02-23 16:13:37 +00:00
|
|
|
<hr />
|
2022-02-23 05:05:39 +00:00
|
|
|
<h2>Public keys</h2>
|
|
|
|
<pre>{JSON.stringify(publicKeys, null, 2)}</pre>
|
2022-02-11 13:56:28 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2022-02-23 17:57:44 +00:00
|
|
|
|
|
|
|
export default Index;
|
2022-02-23 05:39:12 +00:00
|
|
|
|
|
|
|
function useEagerConnect() {
|
|
|
|
const { connect } = useVegaWallet();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-02-23 19:24:30 +00:00
|
|
|
const cfg = LocalStorage.getItem('vega_wallet');
|
|
|
|
const cfgObj = JSON.parse(cfg);
|
|
|
|
|
|
|
|
// No stored config, user has never connected or manually cleared storage
|
|
|
|
if (!cfgObj || !cfgObj.connector) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const connector = Connectors[cfgObj.connector];
|
|
|
|
|
|
|
|
// Developer hasn't provided this connector
|
|
|
|
if (!connector) {
|
|
|
|
throw new Error(`Connector ${cfgObj?.connector} not configured`);
|
2022-02-23 05:39:12 +00:00
|
|
|
}
|
2022-02-23 19:24:30 +00:00
|
|
|
|
|
|
|
connect(Connectors[cfgObj.connector]);
|
2022-02-23 05:39:12 +00:00
|
|
|
}, [connect]);
|
|
|
|
}
|
2022-02-23 19:24:30 +00:00
|
|
|
|
|
|
|
function useCurrentVegaKey(): {
|
|
|
|
publicKey: VegaKeyExtended | null;
|
|
|
|
onSelect: (pk: string) => void;
|
|
|
|
} {
|
|
|
|
const { publicKeys } = useVegaWallet();
|
|
|
|
const [pk, setPk] = useState<string | null>(() =>
|
|
|
|
LocalStorage.getItem('vega_selected_publickey')
|
|
|
|
);
|
|
|
|
|
|
|
|
const publicKey = useMemo(() => {
|
|
|
|
if (!publicKeys?.length) return null;
|
|
|
|
|
|
|
|
const found = publicKeys.find((x) => x.pub === pk);
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}, [pk, publicKeys]);
|
|
|
|
|
|
|
|
// on public key change set to localStorage
|
|
|
|
useEffect(() => {
|
|
|
|
LocalStorage.setItem('vega_selected_publickey', pk);
|
|
|
|
}, [pk]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
publicKey,
|
|
|
|
onSelect: setPk,
|
|
|
|
};
|
|
|
|
}
|