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

Add prompt before sending invalid commands #1919

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/app/components/editor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@ export const isEmptyEditor = (editor: Editor): boolean => {
return false;
};

export const getAttemptedCommand = (editor: Editor): string | undefined => {
const lineBlock = editor.children[0];
if (!Element.isElement(lineBlock)) return undefined;
if (lineBlock.type !== BlockType.Paragraph) return undefined;

const [firstInline] = lineBlock.children;
const isCommand = Text.isText(firstInline) && firstInline.text[0] === '/';
if (isCommand) return firstInline.text.split(' ')[0];
return undefined;
};

export const getBeginCommand = (editor: Editor): string | undefined => {
const lineBlock = editor.children[0];
if (!Element.isElement(lineBlock)) return undefined;
Expand Down
18 changes: 16 additions & 2 deletions src/app/features/room/RoomInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ import {
trimCustomHtml,
isEmptyEditor,
getBeginCommand,
getAttemptedCommand,
trimCommand,
} from '../../components/editor';
import { EmojiBoard, EmojiBoardTab } from '../../components/emoji-board';
import { UseStateProvider } from '../../components/UseStateProvider';
import { confirmDialog } from '../../molecules/confirm-dialog/ConfirmDialog';
import { TUploadContent, encryptFile, getImageInfo, getMxIdLocalPart } from '../../utils/matrix';
import { useTypingStatusUpdater } from '../../hooks/useTypingStatusUpdater';
import { useFilePicker } from '../../hooks/useFilePicker';
Expand Down Expand Up @@ -242,10 +244,21 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
contents.forEach((content) => mx.sendMessage(roomId, content));
};

const submit = useCallback(() => {
const submit = useCallback(async () => {
uploadBoardHandlers.current?.handleSend();

const commandName = getBeginCommand(editor);
const attemptedCommandName = getAttemptedCommand(editor);
if (attemptedCommandName) {
const sendAsMessage = await confirmDialog(
'Invalid Command',
`"${attemptedCommandName}" is not a valid command. Would you like to send this as a message?`,
'Send as message'
);
if (!sendAsMessage) {
ReactEditor.focus(editor);
return;
}
}

let plainText = toPlainText(editor.children).trim();
let customHtml = trimCustomHtml(
Expand All @@ -257,6 +270,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
);
let msgType = MsgType.Text;

const commandName = getBeginCommand(editor);
if (commandName) {
plainText = trimCommand(commandName, plainText);
customHtml = trimCommand(commandName, customHtml);
Expand Down