import { PlusIcon } from "@radix-ui/react-icons"; import { cn } from "@/util/utils"; import { AutoResizingTextarea } from "./AutoResizingTextarea/AutoResizingTextarea"; import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover"; import { WorkflowBlockParameterSelect } from "@/routes/workflows/editor/nodes/WorkflowBlockParameterSelect"; import { useWorkflowTitleStore } from "@/store/WorkflowTitleStore"; import { useEffect, useRef, useState } from "react"; import { useDebouncedCallback } from "use-debounce"; type Props = Omit< React.ComponentProps, "onChange" > & { canWriteTitle?: boolean; onChange: (value: string) => void; nodeId: string; }; function WorkflowBlockInputTextarea(props: Props) { const { maybeAcceptTitle, maybeWriteTitle } = useWorkflowTitleStore(); const { nodeId, onChange, canWriteTitle = false, ...textAreaProps } = props; const [internalValue, setInternalValue] = useState(props.value ?? ""); const textareaRef = useRef(null); const [cursorPosition, setCursorPosition] = useState<{ start: number; end: number; } | null>(null); useEffect(() => { setInternalValue(props.value ?? ""); }, [props.value]); const doOnChange = useDebouncedCallback((value: string) => { onChange(value); if (canWriteTitle) { maybeWriteTitle(value); maybeAcceptTitle(); } }, 300); const handleTextareaSelect = () => { if (textareaRef.current) { setCursorPosition({ start: textareaRef.current.selectionStart, end: textareaRef.current.selectionEnd, }); } }; const insertParameterAtCursor = (parameterKey: string) => { const value = props.value ?? ""; const parameterText = `{{${parameterKey}}}`; if (cursorPosition && textareaRef.current) { const { start, end } = cursorPosition; const newValue = value.substring(0, start) + parameterText + value.substring(end); doOnChange(newValue); setTimeout(() => { if (textareaRef.current) { const newPosition = start + parameterText.length; textareaRef.current.focus(); textareaRef.current.setSelectionRange(newPosition, newPosition); } }, 0); } else { doOnChange(`${value}${parameterText}`); } }; return (
{ doOnChange.flush(); }} onChange={(event) => { setInternalValue(event.target.value); handleTextareaSelect(); doOnChange(event.target.value); }} onClick={handleTextareaSelect} onKeyUp={handleTextareaSelect} onSelect={handleTextareaSelect} className={cn("pr-9", props.className)} />
); } export { WorkflowBlockInputTextarea };