vega-frontend-monorepo/apps/token/src/components/token-input/token-input.tsx
botond 836c232a1c
Feat/675: Git info (#683)
* feat: add network-switcher lib

* feat: add env variables for some deployed app urls

* feat: add network processing to environment hoook

* refactor: network handling

* refactor: remove dialog from provider and add env setter

* feat: add network switcher dialog to the trading app

* refactor: add network redirect to dialog connect callback

* fix: lint

* feat: add network configuration files to static app

* feat: update environments to use config file instead of static node url

* refactor: split out network switcher utils

* refactor: split up environment hook

* fix: jsonify env variable for possible networks

* fix: add formatter file

* feat: add network loader component

* feat: add network loader to the trading app

* fix: assign correct global state to network swicther

* feat: add status modal

* feat: add network-switcher lib

* feat: add env variables for some deployed app urls

* feat: add network processing to environment hoook

* refactor: network handling

* refactor: remove dialog from provider and add env setter

* feat: add network switcher dialog to the trading app

* refactor: add network redirect to dialog connect callback

* fix: lint

* fix: jsonify env variable for possible networks

* fix: add formatter file

* fix: assign correct global state to network swicther

* fix: failing tests from UI changes

* feat: add environment validation

* feat: add runtime validation for network configs

* fix: readd node urls to envs to avoid breaking the apps for now

* chore: rename network swicther lib to environmnet

* fix: lint

* feat: add tests for config hook

* feat: add environment hook tests

* fix: lint

* fix: lint

* feat: add environment hook tests

* feat: add storage tests

* fix: formet

* feat: improve loading states

* fix: format

* fix: use router instead of window location

* fix: rearrange network loader props and components

* fix: remove FC type

* fix: env validation

* fix: untangle returns in network loader

* fix: add teardown for env and localstorage

* fix: add custom to env networks

* fix: lint

* fix: format

* fix: lint

* fix: remove env provider from simple trading app

* fix: remove failing promise hacks

* fix: some leftover format files

* fix: remove network switcher from tsconf

* fix: move Networks to libs/environment

* fix: add defaults for ether env vars

* feat: add tests for default ether env vars

* fix: remove chain id env var from web3 container

* fix: remove chain id from the environment

* fix: format

* fix: lint token

* fix: lint env

* fix: add comment to callout hack

* fix: lint token again

* fix: remove skip

* fix: move addresses to token app

* fix: improve schema validation errors and fix token app

* fix: lint

* fix: format

* fix: format

* fix: add network loaders to apps

* fix: format

* fix: remove logs

* fix: cypress process errors

* fix: change network loader hierarchy in token

* fix: remove stray console.log

* fix: revert test changes in simple trading app

* fix: prefix env vars with NX

Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>

* fix: improve schema validation errors and fix token app

* fix: format

* fix: disable lint rules for catch block any types

* fix: format again

* fix: remove redundant process.platform injections

* fix: format

* feat: add explorer footer

* fix: format

* feat: add custom executor for next

* feat: adjust footer links and add custom executors

* fix: lint and format

* fix: format again

* fix: add explicit github feedback url

* fix: create footer lib

* feat: add footer to token app

* fix: add compiled file to ignore

* fix: make lozenge styling more flexible when no variant specified

* fix: remove schema duplication for third party executors

* fix: add missing intent variant for the lozenge

* fix: move commit hash in footer down two positions

* fix: revert breaking schema removals

* fix: format

* chore: rename footer to network-info

* fix: format

Co-authored-by: Joe <joe@vega.xyz>
Co-authored-by: Matthew Russell <mattrussell36@gmail.com>
Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>
2022-07-06 15:53:35 +00:00

197 lines
4.8 KiB
TypeScript

import {
Button,
Callout,
Input,
Intent,
FormGroup,
Lozenge,
} from '@vegaprotocol/ui-toolkit';
import React from 'react';
import { useTranslation } from 'react-i18next';
import type {
TransactionAction,
TransactionState,
} from '../../hooks/transaction-reducer';
import {
TransactionActionType,
TxState,
} from '../../hooks/transaction-reducer';
import { BigNumber } from '../../lib/bignumber';
import { TransactionCallout } from '../transaction-callout';
const inputName = 'amount';
export const AmountInput = ({
amount,
setAmount,
maximum,
currency,
}: {
amount: string;
setAmount: React.Dispatch<React.SetStateAction<string>>;
maximum: BigNumber;
currency: string;
}) => {
const { t } = useTranslation();
return (
<div className="flex">
<div className="flex-1">
<Input
data-testid="token-amount-input"
name={inputName}
id={inputName}
onChange={(e) => setAmount(e.target.value)}
value={amount}
autoComplete="off"
type="number"
max={maximum.toString()}
min={0}
step="any"
appendElement={
<Lozenge
variant={Intent.None}
className="text-[10px] relative top-[-2px]"
>
{currency}
</Lozenge>
}
/>
</div>
{maximum && (
<Button
variant="inline-link"
onClick={() => setAmount(maximum.toString())}
data-testid="token-amount-use-maximum"
className="flex flex-col justify-center p-8 h-28 my-0 mx-8"
>
{t('Use maximum')}
</Button>
)}
</div>
);
};
export const TokenInput = ({
amount,
setAmount,
perform,
submitText,
currency,
approveText,
allowance,
approve,
requireApproval = false,
maximum = new BigNumber('0'),
minimum = new BigNumber('0'),
approveTxState,
approveTxDispatch,
}: {
amount: string;
setAmount: React.Dispatch<React.SetStateAction<string>>;
perform: () => void;
submitText: string;
currency: string;
requireApproval?: boolean;
maximum?: BigNumber;
minimum?: BigNumber;
allowance?: BigNumber;
approve?: () => void;
approveText?: string;
approveTxState?: TransactionState;
approveTxDispatch?: React.Dispatch<TransactionAction>;
}) => {
if (
requireApproval &&
(allowance == null ||
approve == null ||
!approveTxState ||
!approveTxDispatch)
) {
throw new Error(
'If requires approval is true allowance, approve, approveTxState and approveDispatch props are required!'
);
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const isApproved = !new BigNumber(allowance!).isEqualTo(0);
const showApproveButton =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
!isApproved || new BigNumber(amount).isGreaterThan(allowance!);
const isDisabled = React.useMemo<boolean>(() => {
if (requireApproval) {
return (
!isApproved ||
!amount ||
new BigNumber(amount).isLessThanOrEqualTo('0') ||
new BigNumber(amount).isGreaterThan(maximum)
);
}
return (
!amount ||
new BigNumber(amount).isLessThanOrEqualTo('0') ||
new BigNumber(amount).isGreaterThan(maximum) ||
new BigNumber(amount).isLessThan(minimum)
);
}, [amount, isApproved, maximum, requireApproval, minimum]);
let approveContent = null;
if (showApproveButton) {
if (
approveTxDispatch &&
approveTxState &&
approveTxState.txState !== TxState.Default
) {
approveContent = (
<TransactionCallout
state={approveTxState}
pendingHeading={`Approve ${currency} for staking on Vega`}
reset={() =>
approveTxDispatch({ type: TransactionActionType.TX_RESET })
}
/>
);
} else {
approveContent = (
<Button
data-testid="token-input-approve-button"
className="token-input__submit w-full"
onClick={approve}
>
{approveText}
</Button>
);
}
} else if (requireApproval) {
approveContent = (
<Callout iconName="tick" intent={Intent.Success}>
<p>{`${currency} are approved for staking`}</p>
</Callout>
);
}
return (
<>
<FormGroup label="" labelFor={inputName}>
<AmountInput
amount={amount}
setAmount={setAmount}
maximum={maximum}
currency={currency}
/>
</FormGroup>
{approveContent ? <div className="mb-24">{approveContent}</div> : null}
<Button
data-testid="token-input-submit-button"
className="w-full"
disabled={isDisabled}
onClick={perform}
>
{submitText}
</Button>
</>
);
};