vega-frontend-monorepo/apps/token/src/components/eth-wallet/eth-wallet.tsx
Sam Keen 2302ef4378
feat(#447): 447 - UI toolkit and theme updates
* feat: 447 Refactored 'progress' intent to be 'prompt' as now white. Added yellow 'selected' intent

* feat: 447 Colour consolidation

* feat: 447 Colour consolidation extra renaming

* feat: 447 Fixing specified red colours

* feat: 447 Removed unused darker red

* feat: 447 Documenting additional colours in storybook

* feat: 447 Buttons updated (except 'accent', which will probably get removed when navs built)

* feat: 447 Text inputs updated

* feat:frontend-monorepo-447: Trading nav

* feat:frontend-monorepo-447: Updated toggle button colours

* feat:frontend-monorepo-447: Custom checkboxes

* feat:frontend-monorepo-447: Tweaks to radio buttons

* feat:frontend-monorepo-447: Input dates get dark color scheme in dark mode

* feat:frontend-monorepo-447: Dropdown updates

* feat:frontend-monorepo-447: Icon menu

* feat:frontend-monorepo-447: Focus visual styles moved to focus-visible for radios and toggle

* feat:frontend-monorepo-447: Tweak to focus styles for text input and textarea

* feat:frontend-monorepo-447: Labeled input

* feat:frontend-monorepo-447: Labeled input description red when in error

* feat:frontend-monorepo-447: Tooltip visual update

* feat:frontend-monorepo-447: Added disabled state to checkbox

* feat:frontend-monorepo-447: Custom select with radix

* feat:frontend-monorepo-447: Reverted back to native Select for a11y concerns

* feat:frontend-monorepo-447: Added visual cue for dropdown items when multiple can be selected

* feat:frontend-monorepo-447: Removed shadow from buttons in Explorer where it looked wrong

* feat:frontend-monorepo-447: Added box shadow classes into tailwind theme

* feat:frontend-monorepo-447: Colour primitives documentation updated

* feat:frontend-monorepo-447: Cleaning up box shadow use further

* feat:frontend-monorepo-447: Intents util updated

* feat:frontend-monorepo-447: Dialog component updated

* feat:frontend-monorepo-447: Callout component updated

* feat:frontend-monorepo-447: Adjusted apps to handle toolkit changes

* feat:frontend-monorepo-447: Moved tabs to ui-toolkit and styled

* feat:frontend-monorepo-447: Fixed ui-toolkit tests

* feat:frontend-monorepo-447: Token eth wallet made dark to support new buttons

* feat:frontend-monorepo-447: Ran prettier

* frontend-monorepo-447: Simplified button class functions and exported for use on other elements

* frontend-monorepo-447: Used newly exported button classes on Link elements in eth-wallet

* frontend-monorepo-447: Moved trading nav from ui-toolkit to trading app

* frontend-monorepo-447: Simplified intents and updated stories

* frontend-monorepo-447: Using classnames in requested spot

* frontend-monorepo-447: Removed unnecessary 'asChild' prop on dropdown triggers

* frontend-monorepo-447: Made use of the XPrimitive Radix naming convention

* frontend-monorepo-447: Simplified types in 'getButtonClasses'

* frontend-monorepo-447: Added 'asChild' to dropdown trigger to avoid nested buttons

* frontend-monorepo-447: Moved input label and description into Formgroup component. Refactored based on tweaked structure

* frontend-monorepo-447: Externally linked input label

* frontend-monorepo-447: Adding correct text colours to Intent.None backgrounds

* frontend-monorepo-447: Improved intent function name

* frontend-monorepo-447: Removed new navbar until implementation ticket is picked up

* frontend-monorepo-447: using testing-library/user-event for tab click unit tests

* frontend-monorepo-447: Removed unused button import

* frontend-monorepo-447: Little extra use of classnames in form-group.tsx

* feat: make navbar pink for light mode

* fix: problem with theme not switching when dependent in js on theme value

* fix: bg of row hover

* fix: dont use vega pink for sell red

* fix: type error in generate orders func

* fix: lint

Co-authored-by: Matthew Russell <mattrussell36@gmail.com>
2022-06-23 20:16:01 -07:00

251 lines
7.6 KiB
TypeScript

import { useWeb3React } from '@web3-react/core';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { Link } from 'react-router-dom';
import { getButtonClasses, Button } from '@vegaprotocol/ui-toolkit';
import {
AppStateActionType,
useAppState,
} from '../../contexts/app-state/app-state-context';
import { usePendingTransactions } from '../../hooks/use-pending-transactions';
import vegaVesting from '../../images/vega_vesting.png';
import vegaWhite from '../../images/vega_white.png';
import { BigNumber } from '../../lib/bignumber';
import { truncateMiddle } from '../../lib/truncate-middle';
import { Routes } from '../../routes/router-config';
import { LockedProgress } from '../locked-progress';
import {
WalletCard,
WalletCardActions,
WalletCardAsset,
WalletCardContent,
WalletCardHeader,
WalletCardRow,
} from '../wallet-card';
import { Loader } from '@vegaprotocol/ui-toolkit';
import { theme } from '@vegaprotocol/tailwindcss-config';
const Colors = theme.colors;
const removeLeadingAddressSymbol = (key: string) => {
if (key && key.length > 2 && key.slice(0, 2) === '0x') {
return truncateMiddle(key.substring(2));
}
return truncateMiddle(key);
};
const AssociatedAmounts = ({
associations,
notAssociated,
}: {
associations: { [key: string]: BigNumber };
notAssociated: BigNumber;
}) => {
const { t } = useTranslation();
const vestingAssociationByVegaKey = React.useMemo(
() =>
Object.entries(associations).filter(([, amount]) =>
amount.isGreaterThan(0)
),
[associations]
);
const associationAmounts = React.useMemo(() => {
const totals = vestingAssociationByVegaKey.map(([, amount]) => amount);
const associated = BigNumber.sum.apply(null, [new BigNumber(0), ...totals]);
return {
total: associated.plus(notAssociated),
associated,
notAssociated,
};
}, [notAssociated, vestingAssociationByVegaKey]);
return (
<>
<LockedProgress
locked={associationAmounts.associated}
unlocked={associationAmounts.notAssociated}
total={associationAmounts.total}
leftLabel={t('associated')}
rightLabel={t('notAssociated')}
leftColor={Colors.white.DEFAULT}
rightColor={Colors.black.DEFAULT}
light={false}
/>
{vestingAssociationByVegaKey.length ? (
<>
<hr style={{ borderStyle: 'dashed', color: Colors.text }} />
<WalletCardRow label="Associated with Vega keys" bold={true} />
{vestingAssociationByVegaKey.map(([key, amount]) => {
return (
<WalletCardRow
key={key}
label={removeLeadingAddressSymbol(key)}
value={amount}
/>
);
})}
</>
) : null}
</>
);
};
const ConnectedKey = () => {
const { t } = useTranslation();
const { appState } = useAppState();
const { walletBalance, totalLockedBalance, totalVestedBalance } = appState;
const totalInVestingContract = React.useMemo(() => {
return totalLockedBalance.plus(totalVestedBalance);
}, [totalLockedBalance, totalVestedBalance]);
const notAssociatedInContract = React.useMemo(() => {
const totals = Object.values(
appState.associationBreakdown.vestingAssociations
);
const associated = BigNumber.sum.apply(null, [new BigNumber(0), ...totals]);
return totalInVestingContract.minus(associated);
}, [
appState.associationBreakdown.vestingAssociations,
totalInVestingContract,
]);
const walletWithAssociations = React.useMemo(() => {
const totals = Object.values(
appState.associationBreakdown.stakingAssociations
);
const associated = BigNumber.sum.apply(null, [new BigNumber(0), ...totals]);
return walletBalance.plus(associated);
}, [appState.associationBreakdown.stakingAssociations, walletBalance]);
return (
<>
{totalVestedBalance.plus(totalLockedBalance).isEqualTo(0) ? null : (
<>
<WalletCardAsset
image={vegaVesting}
decimals={appState.decimals}
name="VEGA"
symbol="In vesting contract"
balance={totalInVestingContract}
dark={true}
/>
<LockedProgress
locked={totalLockedBalance}
unlocked={totalVestedBalance}
total={totalVestedBalance.plus(totalLockedBalance)}
leftLabel={t('Locked')}
rightLabel={t('Unlocked')}
light={false}
/>
</>
)}
{!Object.keys(appState.associationBreakdown.vestingAssociations)
.length ? null : (
<AssociatedAmounts
associations={appState.associationBreakdown.vestingAssociations}
notAssociated={notAssociatedInContract}
/>
)}
<WalletCardAsset
image={vegaWhite}
decimals={appState.decimals}
name="VEGA"
symbol="In Wallet"
balance={walletWithAssociations}
dark={true}
/>
{!Object.keys(
appState.associationBreakdown.stakingAssociations
) ? null : (
<AssociatedAmounts
associations={appState.associationBreakdown.stakingAssociations}
notAssociated={walletBalance}
/>
)}
<WalletCardActions>
<Link
className={getButtonClasses('flex-1 mr-4', 'secondary')}
to={`${Routes.STAKING}/associate`}
>
{t('associate')}
</Link>
<Link
className={getButtonClasses('flex-1 ml-4', 'secondary')}
to={`${Routes.STAKING}/disassociate`}
>
{t('disassociate')}
</Link>
</WalletCardActions>
</>
);
};
export const EthWallet = () => {
const { t } = useTranslation();
const { appDispatch } = useAppState();
const { account, connector } = useWeb3React();
const pendingTxs = usePendingTransactions();
return (
<WalletCard dark={true}>
<WalletCardHeader>
<h1 className="text-h3 uppercase">{t('ethereumKey')}</h1>
{account && (
<div className="px-4 text-right">
<div className="font-mono">{truncateMiddle(account)}</div>
{pendingTxs && (
<div>
<button
className="flex items-center gap-4 p-4 border whitespace-nowrap"
data-testid="pending-transactions-btn"
onClick={() =>
appDispatch({
type: AppStateActionType.SET_TRANSACTION_OVERLAY,
isOpen: true,
})
}
>
<Loader size="small" forceTheme="dark" />
{t('pendingTransactions')}
</button>
</div>
)}
</div>
)}
</WalletCardHeader>
<WalletCardContent>
{account ? (
<ConnectedKey />
) : (
<Button
variant={'secondary'}
className="w-full px-28 border h-28"
onClick={() =>
appDispatch({
type: AppStateActionType.SET_ETH_WALLET_OVERLAY,
isOpen: true,
})
}
data-test-id="connect-to-eth-wallet-button"
>
{t('connectEthWalletToAssociate')}
</Button>
)}
{account && (
<WalletCardActions>
<button
className="mt-4 underline"
onClick={() => connector.deactivate()}
>
{t('disconnect')}
</button>
</WalletCardActions>
)}
</WalletCardContent>
</WalletCard>
);
};