import { IconButton, Input, InputGroup, InputProps, InputRightElement, Popover, PopoverContent, PopoverTrigger, } from '@chakra-ui/react' import { UserIcon } from 'assets/icons' import { Variable } from 'models' import React, { useEffect, useRef, useState } from 'react' import { useDebounce } from 'use-debounce' import { VariableSearchInput } from './VariableSearchInput' export const InputWithVariable = ({ initialValue, noAbsolute, onValueChange, ...props }: { initialValue: string onValueChange: (value: string) => void noAbsolute?: boolean } & InputProps) => { const inputRef = useRef(null) const [value, setValue] = useState(initialValue) const [debouncedValue] = useDebounce(value, 100) const [carretPosition, setCarretPosition] = useState(0) useEffect(() => { onValueChange(debouncedValue) // eslint-disable-next-line react-hooks/exhaustive-deps }, [debouncedValue]) const handleVariableSelected = (variable: Variable) => { if (!inputRef.current) return const cursorPosition = carretPosition const textBeforeCursorPosition = inputRef.current.value.substring( 0, cursorPosition ) const textAfterCursorPosition = inputRef.current.value.substring( cursorPosition, inputRef.current.value.length ) setValue( textBeforeCursorPosition + `{{${variable.name}}}` + textAfterCursorPosition ) inputRef.current.focus() setTimeout(() => { if (!inputRef.current) return inputRef.current.selectionStart = inputRef.current.selectionEnd = carretPosition + `{{${variable.name}}}`.length }, 100) } const handleKeyUp = () => { if (!inputRef.current?.selectionStart) return setCarretPosition(inputRef.current.selectionStart) } const handleChange = (e: React.ChangeEvent) => setValue(e.target.value) return ( } size="sm" pos="relative" /> ) }