Skip to content

Commit

Permalink
Prevent sending empty message (#126)
Browse files Browse the repository at this point in the history
* Don't send empty message

* Same condition to prevent sending message with keyboard or button

Co-authored-by: david qiu <[email protected]>

* Do not disable the 'cancel edition' button

---------

Co-authored-by: david qiu <[email protected]>
  • Loading branch information
brichet and dlqqq authored Dec 18, 2024
1 parent afe699f commit 4b0bbdc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
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

0 comments on commit 4b0bbdc

Please sign in to comment.