Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 50% and Max from the Backstop pool withdraw UI #496

Merged
5 changes: 3 additions & 2 deletions src/components/Form/From/AvailableActions/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
interface AvailableActionsProps {
max?: number;
setValue?: (n: number) => void;
hideAvailableBalance?: boolean;
}

export const AvailableActions = ({ max, setValue }: AvailableActionsProps) => (
export const AvailableActions = ({ max, setValue, hideAvailableBalance = false }: AvailableActionsProps) => (
<div className="flex gap-1 text-sm">
{max !== undefined && setValue !== undefined && (
<>
<span className="mr-1">Available: {max.toFixed(2)}</span>
{hideAvailableBalance ? <></> : <span className="mr-1">Available: {max.toFixed(2)}</span>}
<button className="text-primary hover:underline" onClick={() => setValue(Number(max) * 0.5)} type="button">
50%
</button>
Expand Down
9 changes: 7 additions & 2 deletions src/components/nabla/Pools/Backstop/AddLiquidity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ const AddLiquidity = ({ data, onClose }: AddLiquidityProps): JSX.Element | null
</p>
</div>
)}
<AmountSelector maxBalance={balanceQuery.data} formFieldName="amount" form={form} />
<AmountSelector
maxBalance={balanceQuery.data}
formFieldName="amount"
form={form}
showAvailableActions={true}
/>
<Validation className="text-center mt-2" errors={errors} />
<div className="relative flex w-full flex-col gap-4 rounded-lg bg-neutral-100 dark:bg-neutral-700 text-neutral-500 dark:text-neutral-300 p-4 mt-4">
<div className="relative flex w-full flex-col gap-4 rounded-lg bg-base-300 text-neutral-500 dark:text-neutral-300 p-4 mt-4">
<div className="flex items-center justify-between">
<div>Total LP tokens</div>
<div>
Expand Down
9 changes: 7 additions & 2 deletions src/components/nabla/Pools/Swap/AddLiquidity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ const AddLiquidity = ({ data, onClose }: AddLiquidityProps): JSX.Element | null
</p>
</div>
)}
<AmountSelector maxBalance={balanceQuery.data} formFieldName="amount" form={form} />
<AmountSelector
maxBalance={balanceQuery.data}
formFieldName="amount"
form={form}
showAvailableActions={true}
/>
<Validation className="text-center mt-2" errors={errors} />
<div className="relative flex w-full flex-col gap-4 rounded-lg bg-neutral-100 dark:bg-neutral-700 text-neutral-500 dark:text-neutral-300 p-4 mt-4">
<div className="relative flex w-full flex-col gap-4 rounded-lg bg-base-300 text-neutral-500 dark:text-neutral-300 p-4 mt-4">
<div className="flex items-center justify-between">
<div>Total LP Tokens</div>
<div>
Expand Down
5 changes: 3 additions & 2 deletions src/components/nabla/Pools/Swap/Redeem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const Redeem = ({ data, onClose }: RedeemProps): JSX.Element | null => {
</div>
</AmountSelector>
<Validation className="text-center mt-2" errors={errors} />
<div className="relative flex w-full flex-col gap-4 rounded-lg bg-neutral-100 dark:bg-neutral-700 text-neutral-500 dark:text-neutral-300 p-4 mt-4">
<div className="relative flex w-full flex-col gap-4 rounded-lg bg-base-300 text-neutral-500 dark:text-neutral-300 p-4 mt-4">
<div className="flex items-center justify-between">
<div>Security fee</div>
<div>{(data.insuranceFeeBps / 100).toFixed(2)}%</div>
Expand Down Expand Up @@ -151,7 +151,8 @@ const Redeem = ({ data, onClose }: RedeemProps): JSX.Element | null => {
{walletAccount ? (
<Button
color="primary"
className={`w-full ${withdrawalQuote.isLoading ? 'loading' : ''}`}
className="w-full"
loading={withdrawalQuote.isLoading}
type="submit"
disabled={!submitEnabled}
>
Expand Down
5 changes: 3 additions & 2 deletions src/components/nabla/Pools/Swap/WithdrawLiquidity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const WithdrawLiquidity = ({ data, onClose }: WithdrawLiquidityProps): JSX.Eleme
</div>
</AmountSelector>
<Validation className="text-center mt-2" errors={errors} />
<div className="relative flex w-full flex-col gap-4 rounded-lg bg-neutral-100 dark:bg-neutral-700 text-neutral-500 dark:text-neutral-300 p-4 mt-4">
<div className="relative flex w-full flex-col gap-4 rounded-lg bg-base-300 text-neutral-500 dark:text-neutral-300 p-4 mt-4">
<div className="flex items-center justify-between">
<div>Total LP tokens</div>
<div>
Expand Down Expand Up @@ -114,7 +114,8 @@ const WithdrawLiquidity = ({ data, onClose }: WithdrawLiquidityProps): JSX.Eleme
{walletAccount ? (
<Button
color="primary"
className={`w-full ${withdrawalQuote.isLoading ? 'loading' : ''}`}
className="w-full"
loading={withdrawalQuote.isLoading}
type="submit"
disabled={!submitEnabled}
>
Expand Down
2 changes: 1 addition & 1 deletion src/components/nabla/Swap/From.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function From<FormFieldValues extends FieldValues, TFieldName extends Fie
maxBalance={fromTokenBalance.data}
formFieldName={fromFormFieldName}
form={form}
onlyShowNumberInput={true}
onlyShowNumericInput={true}
/>
</div>
<Button
Expand Down
66 changes: 23 additions & 43 deletions src/components/nabla/common/AmountSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, Range } from 'react-daisyui';
import { Range } from 'react-daisyui';
import { FieldPath, FieldValues, PathValue, UseFormReturn, useWatch } from 'react-hook-form';
import { useEffect, useMemo } from 'preact/hooks';
import Big from 'big.js';
Expand All @@ -9,21 +9,24 @@ import { ContractBalance } from '../../../helpers/contracts';
import { calcSharePercentageNumber } from '../../../helpers/calc';
import { NumericInput } from '../../Form/From/NumericInput';
import { USER_INPUT_MAX_DECIMALS } from '../../../shared/parseNumbers/decimal';
import { AvailableActions } from '../../Form/From/AvailableActions';

interface AmountSelectorProps<FormFieldValues extends FieldValues, TFieldName extends FieldPath<FormFieldValues>> {
maxBalance: ContractBalance | undefined;
formFieldName: TFieldName;
form: UseFormReturn<FormFieldValues>;
children?: ReactNode;
onlyShowNumberInput?: boolean;
onlyShowNumericInput?: boolean;
showAvailableActions?: boolean;
}

export function AmountSelector<FormFieldValues extends FieldValues, TFieldName extends FieldPath<FormFieldValues>>({
formFieldName,
maxBalance,
form,
children,
onlyShowNumberInput,
onlyShowNumericInput,
showAvailableActions = false,
}: AmountSelectorProps<FormFieldValues, TFieldName>) {
type K = PathValue<FormFieldValues, TFieldName>;

Expand Down Expand Up @@ -64,7 +67,7 @@ export function AmountSelector<FormFieldValues extends FieldValues, TFieldName e
}
}, [amountString, amountBigDecimal, formFieldName, maxBalance, setError, clearErrors]);

if (onlyShowNumberInput === true) {
if (onlyShowNumericInput) {
return (
<NumericInput
additionalStyle="input-ghost w-full flex-grow text-4xl font-outfit px-0 py-3"
Expand All @@ -76,47 +79,24 @@ export function AmountSelector<FormFieldValues extends FieldValues, TFieldName e
}

return (
<div className="relative rounded-lg bg-neutral-100 dark:bg-neutral-700 p-4">
<div className="flex items-center justify-between mb-4">
<div className="flex items-center gap-1">
<NumericInput
additionalStyle="input-ghost w-full flex-grow text-4xl font-outfit px-0 py-3"
register={form.register(formFieldName)}
autoFocus
maxDecimals={maxBalance?.decimals ?? USER_INPUT_MAX_DECIMALS.PENDULUM}
<div className="relative rounded-lg bg-base-300 p-4">
<NumericInput
additionalStyle="input-ghost w-full flex-grow text-4xl font-outfit px-0 py-3"
register={form.register(formFieldName)}
autoFocus
maxDecimals={maxBalance?.decimals ?? USER_INPUT_MAX_DECIMALS.PENDULUM}
/>
{showAvailableActions ? (
<div className="justify-end flex w-full">
<AvailableActions
setValue={(n) => setValue(formFieldName, n as K)}
max={maxBalance?.approximateNumber}
hideAvailableBalance={true}
/>
<Button
className="bg-neutral-200 dark:bg-neutral-800 px-3 rounded-2xl"
size="sm"
type="button"
onClick={() => {
if (maxBalance !== undefined) {
setValue(formFieldName, fractionOfValue(maxBalance.preciseBigDecimal, 50) as K, {
shouldDirty: true,
shouldTouch: true,
});
}
}}
>
50%
</Button>
<Button
className="bg-neutral-200 dark:bg-neutral-800 px-3 rounded-2xl"
size="sm"
type="button"
onClick={() => {
if (maxBalance !== undefined) {
setValue(formFieldName, fractionOfValue(maxBalance.preciseBigDecimal, 100) as K, {
shouldDirty: true,
shouldTouch: true,
});
}
}}
>
MAX
</Button>
</div>
</div>
) : (
<></>
)}
<Range
color={maxBalance === undefined ? 'secondary' : 'primary'}
min={0}
Expand Down
36 changes: 14 additions & 22 deletions src/components/nabla/common/PoolSelectorModal.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { CheckIcon } from '@heroicons/react/20/solid';
import { matchSorter } from 'match-sorter';
import { ChangeEvent, useMemo, useState } from 'preact/compat';
import { Avatar, AvatarProps, Button, Input, Modal } from 'react-daisyui';
import { Avatar, AvatarProps, Button, Input } from 'react-daisyui';

import pendulumIcon from '../../../assets/pendulum-icon.svg';
import { repeat } from '../../../helpers/general';
import ModalCloseButton from '../../Button/ModalClose';
import { Skeleton } from '../../Skeleton';
import { NablaInstanceBackstopPool, NablaInstanceSwapPool } from '../../../hooks/nabla/useNablaInstance';
import { getIcon } from '../../../shared/AssetIcons';
import { Dialog } from '../../../pages/staking/dialogs/Dialog';

export type PoolEntry =
| { type: 'swapPool'; pool: NablaInstanceSwapPool }
Expand Down Expand Up @@ -91,7 +91,7 @@ function PoolList({ swapPools, backstopPool, onSelect, selected }: PoolListProps
<CheckIcon className="absolute -right-1 -top-1 w-5 h-5 p-[3px] text-white bg-green-600 rounded-full" />
)}
</span>
<span className="flex flex-col">
<span className="flex flex-col ml-3">
<span className="text-lg dark:text-white leading-5">
<strong>
{showPoolType ? (type === 'backstopPool' ? 'Backstop Pool ' : 'Swap Pool ') : ''}
Expand All @@ -114,30 +114,22 @@ interface PoolSelectorModalProps extends PoolListProps {
open: boolean;
}

export function PoolSelectorModal({
export const PoolSelectorModal = ({
swapPools,
backstopPool,
selected,
isLoading,
onSelect,
onClose,
open,
}: PoolSelectorModalProps) {
return (
<Modal className="bg-[--bg-modal]" open={open}>
<Modal.Header className="mb-0">
<ModalCloseButton onClick={onClose} />
<h3 className="text-2xl font-normal">{backstopPool !== undefined ? 'Select a pool' : 'Select a token'}</h3>
</Modal.Header>
<Modal.Body>
<div className="py-4">
{isLoading ? (
repeat(<Skeleton className="w-full h-10 mb-2" />)
) : (
<PoolList swapPools={swapPools} backstopPool={backstopPool} onSelect={onSelect} selected={selected} />
)}
</div>
</Modal.Body>
</Modal>
}: PoolSelectorModalProps) => {
const content = isLoading ? (
<>{repeat(<Skeleton className="w-full h-10 mb-2" />)}</>
) : (
<PoolList swapPools={swapPools} backstopPool={backstopPool} onSelect={onSelect} selected={selected} />
);
}

const title = backstopPool !== undefined ? 'Select a pool' : 'Select a token';

return <Dialog visible={open} onClose={onClose} actions={<></>} content={content} headerText={title} />;
};
26 changes: 10 additions & 16 deletions src/components/nabla/common/SwapProgress.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { JSX } from 'preact';
import { Modal } from 'react-daisyui';
import ModalCloseButton from '../../Button/ModalClose';
import { TransactionProgress, TransactionProgressProps } from '../common/TransactionProgress';
import { Dialog } from '../../../pages/staking/dialogs/Dialog';

export type SwapProgressProps = {
open: boolean;
Expand All @@ -10,19 +9,14 @@ export type SwapProgressProps = {
mutation?: TransactionProgressProps['mutation'];
};

export function SwapProgress({ mutation, children, ...rest }: SwapProgressProps) {
return (
<Modal className="modal-top bg-[--bg-modal]" {...rest}>
<Modal.Header className="mb-0">
<ModalCloseButton onClick={rest.onClose} />
</Modal.Header>
<Modal.Body>
{!!mutation && (
<TransactionProgress mutation={mutation} onClose={rest.onClose}>
{children}
</TransactionProgress>
)}
</Modal.Body>
</Modal>
export function SwapProgress({ mutation, children, onClose, open }: SwapProgressProps) {
const content = mutation ? (
<TransactionProgress mutation={mutation} onClose={onClose}>
{children}
</TransactionProgress>
) : (
<></>
);

return <Dialog visible={open} onClose={onClose} content={content} actions={<></>} />;
}
Loading