|
| 1 | +import React, { forwardRef, useCallback, useEffect, useMemo, useRef, useState } from "react"; |
| 2 | + |
| 3 | +import { useTranslation } from "react-i18next"; |
| 4 | + |
| 5 | +import { AiTextAreaProps } from "@interfaces/components/forms/aiTextarea.interface"; |
| 6 | +import { cn } from "@utilities"; |
| 7 | + |
| 8 | +export const AiTextArea = forwardRef<HTMLTextAreaElement, AiTextAreaProps>( |
| 9 | + ( |
| 10 | + { |
| 11 | + className, |
| 12 | + onBlur, |
| 13 | + onChange, |
| 14 | + onEnterSubmit = true, |
| 15 | + onFocus, |
| 16 | + onKeyDown, |
| 17 | + onShiftEnterNewLine = true, |
| 18 | + onSubmitIconHover, |
| 19 | + placeholder, |
| 20 | + useDefaultPlaceholder = true, |
| 21 | + submitIcon, |
| 22 | + hasClearedTextarea = false, |
| 23 | + onClearTextarea, |
| 24 | + defaultPlaceholderText, |
| 25 | + autoGrow = true, |
| 26 | + minHeightVh = 8, |
| 27 | + maxHeightVh, |
| 28 | + ...rest |
| 29 | + }, |
| 30 | + ref |
| 31 | + ) => { |
| 32 | + const { t } = useTranslation("chatbot"); |
| 33 | + const internalRef = useRef<HTMLTextAreaElement>(null); |
| 34 | + const textareaRef = internalRef; |
| 35 | + const [isFocused, setIsFocused] = useState(false); |
| 36 | + const [isBlurred, setIsBlurred] = useState(false); |
| 37 | + const [isEmpty, setIsEmpty] = useState(false); |
| 38 | + |
| 39 | + const [windowHeight, setWindowHeight] = useState(window.innerHeight); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + const handleResize = () => { |
| 43 | + setWindowHeight(window.innerHeight); |
| 44 | + }; |
| 45 | + |
| 46 | + window.addEventListener("resize", handleResize); |
| 47 | + return () => { |
| 48 | + window.removeEventListener("resize", handleResize); |
| 49 | + // Cleanup focus timeout on unmount |
| 50 | + if (focusTimeoutRef.current) { |
| 51 | + clearTimeout(focusTimeoutRef.current); |
| 52 | + } |
| 53 | + }; |
| 54 | + }, []); |
| 55 | + |
| 56 | + const actualMinHeight = useMemo(() => { |
| 57 | + let responsiveMinHeightVh = minHeightVh; |
| 58 | + if (responsiveMinHeightVh === undefined) { |
| 59 | + if (windowHeight >= 1600) { |
| 60 | + responsiveMinHeightVh = 12; |
| 61 | + } else if (windowHeight >= 1200) { |
| 62 | + responsiveMinHeightVh = 10; |
| 63 | + } else { |
| 64 | + responsiveMinHeightVh = 8; |
| 65 | + } |
| 66 | + } |
| 67 | + return (windowHeight * responsiveMinHeightVh) / 100; |
| 68 | + }, [minHeightVh, windowHeight]); |
| 69 | + |
| 70 | + const getMaxHeight = useCallback(() => { |
| 71 | + const viewportHeight = window.innerHeight; |
| 72 | + |
| 73 | + if (maxHeightVh !== undefined) { |
| 74 | + return (viewportHeight * maxHeightVh) / 100; |
| 75 | + } |
| 76 | + |
| 77 | + let responsiveMaxHeightVh; |
| 78 | + if (viewportHeight >= 1400) { |
| 79 | + responsiveMaxHeightVh = 70; |
| 80 | + } else if (viewportHeight >= 1000) { |
| 81 | + responsiveMaxHeightVh = 40; |
| 82 | + } else if (viewportHeight >= 800) { |
| 83 | + responsiveMaxHeightVh = 30; |
| 84 | + } else { |
| 85 | + responsiveMaxHeightVh = 25; |
| 86 | + } |
| 87 | + return (viewportHeight * responsiveMaxHeightVh) / 100; |
| 88 | + }, [maxHeightVh]); |
| 89 | + const adjustHeight = useCallback(() => { |
| 90 | + if (!autoGrow || !textareaRef.current) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const textarea = textareaRef.current; |
| 95 | + |
| 96 | + const currentMaxHeight = getMaxHeight(); |
| 97 | + |
| 98 | + textarea.style.height = "auto"; |
| 99 | + |
| 100 | + const scrollHeight = textarea.scrollHeight; |
| 101 | + const newHeight = Math.min(Math.max(scrollHeight, actualMinHeight), currentMaxHeight); |
| 102 | + |
| 103 | + textarea.style.height = `${newHeight}px`; |
| 104 | + }, [autoGrow, actualMinHeight, getMaxHeight, textareaRef]); |
| 105 | + |
| 106 | + useEffect(() => { |
| 107 | + adjustHeight(); |
| 108 | + }, [adjustHeight]); |
| 109 | + |
| 110 | + useEffect(() => { |
| 111 | + if (autoGrow && textareaRef.current) { |
| 112 | + textareaRef.current.style.height = `${actualMinHeight}px`; |
| 113 | + } |
| 114 | + }, [autoGrow, actualMinHeight, textareaRef]); |
| 115 | + |
| 116 | + const handleKeyDown = useCallback( |
| 117 | + (e: React.KeyboardEvent<HTMLTextAreaElement>) => { |
| 118 | + if (onEnterSubmit && e.key === "Enter" && !e.shiftKey) { |
| 119 | + e.preventDefault(); |
| 120 | + const form = e.currentTarget.form; |
| 121 | + if (form) { |
| 122 | + form.requestSubmit(); |
| 123 | + } |
| 124 | + } else if (onShiftEnterNewLine && e.key === "Enter" && e.shiftKey) { |
| 125 | + return; |
| 126 | + } |
| 127 | + onKeyDown?.(e); |
| 128 | + }, |
| 129 | + [onKeyDown, onEnterSubmit, onShiftEnterNewLine] |
| 130 | + ); |
| 131 | + |
| 132 | + const focusTimeoutRef = useRef<NodeJS.Timeout | null>(null); |
| 133 | + |
| 134 | + const handleFocus = useCallback( |
| 135 | + (e: React.FocusEvent<HTMLTextAreaElement>) => { |
| 136 | + setIsFocused(true); |
| 137 | + setIsBlurred(false); |
| 138 | + setIsEmpty(!e.target.value); |
| 139 | + if ( |
| 140 | + !hasClearedTextarea && |
| 141 | + e.target.value === (defaultPlaceholderText || t("aiTextarea.defaultPlaceholder")) |
| 142 | + ) { |
| 143 | + e.target.value = ""; |
| 144 | + onClearTextarea?.(true); |
| 145 | + } |
| 146 | + onFocus?.(e); |
| 147 | + |
| 148 | + // Clear existing timeout to prevent memory leaks |
| 149 | + if (focusTimeoutRef.current) { |
| 150 | + clearTimeout(focusTimeoutRef.current); |
| 151 | + } |
| 152 | + |
| 153 | + focusTimeoutRef.current = setTimeout(() => { |
| 154 | + setIsFocused(false); |
| 155 | + focusTimeoutRef.current = null; |
| 156 | + }, 6000); |
| 157 | + }, |
| 158 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 159 | + [onFocus, hasClearedTextarea, defaultPlaceholderText] |
| 160 | + ); |
| 161 | + |
| 162 | + const handleChange = useCallback( |
| 163 | + (e: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 164 | + setIsEmpty(!e.target.value); |
| 165 | + onChange?.(e); |
| 166 | + setTimeout(() => { |
| 167 | + adjustHeight(); |
| 168 | + }, 0); |
| 169 | + }, |
| 170 | + [onChange, adjustHeight] |
| 171 | + ); |
| 172 | + |
| 173 | + const handleBlur = useCallback( |
| 174 | + (e: React.FocusEvent<HTMLTextAreaElement>) => { |
| 175 | + setIsFocused(false); |
| 176 | + setIsBlurred(true); |
| 177 | + setIsEmpty(!e.target.value); |
| 178 | + onBlur?.(e); |
| 179 | + }, |
| 180 | + [onBlur] |
| 181 | + ); |
| 182 | + |
| 183 | + const dynamicStyles = useMemo(() => { |
| 184 | + if (autoGrow) return {}; |
| 185 | + return { |
| 186 | + maxHeight: `${maxHeightVh || 27}vh`, |
| 187 | + minHeight: `${minHeightVh || 8}vh`, |
| 188 | + overflowY: "auto" as const, |
| 189 | + }; |
| 190 | + }, [autoGrow, maxHeightVh, minHeightVh]); |
| 191 | + |
| 192 | + const textAreaClass = cn( |
| 193 | + "w-full resize-none", |
| 194 | + autoGrow ? "overflow-y-auto" : "overflow-hidden", |
| 195 | + "rounded-2xl border-2 p-5 pr-16", |
| 196 | + "bg-black/90 text-base transition-all duration-300 ease-in-out", |
| 197 | + autoGrow ? "whitespace-pre-wrap break-words" : "", |
| 198 | + "placeholder:text-gray-400", |
| 199 | + // State-based styling for security |
| 200 | + { |
| 201 | + "border-green-400 text-white shadow-[0_0_20px_rgba(126,211,33,0.2)]": isFocused, |
| 202 | + "border-green-400/30": !isFocused, |
| 203 | + "text-gray-400": !isFocused && !isEmpty, |
| 204 | + "text-gray-500": isBlurred && isEmpty, |
| 205 | + }, |
| 206 | + className |
| 207 | + ); |
| 208 | + |
| 209 | + const submitButtonClass = cn( |
| 210 | + "absolute flex cursor-pointer items-center justify-center border-none", |
| 211 | + "transition-all duration-300 ease-in-out", |
| 212 | + "right-3 top-1/2 -translate-y-1/2", |
| 213 | + "size-9 rounded-lg bg-green-400 text-black", |
| 214 | + "hover:scale-105 hover:bg-green-500" |
| 215 | + ); |
| 216 | + |
| 217 | + return ( |
| 218 | + <div className="relative mx-auto mb-6 max-w-700"> |
| 219 | + {isFocused ? ( |
| 220 | + <div className="absolute -top-7 left-0 z-10"> |
| 221 | + <span className="rounded-md bg-black/60 px-2 py-1 text-xs text-green-200"> |
| 222 | + {t("aiTextarea.shiftEnterHint")} |
| 223 | + </span> |
| 224 | + </div> |
| 225 | + ) : null} |
| 226 | + <textarea |
| 227 | + {...rest} |
| 228 | + className={textAreaClass} |
| 229 | + onBlur={handleBlur} |
| 230 | + onChange={handleChange} |
| 231 | + onFocus={handleFocus} |
| 232 | + onKeyDown={handleKeyDown} |
| 233 | + placeholder={placeholder || (useDefaultPlaceholder ? t("aiTextarea.placeholder") : undefined)} |
| 234 | + ref={(element) => { |
| 235 | + (textareaRef as React.MutableRefObject<HTMLTextAreaElement | null>).current = element; |
| 236 | + if (typeof ref === "function") { |
| 237 | + ref(element); |
| 238 | + } else if (ref) { |
| 239 | + (ref as React.MutableRefObject<HTMLTextAreaElement | null>).current = element; |
| 240 | + } |
| 241 | + }} |
| 242 | + style={dynamicStyles} |
| 243 | + /> |
| 244 | + {submitIcon ? ( |
| 245 | + <button |
| 246 | + className={submitButtonClass} |
| 247 | + onMouseEnter={() => onSubmitIconHover?.(true)} |
| 248 | + onMouseLeave={() => onSubmitIconHover?.(false)} |
| 249 | + type="submit" |
| 250 | + > |
| 251 | + {submitIcon} |
| 252 | + </button> |
| 253 | + ) : null} |
| 254 | + </div> |
| 255 | + ); |
| 256 | + } |
| 257 | +); |
| 258 | + |
| 259 | +AiTextArea.displayName = "AiTextArea"; |
0 commit comments