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

[HOLD for payment 2023-12-12] [$500] mWeb/Safari- Chat - Focus is in the start of the message when opening a report with a draft message #31708

Closed
1 of 6 tasks
izarutskaya opened this issue Nov 22, 2023 · 22 comments
Assignees
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 External Added to denote the issue can be worked on by a contributor

Comments

@izarutskaya
Copy link

izarutskaya commented Nov 22, 2023

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com to request to join our Slack channel!


Issue found when executing PR: #31295

Version Number: 1.4.2-0
Reproducible in staging?: Y
Reproducible in production?: Y
If this was caught during regression testing, add the test name, ID and link from TestRail:
Email or phone of affected tester (no customers):
Logs: https://stackoverflow.com/c/expensify/questions/4856
Expensify/Expensify Issue URL:
Issue reported by: Applause-Internal Team
Slack conversation: @

Action Performed:

  1. Open any chat and enter a message without sending it
  2. Go back to LHN and open the same chat

Expected Result:

The focus is on the end of the message so user can continue typing

Actual Result:

The focus is on the start of the message. User needs to manually move the cursor to the end to continue

Workaround:

Unknown

Platforms:

Which of our officially supported platforms is this issue occurring on?

  • Android: Native
  • Android: mWeb Chrome
  • iOS: Native
  • iOS: mWeb Safari
  • MacOS: Chrome / Safari
  • MacOS: Desktop

Screenshots/Videos

Bug6287152_1700658449599.RPReplay_Final1700650930.mp4

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~01fc3a996f2399bc80
  • Upwork Job ID: 1727336945085743104
  • Last Price Increase: 2023-11-22
  • Automatic offers:
    • bernhardoj | Contributor | 27867013
@izarutskaya izarutskaya added External Added to denote the issue can be worked on by a contributor Daily KSv2 Bug Something is broken. Auto assigns a BugZero manager. labels Nov 22, 2023
@melvin-bot melvin-bot bot changed the title mWeb/Safari- Chat - Focus is in the start of the message when opening a report with a draft message [$500] mWeb/Safari- Chat - Focus is in the start of the message when opening a report with a draft message Nov 22, 2023
Copy link

melvin-bot bot commented Nov 22, 2023

