Skip to content
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
19 changes: 6 additions & 13 deletions src/hooks/useHandleExceedMaxCommentLength.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import {useCallback, useState} from 'react';
import * as ReportUtils from '@libs/ReportUtils';
import {getCommentLength} from '@libs/ReportUtils';
import type {ParsingDetails} from '@libs/ReportUtils';
import CONST from '@src/CONST';

const useHandleExceedMaxCommentLength = () => {
const [hasExceededMaxCommentLength, setHasExceededMaxCommentLength] = useState(false);

const validateCommentMaxLength = useCallback(
(value: string, parsingDetails?: ParsingDetails) => {
if (ReportUtils.getCommentLength(value, parsingDetails) <= CONST.MAX_COMMENT_LENGTH) {
if (hasExceededMaxCommentLength) {
setHasExceededMaxCommentLength(false);
}
return;
}
setHasExceededMaxCommentLength(true);
},
[hasExceededMaxCommentLength],
);
const validateCommentMaxLength = useCallback((value: string, parsingDetails?: ParsingDetails) => {
const exceeded = getCommentLength(value, parsingDetails) > CONST.MAX_COMMENT_LENGTH;
setHasExceededMaxCommentLength(exceeded);
return !exceeded;
}, []);

return {hasExceededMaxCommentLength, validateCommentMaxLength, setHasExceededMaxCommentLength};
};
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useHandleExceedMaxTaskTitleLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const useHandleExceedMaxTaskTitleLength = () => {
const validateTaskTitleMaxLength = useCallback((title: string) => {
const exceeded = title ? title.length > CONST.TITLE_CHARACTER_LIMIT : false;
setHasExceededMaxTitleLength(exceeded);
return !exceeded;
}, []);

return {hasExceededMaxTaskTitleLength, validateTaskTitleMaxLength, setHasExceededMaxTitleLength};
Expand Down
60 changes: 31 additions & 29 deletions src/pages/home/report/ReportActionCompose/ReportActionCompose.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -373,27 +373,46 @@ function ReportActionCompose({

const isSendDisabled = isCommentEmpty || isBlockedFromConcierge || !!exceededMaxLength;

const validateMaxLength = useCallback(
(value: string) => {
const taskCommentMatch = value?.match(CONST.REGEX.TASK_TITLE_WITH_OPTIONAL_SHORT_MENTION);
if (taskCommentMatch) {
const title = taskCommentMatch?.[3] ? taskCommentMatch[3].trim().replace(/\n/g, ' ') : '';
setHasExceededMaxCommentLength(false);
return validateTaskTitleMaxLength(title);
}
setHasExceededMaxTitleLength(false);
return validateCommentMaxLength(value, {reportID});
},
[setHasExceededMaxCommentLength, setHasExceededMaxTitleLength, validateTaskTitleMaxLength, validateCommentMaxLength, reportID],
);

const debouncedValidate = useMemo(() => lodashDebounce(validateMaxLength, CONST.TIMING.COMMENT_LENGTH_DEBOUNCE_TIME, {leading: true}), [validateMaxLength]);

// Note: using JS refs is not well supported in reanimated, thus we need to store the function in a shared value
// useSharedValue on web doesn't support functions, so we need to wrap it in an object.
const composerRefShared = useSharedValue<{
clear: (() => void) | undefined;
}>({clear: undefined});

const handleSendMessage = useCallback(() => {
'worklet';

const clearComposer = composerRefShared.get().clear;
if (!clearComposer) {
throw new Error('The composerRefShared.clear function is not set yet. This should never happen, and indicates a developer error.');
}

if (isSendDisabled) {
if (isSendDisabled || !debouncedValidate.flush()) {
return;
}

// This will cause onCleared to be triggered where we actually send the message
clearComposer();
}, [isSendDisabled, composerRefShared]);
runOnUI(() => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It turned out that calling flush from the UI thread throws an error on native (works fine on web). I made the validation to run on the JS thread, and then call clearComposer on the UI thread as it was before. What do you think about this change?

Screenshot Image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @andriivitiv I'm confused. Isn't debouncedValidate.flush() still be triggered at line 399 on native?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added .runOnJS(true); in SendButton.tsx‎ so it now runs on the correct thread.

'worklet';

const {clear: clearComposer} = composerRefShared.get();

if (!clearComposer) {
throw new Error('The composerRefShared.clear function is not set yet. This should never happen, and indicates a developer error.');
}

// This will cause onCleared to be triggered where we actually send the message
clearComposer?.();
})();
}, [isSendDisabled, debouncedValidate, composerRefShared]);

const measureComposer = useCallback(
(e: LayoutChangeEvent) => {
Expand Down Expand Up @@ -434,23 +453,6 @@ function ReportActionCompose({
return reportActionComposeHeight - emojiOffsetWithComposeBox - CONST.MENU_POSITION_REPORT_ACTION_COMPOSE_BOTTOM;
}, [emojiPositionValues]);

const validateMaxLength = useCallback(
(value: string) => {
const taskCommentMatch = value?.match(CONST.REGEX.TASK_TITLE_WITH_OPTIONAL_SHORT_MENTION);
if (taskCommentMatch) {
const title = taskCommentMatch?.[3] ? taskCommentMatch[3].trim().replace(/\n/g, ' ') : '';
setHasExceededMaxCommentLength(false);
validateTaskTitleMaxLength(title);
} else {
setHasExceededMaxTitleLength(false);
validateCommentMaxLength(value, {reportID});
}
},
[setHasExceededMaxCommentLength, setHasExceededMaxTitleLength, validateTaskTitleMaxLength, validateCommentMaxLength, reportID],
);

const debouncedValidate = useMemo(() => lodashDebounce(validateMaxLength, CONST.TIMING.COMMENT_LENGTH_DEBOUNCE_TIME, {leading: true}), [validateMaxLength]);

const onValueChange = useCallback(
(value: string) => {
if (value.length === 0 && isComposerFullSize) {
Expand Down Expand Up @@ -635,4 +637,4 @@ ReportActionCompose.displayName = 'ReportActionCompose';

export default memo(ReportActionCompose);
export {onSubmitAction};
export type {SuggestionsRef, ComposerRef};
export type {SuggestionsRef, ComposerRef, ReportActionComposeProps};
8 changes: 5 additions & 3 deletions src/pages/home/report/ReportActionCompose/SendButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ function SendButton({isDisabled: isDisabledProp, handleSendMessage}: SendButtonP
// We need to use isSmallScreenWidth instead of shouldUseNarrowLayout to manage GestureDetector correctly
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {isSmallScreenWidth} = useResponsiveLayout();
const Tap = Gesture.Tap().onEnd(() => {
handleSendMessage();
});
const Tap = Gesture.Tap()
.onEnd(() => {
handleSendMessage();
})
.runOnJS(true);

return (
<View
Expand Down
8 changes: 5 additions & 3 deletions src/pages/home/report/ReportActionItemMessageEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Parser from '@libs/Parser';
import ReportActionComposeFocusManager from '@libs/ReportActionComposeFocusManager';
import reportActionItemEventHandler from '@libs/ReportActionItemEventHandler';
import {getReportActionHtml, isDeletedAction} from '@libs/ReportActionsUtils';
import {getCommentLength, getOriginalReportID} from '@libs/ReportUtils';
import {getOriginalReportID} from '@libs/ReportUtils';
import setShouldShowComposeInputKeyboardAware from '@libs/setShouldShowComposeInputKeyboardAware';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -297,7 +297,7 @@ function ReportActionItemMessageEdit({
*/
const publishDraft = useCallback(() => {
// Do nothing if draft exceed the character limit
if (getCommentLength(draft, {reportID}) > CONST.MAX_COMMENT_LENGTH) {
if (!debouncedValidateCommentMaxLength.flush()) {
return;
}

Expand All @@ -311,7 +311,7 @@ function ReportActionItemMessageEdit({
}
editReportComment(originalReport, action, trimmedNewDraft, Object.fromEntries(draftMessageVideoAttributeCache), isOriginalReportArchived, isOriginalParentReportArchived);
deleteDraft();
}, [reportID, action, deleteDraft, draft, originalReportID, isOriginalReportArchived, originalReport, isOriginalParentReportArchived]);
}, [reportID, action, deleteDraft, draft, originalReportID, isOriginalReportArchived, originalReport, isOriginalParentReportArchived, debouncedValidateCommentMaxLength]);

/**
* @param emoji
Expand Down Expand Up @@ -551,6 +551,7 @@ function ReportActionItemMessageEdit({
isGroupPolicyReport={isGroupPolicyReport}
shouldCalculateCaretPosition
onScroll={onSaveScrollAndHideSuggestionMenu}
testID="composer"
/>
</View>

Expand Down Expand Up @@ -613,3 +614,4 @@ function ReportActionItemMessageEdit({
ReportActionItemMessageEdit.displayName = 'ReportActionItemMessageEdit';

export default ReportActionItemMessageEdit;
export type {ReportActionItemMessageEditProps};
Loading