Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
snoopy1412 committed Sep 13, 2024
1 parent d38545b commit 0423ccb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
46 changes: 34 additions & 12 deletions src/components/amount-input-with-balance.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React, { useCallback, useMemo } from 'react';
import { Button, cn } from '@nextui-org/react';
import { BigNumber } from 'bignumber.js';

import BalanceDescription from './balance-description';

BigNumber.config({
EXPONENTIAL_AT: [-1000, 1000],
DECIMAL_PLACES: 1000
});

interface AmountInputWithBalanceProps {
symbol?: string;
className?: string;
Expand Down Expand Up @@ -39,22 +43,39 @@ const AmountInputWithBalance = ({
(e: React.ChangeEvent<HTMLInputElement>) => {
if (isDisabled) return;
const newValue = e.target.value;
const numValue = new BigNumber(newValue);

if (newValue === '' || numValue.isNaN()) {
onChange?.(e);
} else {
const minValue = new BigNumber(min);
const result = BigNumber.max(BigNumber.min(numValue, maxValue), minValue);
onChange?.({ target: { value: result.toString() } } as React.ChangeEvent<HTMLInputElement>);
// // 允许输入小数点和数字
if (newValue === '' || /^[0-9]*\.?[0-9]*$/.test(newValue)) {
onChange?.({
target: { value: newValue }
} as React.ChangeEvent<HTMLInputElement>);
}
},
[onChange, min, maxValue, isDisabled]
[isDisabled, onChange]
);

const handleInputBlur = useCallback(() => {
if (value === '' || value === '.') {
onChange?.({
target: { value: '0' }
} as React.ChangeEvent<HTMLInputElement>);
return;
}

const numValue = new BigNumber(value || '0');
const minValue = new BigNumber(min);
const result = BigNumber.max(BigNumber.min(numValue, maxValue), minValue);
const formattedResult = result.decimalPlaces(18, BigNumber.ROUND_DOWN);

onChange?.({
target: { value: formattedResult?.toString() }
} as React.ChangeEvent<HTMLInputElement>);
}, [value, onChange, min, maxValue]);

const handleMaxClick = useCallback(() => {
onChange?.({ target: { value: balance || '0' } } as React.ChangeEvent<HTMLInputElement>);
}, [balance, onChange]);
onChange?.({
target: { value: maxValue?.toString() }
} as React.ChangeEvent<HTMLInputElement>);
}, [maxValue, onChange]);

return (
<div className={cn('flex w-full flex-col gap-[0.31rem]', className)}>
Expand All @@ -67,6 +88,7 @@ const AmountInputWithBalance = ({
placeholder="0"
className="w-full appearance-none bg-transparent pr-16 text-[1rem] font-bold text-foreground placeholder:text-[0.875rem] placeholder:font-bold placeholder:text-[#c6c6c6] hover:bg-transparent focus-visible:outline-none"
onChange={handleInputChange}
onBlur={handleInputBlur}
step="any"
disabled={isDisabled}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/collator/_hooks/collator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const useCreateCollator = ({
functionName: 'assetsToVotes',
args: [stakedOf ?? 0n, commission],
query: {
enabled: isEnabled && !!commission && !!stakedOf && enabled
enabled: isEnabled && !!stakedOf && enabled
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/collator/_hooks/update-commission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const useUpdateCommission = ({
functionName: 'assetsToVotes',
args: [totalAssets, newCommission],
query: {
enabled: !!newCommission && !isNil(totalAssets)
enabled: !isNil(totalAssets)
}
});

Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useAssetsToVotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ interface AssetsToVotesProps {

function useAssetsToVotes({ commission, totalAmount, inputAmount, operation }: AssetsToVotesProps) {
const { isEnabled } = useWalletStatus();

const result = useReadContract({
abi,
address,
functionName: 'assetsToVotes',
args: [commission, calculateAssets(totalAmount, inputAmount, operation)],
query: {
enabled: isEnabled && !isNil(totalAmount) && !isNil(inputAmount) && !!commission
enabled: isEnabled && !isNil(totalAmount) && !isNil(inputAmount)
}
});

Expand Down
19 changes: 10 additions & 9 deletions src/hooks/useDebouncedState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useCallback } from 'react';
import { useState, useCallback, useEffect } from 'react';
import { useDebounce } from 'react-use';

interface UseDebouncedStateProps<T> {
Expand All @@ -23,7 +23,7 @@ export function useDebouncedState<T>({
const [debouncedValue, setDebouncedValue] = useState<T>(initialValue);
const [isLoading, setIsLoading] = useState(false);

const [, cancel] = useDebounce(
useDebounce(
() => {
setDebouncedValue(value);
setIsLoading(false);
Expand All @@ -32,14 +32,15 @@ export function useDebouncedState<T>({
[value]
);

const handleChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
cancel();
useEffect(() => {
if (value !== debouncedValue) {
setIsLoading(true);
setValue(e.target.value as unknown as T);
},
[cancel]
);
}
}, [value, debouncedValue]);

const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value as unknown as T);
}, []);

const reset = useCallback(() => {
setValue(initialValue);
Expand Down

0 comments on commit 0423ccb

Please sign in to comment.