@@ -275,16 +275,20 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
275275 const newCursorPosition = newValue . indexOf ( " " , mentionIndex + insertValue . length ) + 1
276276 setCursorPosition ( newCursorPosition )
277277 setIntendedCursorPosition ( newCursorPosition )
278- } else if ( type === ContextMenuOptionType . Escape ) {
278+ }
279+
280+ if ( type === ContextMenuOptionType . Escape ) {
279281 // Replace @ with \@ and position cursor after the escaped sequence
280- const beforeCursor = textAreaRef . current . value . substring ( 0 , cursorPosition - 1 ) // -1 to remove the '@'
281- const afterCursor = textAreaRef . current . value . substring ( cursorPosition )
282- const newValue = beforeCursor + "\\@" + afterCursor
283- setInputValue ( newValue )
284- // Position cursor after the escaped '@'
285- const newCaretPosition = beforeCursor . length + 2 // +2 for '\@'
286- setCursorPosition ( newCaretPosition )
287- setIntendedCursorPosition ( newCaretPosition )
282+ if ( textAreaRef . current ) {
283+ const beforeCursor = textAreaRef . current . value . substring ( 0 , cursorPosition - 1 ) // -1 to remove the '@'
284+ const afterCursor = textAreaRef . current . value . substring ( cursorPosition )
285+ const newValue = beforeCursor + "\\@" + afterCursor
286+ setInputValue ( newValue )
287+ // Position cursor after the escaped '@'
288+ const newCaretPosition = beforeCursor . length + 2 // +2 for '\@'
289+ setCursorPosition ( newCaretPosition )
290+ setIntendedCursorPosition ( newCaretPosition )
291+ }
288292
289293 // Scroll to cursor.
290294 setTimeout ( ( ) => {
0 commit comments