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

Fix: Edit composer moves focus to main composer after press LHN menu item #24481

Merged
merged 16 commits into from
Aug 22, 2023

Conversation

tienifr
Copy link
Contributor

@tienifr tienifr commented Aug 12, 2023

Details

App loses focus on edit composer and switches to focus main composer after press LHN menu item. This PR fixes that.

Fixed Issues

$ #23898
PROPOSAL: #23898 (comment)

Tests

  1. Login with any account
  2. Go to any chat
  3. Toggle a message context menu >> Edit message
  4. Verify that the edit composer is focused
  5. Toggle LHN context menu >> Mark as unread
  6. Verify that the edit composer is focused again
  • Verify that no errors appear in the JS console

Offline tests

NA

QA Steps

  1. Login with any account
  2. Go to any chat
  3. Toggle a message context menu >> Edit message
  4. Verify that the edit composer is focused
  5. Toggle LHN context menu >> Mark as unread
  6. Verify that the edit composer is focused again
  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
Screen.Recording.2023-08-12.at.18.33.51.mov
Mobile Web - Chrome
Screen.Recording.2023-08-12.at.23.10.56.mov
Mobile Web - Safari
Screen.Recording.2023-08-12.at.23.08.51.mov
Desktop
Screen.Recording.2023-08-12.at.22.39.54.mov
iOS
Screen.Recording.2023-08-12.at.22.48.26.mov
Android
Screen.Recording.2023-08-12.at.23.10.10.mov

@tienifr tienifr marked this pull request as ready for review August 12, 2023 16:13
@tienifr tienifr requested a review from a team as a code owner August 12, 2023 16:13
@melvin-bot melvin-bot bot removed the request for review from a team August 12, 2023 16:13
@melvin-bot
Copy link

melvin-bot bot commented Aug 12, 2023

@Ollyws Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@Ollyws
Copy link
Contributor

Ollyws commented Aug 12, 2023

@tienifr Could you please add ReportActionComposeFocusManager.clear(); to ReportItemActionMessageEdit as specified in step 5 of your proposal. Thanks.

@tienifr
Copy link
Contributor Author

tienifr commented Aug 12, 2023

Thanks! I've updated.

@@ -354,6 +358,11 @@ function Composer({
updateNumberOfLines();
}, [updateNumberOfLines]);

