Composer: Notify user if message is too long

This commit is contained in:
Alexander Zinchuk 2021-08-20 23:47:17 +03:00
parent 2a9f4f7d63
commit 08e084344d
3 changed files with 10 additions and 6 deletions

View File

@ -162,6 +162,7 @@ const SCREEN_WIDTH_TO_HIDE_PLACEHOLDER = 600; // px
const MOBILE_KEYBOARD_HIDE_DELAY_MS = 100;
const SELECT_MODE_TRANSITION_MS = 200;
const MESSAGE_MAX_LENGTH = 4096;
const CAPTION_MAX_LENGTH = 1024;
const SENDING_ANIMATION_DURATION = 350;
// eslint-disable-next-line max-len
@ -474,15 +475,17 @@ const Composer: FC<OwnProps & StateProps & DispatchProps> = ({
}
const { text, entities } = parseMessageInput(htmlRef.current!);
if (!currentAttachments.length && !text && !isForwarding) {
return;
}
if (currentAttachments.length && text && text.length > CAPTION_MAX_LENGTH) {
const extraLength = text.length - CAPTION_MAX_LENGTH;
const maxLength = currentAttachments.length ? CAPTION_MAX_LENGTH : MESSAGE_MAX_LENGTH;
if (text?.length > maxLength) {
const extraLength = text.length - maxLength;
showDialog({
data: {
message: 'CAPTION_TOO_LONG_PLEASE_REMOVE_CHARACTERS',
message: 'MESSAGE_TOO_LONG_PLEASE_REMOVE_CHARACTERS',
textParams: {
'{EXTRA_CHARS_COUNT}': extraLength,
'{PLURAL_S}': extraLength > 1 ? 's' : '',
@ -490,6 +493,7 @@ const Composer: FC<OwnProps & StateProps & DispatchProps> = ({
hasErrorKey: true,
},
});
return;
}

View File

@ -16,12 +16,11 @@ const ENTITY_CLASS_BY_NODE_NAME: Record<string, string> = {
};
const MAX_TAG_DEEPNESS = 3;
const MAX_MESSAGE_LENGTH = 4096;
export default function parseMessageInput(html: string): ApiFormattedText {
const fragment = document.createElement('div');
fragment.innerHTML = parseMarkdown(html);
const text = fragment.innerText.trim().replace(/\u200b+/g, '').slice(0, MAX_MESSAGE_LENGTH);
const text = fragment.innerText.trim().replace(/\u200b+/g, '');
let textIndex = 0;
let recursionDeepness = 0;
const entities: ApiMessageEntity[] = [];

View File

@ -40,11 +40,12 @@ const READABLE_ERROR_MESSAGES: Record<string, string> = {
// TODO Bring back after fixing the weird bug
// CHANNEL_INVALID: 'An error occurred. Please try again later',
LINK_NOT_MODIFIED: 'This discussion is already linked to the channel',
MESSAGE_TOO_LONG: 'Message is too long',
// Non-API errors
SERVICE_WORKER_DISABLED: 'Service Worker is disabled. Please reload the page without holding <Shift> key.',
// eslint-disable-next-line max-len
CAPTION_TOO_LONG_PLEASE_REMOVE_CHARACTERS: 'The provided caption is too long. Please remove {EXTRA_CHARS_COUNT} character{PLURAL_S}.',
MESSAGE_TOO_LONG_PLEASE_REMOVE_CHARACTERS: 'The provided message is too long. Please remove {EXTRA_CHARS_COUNT} character{PLURAL_S}.',
// eslint-disable-next-line max-len
FRESH_RESET_AUTHORISATION_FORBIDDEN: 'You cant logout other sessions if less than 24 hours have passed since you logged on the current session',