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
Show file tree
Hide file tree
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 packages/jupyter-chat/src/components/chat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export function ChatInput(props: ChatInput.IProps): JSX.Element {
// controls whether the slash command autocomplete is open
const [open, setOpen] = useState<boolean>(false);

const inputExists = !!input.trim();

/**
* Effect: fetch the list of available autocomplete commands.
*/
Expand Down Expand Up @@ -130,16 +132,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 (!inputExists) {
event.stopPropagation();
event.preventDefault();
return;
}

if (
event.key === 'Enter' &&
((sendWithShiftEnter && event.shiftKey) ||
(!sendWithShiftEnter && !event.shiftKey))
(sendWithShiftEnter && event.shiftKey) ||
(!sendWithShiftEnter && !event.shiftKey)
) {
onSend();
event.stopPropagation();
Expand Down Expand Up @@ -224,16 +232,11 @@ ${selection.source}
...params.InputProps,
endAdornment: (
<InputAdornment position="end">
{props.onCancel && (
<CancelButton
inputExists={input.length > 0}
onCancel={onCancel}
/>
)}
{props.onCancel && <CancelButton onCancel={onCancel} />}
<SendButton
model={model}
sendWithShiftEnter={sendWithShiftEnter}
inputExists={input.length > 0}
inputExists={inputExists}
onSend={onSend}
hideIncludeSelection={hideIncludeSelection}
hasButtonOnLeft={!!props.onCancel}
Expand Down
3 changes: 0 additions & 3 deletions packages/jupyter-chat/src/components/input/cancel-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const CANCEL_BUTTON_CLASS = 'jp-chat-cancel-button';
* The cancel button props.
*/
export type CancelButtonProps = {
inputExists: boolean;
onCancel: () => void;
};

Expand All @@ -22,11 +21,9 @@ export type CancelButtonProps = {
*/
export function CancelButton(props: CancelButtonProps): JSX.Element {
const tooltip = 'Cancel edition';
const disabled = !props.inputExists;
return (
<TooltippedButton
onClick={props.onCancel}
disabled={disabled}
tooltip={tooltip}
buttonProps={{
size: 'small',
Expand Down
Loading