Skip to content

Commit cb5db71

Browse files
authored
Merge pull request Expensify#89898 from callstack-internal/decompose/composer-pr8-text-ownership
Composer decomposition PR8: text ownership migration
2 parents f4f0733 + e3702a3 commit cb5db71

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import ReportActionComposeFocusManager from '@libs/ReportActionComposeFocusManag
4949
import {isValidReportIDFromPath, shouldAutoFocusOnKeyPress} from '@libs/ReportUtils';
5050
import updateMultilineInputRange from '@libs/updateMultilineInputRange';
5151
import willBlurTextInputOnTapOutsideFunc from '@libs/willBlurTextInputOnTapOutside';
52+
import {useComposerActions, useComposerText} from '@pages/inbox/report/ReportActionCompose/ComposerContext';
5253
import getCursorPosition from '@pages/inbox/report/ReportActionCompose/getCursorPosition';
5354
import getScrollPosition from '@pages/inbox/report/ReportActionCompose/getScrollPosition';
5455
import type {SuggestionsRef} from '@pages/inbox/report/ReportActionCompose/ReportActionCompose';
@@ -255,16 +256,19 @@ function ComposerWithSuggestions({
255256
const cursorPositionValue = useSharedValue({x: 0, y: 0});
256257
const tag = useSharedValue(-1);
257258
const isInSidePanel = useIsInSidePanel();
258-
const [draftComment = ''] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`);
259-
const [value, setValue] = useState(() => {
260-
if (draftComment) {
261-
emojisPresentBefore.current = extractEmojis(draftComment);
259+
const value = useComposerText();
260+
const {setText} = useComposerActions();
261+
// Snapshot of provider text at mount — used for one-time selection cursor + emoji baseline + autofocus decision.
262+
// Reading the live `value` for these would cause re-renders and effect re-fires on every keystroke.
263+
const [initialText] = useState(() => {
264+
if (value) {
265+
emojisPresentBefore.current = extractEmojis(value);
262266
}
263-
return draftComment;
267+
return value;
264268
});
265269
const {accountID: currentUserAccountID} = useCurrentUserPersonalDetails();
266270

267-
const commentRef = useRef(value);
271+
const commentRef = useRef(initialText);
268272

269273
const {superWideRHPRouteKeys} = useWideRHPState();
270274
// When SearchReport is stacked above another RHP, delay autofocus until after the transition completes to avoid animation jank
@@ -276,20 +280,20 @@ function ComposerWithSuggestions({
276280
const [preferredSkinTone = CONST.EMOJI_DEFAULT_SKIN_TONE] = useOnyx(ONYXKEYS.PREFERRED_EMOJI_SKIN_TONE);
277281
const [editFocused] = useOnyx(ONYXKEYS.INPUT_FOCUSED);
278282

279-
const lastTextRef = useRef(value);
283+
const lastTextRef = useRef(initialText);
280284
useEffect(() => {
281285
lastTextRef.current = value;
282286
}, [value]);
283287

284288
const {shouldUseNarrowLayout} = useResponsiveLayout();
285289
const maxComposerLines = shouldUseNarrowLayout ? CONST.COMPOSER.MAX_LINES_SMALL_SCREEN : CONST.COMPOSER.MAX_LINES;
286-
const shouldAutoFocus = (shouldFocusInputOnScreenFocus || !!draftComment) && shouldShowComposeInput && areAllModalsHidden() && isFocused;
290+
const shouldAutoFocus = (shouldFocusInputOnScreenFocus || !!value) && shouldShowComposeInput && areAllModalsHidden() && isFocused;
287291
const delayedAutoFocusRouteKeyRef = useRef<string | null>(null);
288292

289-
const valueRef = useRef(value);
293+
const valueRef = useRef(initialText);
290294
valueRef.current = value;
291295

292-
const [selection, setSelection] = useState<TextSelection>(() => ({start: value.length, end: value.length, positionX: 0, positionY: 0}));
296+
const [selection, setSelection] = useState<TextSelection>(() => ({start: initialText.length, end: initialText.length, positionX: 0, positionY: 0}));
293297

294298
const [composerHeightAfterClear, setDefaultComposerHeight] = useState<number | null>(null);
295299
const emptyComposerHeightRef = useRef<number | null>(null);
@@ -447,7 +451,7 @@ function ComposerWithSuggestions({
447451
const newCommentConverted = convertToLTRForComposer(newComment);
448452
emojisPresentBefore.current = emojis;
449453

450-
setValue(newCommentConverted);
454+
setText(newCommentConverted);
451455
if (commentValue !== newComment) {
452456
const adjustedCursorPosition = cursorPosition !== undefined && cursorPosition !== null ? cursorPosition + textVSOffset : undefined;
453457
const position = Math.max((selection.end ?? 0) + (newComment.length - commentRef.current.length), adjustedCursorPosition ?? 0);
@@ -486,6 +490,7 @@ function ComposerWithSuggestions({
486490
selection?.end,
487491
selection?.start,
488492
currentUserAccountID,
493+
setText,
489494
],
490495
);
491496

0 commit comments

Comments
 (0)