useEffect(() => () => {
ReportActionComposeFocusManager.clear();
ReportActionComposeFocusManager.focus();
Copy link
Contributor

@Ollyws Ollyws Aug 19, 2023

Choose a reason for hiding this comment

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

Thanks for the update. Can you tell me, why is .focus() necessary here?
Also, wouldn't it be better to put this in our already existing useEffect return function?

Copy link
Contributor Author

@tienifr tienifr Aug 20, 2023

Choose a reason for hiding this comment

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

Why is .focus() necessary here?

After clearing the callback we need to re-focus on the main composer. It's the same logic just like within ReportActionItemMessageEdit's deleteDraft.

const deleteDraft = useCallback(() => {
    debouncedSaveDraft.cancel();
    Report.saveReportActionDraft(props.reportID, props.action, '');
    ComposerActions.setShouldShowComposeInput(true);
    ReportActionComposeFocusManager.clear();
    ReportActionComposeFocusManager.focus();
    ...
}

wouldn't it be better to put this in our already existing useEffect

Thanks, I've updated that!

Copy link
Contributor

Choose a reason for hiding this comment

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

@tienifr The problem with using .focus() here is that if you open two edit composers, and delete the unfocused one from another tab, the focus will be moved from the edit composer to the main composer.
The current behaviour on production is not to focus after deleting the comment from another tab so I think we can remove the .focus() here and keep the current production behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought that's what we want to fix here? That the main composer should be focused when edit composer is closed/unmounted? Whichever approach we choose should be noted so that later issues wouldn't treat it as a regression.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not that it should be focused, just that the correct callback is used when we select something from the LHN context-menu and aren't left with any stale callbacks after a comment has been deleted. Simply clearing on unmount seems to fix this as far as I can see.

Copy link
Contributor Author

@tienifr tienifr Aug 21, 2023

Choose a reason for hiding this comment

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

The main point is to check whether the main composer is being unmounted.

Solution

Check if mainComposerFocusCallback has already been cleared before, this means that the main composer has been unmounted:

if (isMainComposer) {
    mainComposerFocusCallback = null;
} else if (mainComposerFocusCallback) {
    focusCallback = null;
}

This seems like a workaround and needs more testing but is the shortest solution.

Alternative solution

  1. Use the shouldShowComposeInput prop from Onyx. However, there're more reasons to hide the main composer than just switching to edit mode. shouldShowComposeInput doesn't cover all cases of un-mounting. For example: report with disabled write actions. Also, subscribing to Onyx may introduce unexpected re-rendering and performance decrease.

{!hideComposer && (props.shouldShowComposeInput || !props.isSmallScreenWidth) && (
<View style={[chatFooterStyles, props.isComposerFullSize && styles.chatFooterFullCompose]}>
<SwipeableView onSwipeDown={Keyboard.dismiss}>
<ReportActionCompose

  1. Another solution is to introduce a prop called isMainComposer to Composer component. We only clear if it's an edit composer.

I've already pushed the changes for the first solution. Please take a look and leave some feedback! @Ollyws

Copy link
Contributor

@Ollyws Ollyws Aug 21, 2023

Choose a reason for hiding this comment

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

@tienifr So we'd only be clearing the edit composer callback when the main composer is visible? Won't this cause a problem if we deleted an edit message from another tab on mWeb? (while the main composer is hidden)
Edit: Actually, seems to auto-focus the main composer when we do that on mWeb. Let me test this further.

Copy link
Contributor Author

@tienifr tienifr Aug 21, 2023

Choose a reason for hiding this comment

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

Edited: Imma put the second alternative solution to main solution since it's short and not prone to regressions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that does sound like a simpler solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please take a look.

ReportActionComposeFocusManager.clear();
ReportActionComposeFocusManager.focus();
}, []);
useEffect(
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this still here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My fault!!

@@ -159,6 +161,8 @@ function Composer({
}) {
const textRef = useRef(null);
const textInput = useRef(null);
const willBlurTextInputOnTapOutsideRef = useRef();
Copy link
Contributor

Choose a reason for hiding this comment

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

Just out of curiosity, why did you change this to a ref?

Copy link
Contributor Author

@tienifr tienifr Aug 20, 2023

Choose a reason for hiding this comment

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

I think it's better to just directly call the function instead of creating the ref, right? I've updated that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah we do it that way in reportActionCompose.

Copy link
Contributor

@Ollyws Ollyws Aug 20, 2023

Choose a reason for hiding this comment

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

But do we even need it allall seeing as we are only adding this to composer on web, and willBlurTextInputOnTapOutside only returns true on web. Correct me if I'm wrong but won't it return true 100% of the time here?

Copy link
Contributor Author

@tienifr tienifr Aug 20, 2023

Choose a reason for hiding this comment

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

Agree! I was trying to replicate the conditions from ReportActionCompose without noticing that.

@@ -2,16 +2,23 @@ import _ from 'underscore';
import React from 'react';

const composerRef = React.createRef();
// There are two types of composer: general composer (edit composer) and main composer
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// There are two types of composer: general composer (edit composer) and main composer
// There are two types of composer: general composer (edit composer) and main composer.
// The general composer callback will take priority if it exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@Ollyws
Copy link
Contributor

Ollyws commented Aug 21, 2023

I have found one problem, although it is quite strange and I'm not sure it's in scope for this issue.
It only happens on Android Chrome, other android browsers it works fine.
If you: edit comment > long-press another comment on report-screen > select an item sometimes the composer will not appear to be focused, although in the console it says it's being focused then immediately blurred.
What's even more odd is that if I navigate using the Chrome devtools it doesn't happen at all:

VIDEO
Screen.Recording.2023-08-21.at.23.23.26.mov
It seems like possibly a bug in Chrome itself, any insight into this @tienifr ?

@Ollyws
Copy link
Contributor

Ollyws commented Aug 22, 2023

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
MacOS_Chrome.mov
Mobile Web - Chrome
Android_Chrome.mov
Mobile Web - Safari
iOS_Safari.mov
Desktop
MacOS_Desktop.mov
iOS
iOS_Native.mov
Android
Android_Native.mov

Copy link
Contributor

@Ollyws Ollyws left a comment

Choose a reason for hiding this comment

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

Aside from #24481 (comment) which I think is beyond the scope of this PR, changes LGTM.

@melvin-bot melvin-bot bot requested a review from robertjchen August 22, 2023 09:37
@tienifr
Copy link
Contributor Author

tienifr commented Aug 22, 2023

Sorry, I've been investigating that but had no clue why it got blurred.

Copy link
Contributor

@robertjchen robertjchen left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@robertjchen robertjchen merged commit 893c2a5 into Expensify:main Aug 22, 2023
@OSBotify
Copy link
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/robertjchen in version: 1.3.57-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@@ -448,6 +457,18 @@ function Composer({
numberOfLines={numberOfLines}
disabled={isDisabled}
onKeyPress={handleKeyPress}
onFocus={(e) => {
ReportActionComposeFocusManager.onComposerFocus(() => {
Copy link
Member

@parasharrajat parasharrajat Aug 27, 2023

Choose a reason for hiding this comment

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

We shouldn't be using this onComposerFocus here. The composer component does seem related to the composer only but it is a standalone component. This logic adds a side-effect that will become hard to maintain over time.

There is no main or general composer inside composer/index.js. We handled this inside composer/index.js (web variant) but what about the native variant?

Can you optimize this solution keeping scalability in mind?

Can we handle this ReportActionCompose instead given that we allow onFocus prop as well as forward ref to inner textinput?

cc: @Ollyws

Copy link
Contributor

@Ollyws Ollyws Aug 30, 2023

Choose a reason for hiding this comment

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

Thanks for the feedback. Yes good point, we could pass the function to Composer from ReportActionCompose but we would also need to pass it in ReportActionItemMessageEdit.

Copy link
Member

Choose a reason for hiding this comment

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

Yes. Do you think we should do this @Ollyws? if not, why not.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see why not, but we should make sure to check willBlurTextInputOnTapOutside() so it's not run on native.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I thought we should handle the refactor in #25892, right? I'll update my proposal to handle that.

Copy link
Member

Choose a reason for hiding this comment

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

That works for me.

Comment on lines 30 to 37
if (!_.isFunction(focusCallback)) {
if (!_.isFunction(mainComposerFocusCallback)) {
return;
}

mainComposerFocusCallback();
return;
}
Copy link
Member

@parasharrajat parasharrajat Aug 27, 2023

Choose a reason for hiding this comment

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

if (!_.isFunction(focusCallback) && !_.isFunction(mainComposerFocusCallback)) {
     return;
   }
if (_.isFunction(mainComposerFocusCallback)) {	
     mainComposerFocusCallback();
     return;
   }
   focusCallback();

This seems more readable to me.

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/robertjchen in version: 1.3.58-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/luacmartins in version: 1.3.57-6 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/luacmartins in version: 1.3.58-5 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants