import { VariablesButton } from '@/features/variables' import { NumberInputProps, NumberInput, NumberInputField, NumberInputStepper, NumberIncrementStepper, NumberDecrementStepper, HStack, FormControl, FormLabel, } from '@chakra-ui/react' import { Variable, VariableString } from 'models' import { useEffect, useState } from 'react' import { useDebouncedCallback } from 'use-debounce' import { env } from 'utils' import { MoreInfoTooltip } from '../MoreInfoTooltip' type Value = HasVariable extends undefined | true ? number | VariableString : number type Props = { defaultValue?: Value debounceTimeout?: number withVariableButton?: HasVariable label?: string moreInfoTooltip?: string isRequired?: boolean onValueChange: (value?: Value) => void } & Omit export const SmartNumberInput = ({ defaultValue, onValueChange, withVariableButton, debounceTimeout = 1000, label, moreInfoTooltip, isRequired, ...props }: Props) => { const [value, setValue] = useState(defaultValue?.toString() ?? '') const onValueChangeDebounced = useDebouncedCallback( onValueChange, env('E2E_TEST') === 'true' ? 0 : debounceTimeout ) useEffect( () => () => { onValueChangeDebounced.flush() }, [onValueChangeDebounced] ) const handleValueChange = (value: string) => { setValue(value) if (value.endsWith('.') || value.endsWith(',')) return if (value === '') return onValueChangeDebounced(undefined) if ( value.startsWith('{{') && value.endsWith('}}') && value.length > 4 && (withVariableButton ?? true) ) { onValueChangeDebounced(value as Value) return } const newValue = parseFloat(value) if (isNaN(newValue)) return onValueChangeDebounced(newValue) } const handleVariableSelected = (variable?: Variable) => { if (!variable) return const newValue = `{{${variable.name}}}` handleValueChange(newValue) } const Input = ( ) return ( {label && ( {label}{' '} {moreInfoTooltip && ( {moreInfoTooltip} )} )} {withVariableButton ?? true ? ( {Input} ) : ( Input )} ) }