import clsx from 'clsx' import { HugeDecimal } from '@dao-dao/math' import { ButtonProps, LoadingData } from '@dao-dao/types' import { Button } from '../buttons' export type PercentButtonProps = { loadingMax: LoadingData /** * Value out of 100 (e.g. 50 = 50%). */ percent: number amount: HugeDecimal setAmount: (newAmount: HugeDecimal) => void /** * Override the default label of `{percent}%`. */ label?: string className?: string absoluteOffset?: HugeDecimal size?: ButtonProps['size'] } export const PercentButton = ({ loadingMax, percent, amount, setAmount, label = `${percent}%`, className, absoluteOffset, size, }: PercentButtonProps) => { const newAmount = loadingMax.loading ? HugeDecimal.zero : // Cap between 1 and max HugeDecimal.min( HugeDecimal.max( HugeDecimal.one, loadingMax.data .times(percent) .div(100) .plus(absoluteOffset || 0) .toFixed(0) ), loadingMax.data ) return ( ) }