Triggered auto assignment to @conorpendergrast (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details.

Copy link

melvin-bot bot commented Nov 22, 2023

Job added to Upwork: https://www.upwork.com/jobs/~01fc3a996f2399bc80

Copy link

melvin-bot bot commented Nov 22, 2023

Bug0 Triage Checklist (Main S/O)

  • This "bug" occurs on a supported platform (ensure Platforms in OP are ✅)
  • This bug is not a duplicate report (check E/App issues and #expensify-bugs)
    • If it is, comment with a link to the original report, close the issue and add any novel details to the original issue instead
  • This bug is reproducible using the reproduction steps in the OP. S/O
    • If the reproduction steps are clear and you're unable to reproduce the bug, check with the reporter and QA first, then close the issue.
    • If the reproduction steps aren't clear and you determine the correct steps, please update the OP.
  • This issue is filled out as thoroughly and clearly as possible
    • Pay special attention to the title, results, platforms where the bug occurs, and if the bug happens on staging/production.
  • I have reviewed and subscribed to the linked Slack conversation to ensure Slack/Github stay in sync

@melvin-bot melvin-bot bot added the Help Wanted Apply this label when an issue is open to proposals by contributors label Nov 22, 2023
Copy link

melvin-bot bot commented Nov 22, 2023

Triggered auto assignment to Contributor-plus team member for initial proposal review - @burczu (External)

@PiyushChandra17
Copy link

@izarutskaya Not reproducible on latest main

@erquhart
Copy link
Contributor

erquhart commented Nov 22, 2023

Proposal

Please re-state the problem that we are trying to solve in this issue.

For mobile Safari, when a draft message is opened, the cursor is at the beginning of the message instead of the end.

What is the root cause of that problem?

The only time we don't set the initial selection is for mobile Safari if shouldAutoFocus is truthy, otherwise we always set the initial selction.

const shouldAutoFocus = !modal.isVisible && (shouldFocusInputOnScreenFocus || (isEmptyChat && !ReportActionsUtils.isTransactionThread(parentAction))) && shouldShowComposeInput;
const valueRef = useRef(value);
valueRef.current = value;
const [selection, setSelection] = useState(() => ({
start: isMobileSafari && !shouldAutoFocus ? 0 : value.length,
end: isMobileSafari && !shouldAutoFocus ? 0 : value.length,
}));

But we're not using shouldAutoFocus to set focus, we're intentionally setting it to false for mobile:

// We want consistent auto focus behavior on input between native and mWeb so we have some auto focus management code that will
// prevent auto focus on existing chat for mobile device
const shouldFocusInputOnScreenFocus = canFocusInputOnScreenFocus();

What changes do you think we should make in order to solve the problem?

Remove the conditionals from the initial selection state - we should always set it. It won't have impact if the the composer isn't focused, so any dependency between autofocus logic and initial selection state is just added complexity.

const [selection, setSelection] = useState({ start: value.length, end: value.length })

@bernhardoj
Copy link
Contributor

Proposal

Please re-state the problem that we are trying to solve in this issue.

If we have a composer draft in a chat with existing messages and reopen that chat, the composer will auto-focus and put the cursor at the front of the input text.

What is the root cause of that problem?

When we open a chat with existing messages on a mobile screen, the autofocus shouldn't work (it fails on isEmptyChat).

const shouldAutoFocus = !modal.isVisible && (shouldFocusInputOnScreenFocus || (isEmptyChat && !ReportActionsUtils.isTransactionThread(parentAction))) && shouldShowComposeInput;

And if autofocus is false, we will set the input selection to 0, only on Safari.

const [selection, setSelection] = useState(() => ({
start: isMobileSafari && !shouldAutoFocus ? 0 : value.length,
end: isMobileSafari && !shouldAutoFocus ? 0 : value.length,
}));

It is added in this PR #19293 to fix #15105.

The reason we want to set it to 0 if autofocus is false is because if we set it to > 0, the input will be autofocused regardless of the autofocus attribute and we want to avoid that.

The selection here is 0 and we pass it to Composer, but somehow the composer is still autofocused and it is because, in the Composer component, we have a selection state that has an initial value of the draft length.

const [selection, setSelection] = useState({
start: initialValue.length,
end: initialValue.length,
});

Thus, the composer is autofocused. Then, we have a useEffect that will update the selection state if selection prop is changed.

useEffect(() => {
setSelection((prevSelection) => {
if (!!prevSelection && selectionProp.start === prevSelection.start && selectionProp.end === prevSelection.end) {
return;
}
return selectionProp;
});
}, [selectionProp]);

In our case, the selection props is 0.

There is also another code that caused the unwanted autofocus.

useEffect(() => {
// Scrolls the composer to the bottom and sets the selection to the end, so that longer drafts are easier to edit
updateMultilineInputRange(textInputRef.current, shouldAutoFocus);

The code above will (run on mount) set the selection to the end of the composer and scroll the composer to the bottom. This means we have 2 selection logic, 1 with the fix of #15105 (with a state), and 1 without the fix (with a native method).

That's why the composer is always autofocused and the selection is at the start of the text.

What changes do you think we should make in order to solve the problem?

First, we should move the mWeb Safari fix to updateMultilineInputRange. In updateMultilineInputRange, we already pass shouldAutoFocus, so we can use the same logic as in ComposerWithSuggestion, that is don't set the selection if it's an mWeb Safari and the autofocus is false.

const shouldSetSelection = !(Browser.isMobileSafari() && !shouldAutoFocus);
if (shouldSetSelection) {
    input.setSelectionRange(length, length);
}

Now, update the selection initial value in ComposerWithSuggestion to 0.

const [selection, setSelection] = useState(() => ({start: 0, end: 0}));

Next, initialize the selection state in Composer based on the selection prop.

const [selection, setSelection] = useState({
    start: selectionProp.start,
    end: selectionProp.end,
});

This will make sure the input won't be autofocused when it shouldn't (the real issue) on mWeb Safari but the input is still scrolled to the bottom.

@conorpendergrast
Copy link
Contributor

I have reproduced on iOS, in Safari 👍

@melvin-bot melvin-bot bot added the Overdue label Nov 27, 2023
Copy link

melvin-bot bot commented Nov 27, 2023

@conorpendergrast, @burczu Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

@burczu
Copy link
Contributor

burczu commented Nov 27, 2023

Not overdue, I'll be reviewing this issue soon.

@melvin-bot melvin-bot bot removed the Overdue label Nov 27, 2023
@burczu
Copy link
Contributor

burczu commented Nov 28, 2023

First of all, I've checked how this works on Android mWeb and it does not focus the input when we get back to the chat that is in edit mode, so I think this is the expected behaviour of this issue (not putting the cursor at the end, just not focusing the input at all).

That being said, the solution from @bernhardoj solves the issue this way and make the app on iOS and Android mWeb working consistently, so I believe we should proceed with it.

The solution from @erquhart indeed moves the cursor at the end, but only on iOS and does not change the behaviour on Android so the outcome is inconsistent on both devices.

🎀 👀 🎀 C+ reviewed

Copy link

melvin-bot bot commented Nov 28, 2023

Triggered auto assignment to @dangrous, see https://stackoverflow.com/c/expensify/questions/7972 for more details.

@dangrous
Copy link
Contributor

This sounds good to me! That conditional (!(Browser.isMobileSafari() && !shouldAutoFocus);)is a bit confusing to parse through so please add a comment there. Will assign you now!

@melvin-bot melvin-bot bot removed the Help Wanted Apply this label when an issue is open to proposals by contributors label Nov 28, 2023
Copy link

melvin-bot bot commented Nov 28, 2023

📣 @bernhardoj 🎉 An offer has been automatically sent to your Upwork account for the Contributor role 🎉 Thanks for contributing to the Expensify app!

Offer link
Upwork job
Please accept the offer and leave a comment on the Github issue letting us know when we can expect a PR to be ready for review 🧑‍💻
Keep in mind: Code of Conduct | Contributing 📖

@melvin-bot melvin-bot bot added Reviewing Has a PR in review Weekly KSv2 and removed Daily KSv2 labels Nov 29, 2023
@bernhardoj
Copy link
Contributor

PR is ready

cc: @burczu

@melvin-bot melvin-bot bot added Weekly KSv2 Awaiting Payment Auto-added when associated PR is deployed to production and removed Weekly KSv2 labels Dec 5, 2023
@melvin-bot melvin-bot bot changed the title [$500] mWeb/Safari- Chat - Focus is in the start of the message when opening a report with a draft message [HOLD for payment 2023-12-12] [$500] mWeb/Safari- Chat - Focus is in the start of the message when opening a report with a draft message Dec 5, 2023
@melvin-bot melvin-bot bot removed the Reviewing Has a PR in review label Dec 5, 2023
Copy link

melvin-bot bot commented Dec 5, 2023

Reviewing label has been removed, please complete the "BugZero Checklist".

Copy link

melvin-bot bot commented Dec 5, 2023

The solution for this issue has been 🚀 deployed to production 🚀 in version 1.4.7-4 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

If no regressions arise, payment will be issued on 2023-12-12. 🎊

After the hold period is over and BZ checklist items are completed, please complete any of the applicable payments for this issue, and check them off once done.

  • External issue reporter
  • Contributor that fixed the issue
  • Contributor+ that helped on the issue and/or PR

For reference, here are some details about the assignees on this issue:

Copy link

melvin-bot bot commented Dec 5, 2023

BugZero Checklist: The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

  • [@burczu] The PR that introduced the bug has been identified. Link to the PR:
  • [@burczu] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake. Link to comment:
  • [@burczu] A discussion in #expensify-bugs has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner. Link to discussion:
  • [@burczu] Determine if we should create a regression test for this bug.
  • [@burczu] If we decide to create a regression test for the bug, please propose the regression test steps to ensure the same bug will not reach production again.
  • [@conorpendergrast] Link the GH issue for creating/updating the regression test once above steps have been agreed upon:

@burczu
Copy link
Contributor

burczu commented Dec 11, 2023

BugZero Checklist: The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

  • [@burczu] The PR that introduced the bug has been identified. Link to the PR:

#21371

  • [@burczu] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake. Link to comment:

#21371 (comment)

  • [@burczu] A discussion in #expensify-bugs has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner. Link to discussion:

n/a

  • [@burczu] Determine if we should create a regression test for this bug.

I'm not sure about regression test here - it didn't block the App from being usable, it was just a small inconvenience only in one of the environments we support.

  • [@burczu] If we decide to create a regression test for the bug, please propose the regression test steps to ensure the same bug will not reach production again.
  • [@conorpendergrast] Link the GH issue for creating/updating the regression test once above steps have been agreed upon:

@melvin-bot melvin-bot bot added Daily KSv2 and removed Weekly KSv2 labels Dec 11, 2023
@conorpendergrast
Copy link
Contributor

@burczu This was a bug identified by Applause, so we likely don't need a new regression test. Thanks!

@conorpendergrast
Copy link
Contributor

Payouts due:

Upwork job is here.

@conorpendergrast
Copy link
Contributor

All done, thanks everyone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 External Added to denote the issue can be worked on by a contributor
Projects
None yet
Development

No branches or pull requests

7 participants