Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions webview-ui/src/components/chat/ChatTextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
}, [selectedType, searchQuery])

const handleEnhancePrompt = useCallback(() => {
const trimmedInput = inputValue.trim()
const trimmedInput = (inputValue ?? "").trim()

if (trimmedInput) {
setIsEnhancingPrompt(true)
Expand All @@ -256,7 +256,7 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(

// Memoized check for whether the input has content (text or images)
const hasInputContent = useMemo(() => {
return inputValue.trim().length > 0 || selectedImages.length > 0
return (inputValue ?? "").trim().length > 0 || selectedImages.length > 0
}, [inputValue, selectedImages])

// Compute the key combination text for the send button tooltip based on enterBehavior
Expand Down Expand Up @@ -506,8 +506,9 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
}

if (event.key === "Backspace" && !isComposing) {
const charBeforeCursor = inputValue[cursorPosition - 1]
const charAfterCursor = inputValue[cursorPosition + 1]
const safeInputValue = inputValue ?? ""
const charBeforeCursor = safeInputValue[cursorPosition - 1]
const charAfterCursor = safeInputValue[cursorPosition + 1]

const charBeforeIsWhitespace =
charBeforeCursor === " " || charBeforeCursor === "\n" || charBeforeCursor === "\r\n"
Expand All @@ -519,7 +520,7 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
if (
charBeforeIsWhitespace &&
// "$" is added to ensure the match occurs at the end of the string.
inputValue.slice(0, cursorPosition - 1).match(new RegExp(mentionRegex.source + "$"))
safeInputValue.slice(0, cursorPosition - 1).match(new RegExp(mentionRegex.source + "$"))
) {
const newCursorPosition = cursorPosition - 1
// If mention is followed by another word, then instead
Expand All @@ -534,9 +535,9 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
setCursorPosition(newCursorPosition)
setJustDeletedSpaceAfterMention(true)
} else if (justDeletedSpaceAfterMention) {
const { newText, newPosition } = removeMention(inputValue, cursorPosition)

if (newText !== inputValue) {
const { newText, newPosition } = removeMention(safeInputValue, cursorPosition)
if (newText !== safeInputValue) {
event.preventDefault()
setInputValue(newText)
setIntendedCursorPosition(newPosition) // Store the new cursor position in state
Expand Down Expand Up @@ -674,8 +675,9 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
if (urlRegex.test(pastedText.trim())) {
e.preventDefault()
const trimmedUrl = pastedText.trim()
const safeInputValue = inputValue ?? ""
const newValue =
inputValue.slice(0, cursorPosition) + trimmedUrl + " " + inputValue.slice(cursorPosition)
safeInputValue.slice(0, cursorPosition) + trimmedUrl + " " + safeInputValue.slice(cursorPosition)
setInputValue(newValue)
const newCursorPosition = cursorPosition + trimmedUrl.length + 1
setCursorPosition(newCursorPosition)
Expand Down Expand Up @@ -820,7 +822,8 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(

if (lines.length > 0) {
// Process each line as a separate file path
let newValue = inputValue.slice(0, cursorPosition)
const safeInputValue = inputValue ?? ""
let newValue = safeInputValue.slice(0, cursorPosition)
let totalLength = 0

// Using a standard for loop instead of forEach for potential performance gains.
Expand All @@ -839,7 +842,7 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
}

// Add space after the last mention and append the rest of the input
newValue += " " + inputValue.slice(cursorPosition)
newValue += " " + safeInputValue.slice(cursorPosition)
totalLength += 1

setInputValue(newValue)
Expand Down
Loading