Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent sending empty message #126

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Changes from 1 commit
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
14 changes: 10 additions & 4 deletions packages/jupyter-chat/src/components/chat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,22 @@ export function ChatInput(props: ChatInput.IProps): JSX.Element {
return;
}

// do not send the message if the user was selecting a suggested command from the
// Do not send the message if the user was selecting a suggested command from the
// Autocomplete component.
if (highlighted) {
return;
}

// Do not send empty messages, and avoid adding new line in empty message.
if (!input.trim()) {
event.stopPropagation();
event.preventDefault();
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handler only sends the message if !input.trim(), but the SendButton component is enabled by a different condition (when input.length >= 0).

<SendButton
  model={model}
  sendWithShiftEnter={sendWithShiftEnter}
  inputExists={input.length > 0}
  onSend={onSend}
  hideIncludeSelection={hideIncludeSelection}
  hasButtonOnLeft={!!props.onCancel}
/>

Currently, if the input contains only white space, the send button will be enabled (which shouldn't happen), but pressing Enter/Shift-Enter does nothing. This is a difference in behavior caused by using two separate state variables.

I recommend defining a new local variable const inputExists = !!input.trim() in the component's top scope, and using that variable in both the handler & the send button.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in b28a698


if (
event.key === 'Enter' &&
((sendWithShiftEnter && event.shiftKey) ||
(!sendWithShiftEnter && !event.shiftKey))
(sendWithShiftEnter && event.shiftKey) ||
(!sendWithShiftEnter && !event.shiftKey)
) {
onSend();
event.stopPropagation();
Expand Down
Loading