Skip to content

[$500] mWeb/Safari - Chat - After removing an emoji and adding a new, the focus shifts the top of the page #31594

Closed

Description

@lanitochka17

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


Version Number: 1.4.1.4
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. Navigate to https://staging.new.expensify.com/
  2. Open any 1:1 chat
  3. Insert 3-4 emojis
  4. Edit this message, remove the last emoji and add a new one

Expected Result:

The focus should stay on the edited message

Actual Result:

After removing an emoji and adding a new, the focus shifts to the top of the page and it shakes once.
On prod, the focus only moves up a little bit and the shake isn't present

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

Add any screenshot/video evidence

Bug6284665_1700520608709.Top_focus.1.mp4

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~0124d5bed6c31044e5
  • Upwork Job ID: 1726740264760147968
  • Last Price Increase: 2023-12-11

Activity

  1. added
    External
    Daily
    Bug
    on Nov 20, 2023
  2. changed the title [-]mWeb/Safari - Chat - After removing an emoji and adding a new, the focus shifts the top of the page[/-] [+][$500] mWeb/Safari - Chat - After removing an emoji and adding a new, the focus shifts the top of the page[/+] on Nov 20, 2023
  3. melvin-bot commented on Nov 20, 2023

    @melvin-bot

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

  4. melvin-bot commented on Nov 20, 2023

    @melvin-bot
  5. added
    Help Wanted
    on Nov 20, 2023
  6. melvin-bot commented on Nov 20, 2023

    @melvin-bot

    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
  7. melvin-bot commented on Nov 20, 2023

    @melvin-bot

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

  8. wlegolas commented on Nov 21, 2023

    @wlegolas
    Contributor

    Proposal

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

    mWeb/Safari - Chat - After removing an emoji and adding a new, the focus shifts the top of the page

    What is the root cause of that problem?

    When the user edits a comment and opens the Emoji picker, closes this picker (selecting one emoji or just clicking outside of the picker), and saves the comment, the Flatlist calculates its height according to the current view, at this moment the Composer component is hidden and the Flatlist calculates its height without the space where Composer should be visible. That is the reason the scroll changed when finishing the comment edition because the difference in pixels between the scroll before and after editing the comment is equal to the Compose's height.

    When finishing the edition, the method deleteDraft is called to act to draft the comment being edited. In this method, there is a condition to check if the user is editing the last message, if so, an event listener will be added to monitoring the event keyboardDidHide in the Keyboard object. But adding an event listener to the Keyboard object will only work on native devices, in this case, we are using a Mobile Browser.

    if (props.index === 0) {
    const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => {
    reportScrollManager.scrollToIndex(props.index, false);
    keyboardDidHideListener.remove();
    });
    }

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

    We should change the method deleteDraft in the ReportActionItemMessageEdit component to call the method ComposerActions.setShouldShowComposeInput passing the value true to show the ReportActionCompose before updating the Onyx state with an empty draft message.

    Showing the Composer component before updating the Onyx state will keep the scroll inside the Flatlist in the current position because now the Flatlist will calculate its height without the Composer height.

    For example,

    /**
     * Delete the draft of the comment being edited. This will take the comment out of "edit mode" with the old content.
     */
    const deleteDraft = useCallback(() => {
        InputFocus.callback(() => setIsFocused(false));
        InputFocus.inputFocusChange(false);
        debouncedSaveDraft.cancel();
        ComposerActions.setShouldShowComposeInput(true); // <- Here we should show the Componse
        Report.saveReportActionDraft(props.reportID, props.action, '');
    
        if (isActive()) {
            ReportActionComposeFocusManager.clear();
            ReportActionComposeFocusManager.focus();
        }
    
        // Scroll to the last comment after editing to make sure the whole comment is clearly visible in the report.
        if (props.index === 0) {
            const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => {
                reportScrollManager.scrollToIndex(props.index, false);
                keyboardDidHideListener.remove();
            });
        }
    }, [props.action, debouncedSaveDraft, props.reportID, isActive]);

    Note: There is a delay setTimeout just to wait for the Report Screen to be updated.

    What alternative solutions did you explore? (Optional)

    N/A

    POC

    issue-31594-with-fix.mp4
  9. hoangzinh commented on Nov 22, 2023

    @hoangzinh
    Contributor

    @wlegolas Thanks for your proposal. In your RCA, do you know why this issue is only reproducible with emoji only, but not text messages?

  10. wlegolas commented on Nov 22, 2023

    @wlegolas
    Contributor

    Hi @hoangzinh thank you for reviewing my proposal.

    About your question, In my analysis, I understood the problem is only related when we edit the message and add a new emoji because when the emoji modal is shown the component's height changes and the position changes in the scroll view.

    If you edit the emoji only removing information and saving you'll see that the strange behavior won't occur but if you edit the emoji removing and adding another one, the strange behavior will occur. In the video below you can see when the emoji modal opens in the messages in the background change their position.

    evidence-component-height.mp4
  11. 12 remaining items

  12. melvin-bot commented on Dec 4, 2023

    @melvin-bot

    📣 It's been a week! Do we have any satisfactory proposals yet? Do we need to adjust the bounty for this issue? 💸

  13. ikevin127 commented on Dec 4, 2023

    @ikevin127
    Contributor

    My proposal from #32217 issue which I'm pretty sure has the same RC.

    Proposal

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

    After editing comment emoji by adding adding a new, the scroll view shifts to the top of the page and it shakes once.

    What is the root cause of that problem?

    // Scroll to the last comment after editing to make sure the whole comment is clearly visible in the report.
    if (props.index === 0) {
    const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => {
    reportScrollManager.scrollToIndex(props.index, false);
    keyboardDidHideListener.remove();
    });
    }
    }, [props.action, debouncedSaveDraft, props.index, props.reportID, reportScrollManager, isActive]);

    We have 2 problems:

    1. When props.index === 0, scrollToIndex() is called too soon and doesn't have any effect in scrolling the edited comment into the viewport - the scroll view jumps to the top of the scroll list.

    In the code block above we have logic for scrolling to the latest comment upon editing but this does not account for editing a message that is at an index different than 0, which is the case with this issue because we're editing a previous comment that's higher in the report.

    The fact that we don't account for props.index !== 0 to scroll to the message, and also the fact that after editing and saving message the app sometimes toggles list refresh, any of these two or each separately, will make the scroll jump up and hide part of the conversation.

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

    Since the expected behaviour isn't clear as to where we want to position the scroll with regards to the just edited comment, here's what I propose:

    My solution relies on using react native's flatlist scrollToIndex() method's viewPosition prop in our existing implementation of useReportScrollManager() -> scrollToIndex() so that we can pass one of the 3 values 0, 0.5 or 1 to scroll the view focusing the edited comment according to the following schema:

    viewPosition - Values schema
    // viewPosition: 0
    +--------------------------------+
    |          Viewable Area          |
    +--------------------------------+
    |                                |
    |                                |
    |                                |
    |                                |
    |                                |
    |                                |
    | +-----------------------------+|
    |   edited comment index         |
    | +-----------------------------+|
    +--------------------------------+
    
    // viewPosition: 0.5
    +--------------------------------+
    |          Viewable Area          |
    +--------------------------------+
    |                                |
    |                                |
    |                                |
    | +-----------------------------+|
    |   edited comment index         |
    | +-----------------------------+|
    |                                |
    |                                |
    |                                |
    +--------------------------------+
    
    // viewPosition: 1
    +--------------------------------+
    |          Viewable Area          |
    +--------------------------------+
    | +-----------------------------+|
    |   edited comment index         |
    | +-----------------------------+|
    |                                |
    |                                |
    |                                |
    |                                |
    |                                |
    |                                |
    +--------------------------------+
    

    The idea is that if we do:

    • viewPosition: 0 - this will scroll the view such that edited comment is the last in the scrolled view (latest message of report will likely be hidden lower in scroll view)
    • viewPosition: 0.5 - this will scroll the view such that edited comment is in the middle the view (latest message of report will likely be hidden lower in scroll view)
    • viewPosition: 1 - this will scroll the view such that edited comment is at the top of the view (latest message of report will likely be seen if there's not a lot of of messages between last and edited comments OR will likely be hidden lower in scroll view if there are a lot of messages between the last and the edited message)

    Say in this case if we decide to go with the viewPosition: 1 value, the result can be observed below for the 2nd problem mentioned in RCA.

    For both the first and second problems from RCA to be fixed we need to delay calling scrollToIndex() with some form of timeout in order for the scroll to be effective.

    Video from #32217

    iOS: mWeb Safari
    Screen.Recording.2023-11-30.at.16.47.27.mov
  14. melvin-bot commented on Dec 4, 2023

    @melvin-bot

    @hoangzinh @CortneyOfstad this issue was created 2 weeks ago. Are we close to approving a proposal? If not, what's blocking us from getting this issue assigned? Don't hesitate to create a thread in #expensify-open-source to align faster in real time. Thanks!

  15. wlegolas commented on Dec 5, 2023

    @wlegolas
    Contributor

    @wlegolas your solution doesn't work if we're editing a comment that is not the last comment. I.e before the last comment

    I think we should step back and understand clearly the root cause first, I also reproduced the issue with

    1. Editing a text comment only
    2. Then click open Emoji
    3. Then close Emoji modal
    4. Click save
      Hi @hoangzinh you're right, if we follow your steps above the problem will occur. Sorry for not giving a solution before with the fix for this behavior I focused only on the current behavior when editing the last comment and adding a new emoji.

    I could have the same problem following the steps above in my local, here's a video with the behavior.

    issue-31594-wrong-behavior.mp4

    In my proposal, I said in the alternative solutions section that we can use the method setShouldShowComposeInput from the ComposerActions to show the composer component before the Flatlist calculates its height, but in my proposal was only related to the last comment.

    I'll update my proposal to call the method setShouldShowComposeInput before saving the Report draft. We can fix this behavior for any comment in the Flatlist with this change.

    issue-31594-with-fix.mp4
  16. wlegolas commented on Dec 5, 2023

    @wlegolas
    Contributor

    Proposal

    Updated

  17. CortneyOfstad commented on Dec 6, 2023

    @CortneyOfstad
    Contributor

    @hoangzinh thoughts on the proposals above ^^^? Thanks!

  18. hoangzinh commented on Dec 7, 2023

    @hoangzinh
    Contributor

    ah yeah @CortneyOfstad. Sorry everyone, this issue is not easy. I took time to go through each proposals and try to understand them. Gonna put updates soon.

  19. hoangzinh commented on Dec 10, 2023

    @hoangzinh
    Contributor

    @ikevin127 Thanks for your proposal. I don't think your RCA is correct. I tried to edit a message that is at an index different than 0 with text only, this bug does not occur.

    Screen.Recording.2023-12-10.at.20.30.35.mov
  20. hoangzinh commented on Dec 10, 2023

    @hoangzinh
    Contributor

    @wlegolas @ikevin127 I believe this bug happens only in mWeb Safari. Both of your proposals haven't explained why it happens only in mWeb Safari, but not mWeb Chrome.

    Screen.Recording.2023-12-10.at.20.12.10.mov
  21. melvin-bot commented on Dec 11, 2023

    @melvin-bot

    @hoangzinh @CortneyOfstad this issue is now 3 weeks old. There is one more week left before this issue breaks WAQ and will need to go internal. What needs to happen to get a PR in review this week? Please create a thread in #expensify-open-source to discuss. Thanks!

  22. melvin-bot commented on Dec 11, 2023

    @melvin-bot

    📣 It's been a week! Do we have any satisfactory proposals yet? Do we need to adjust the bounty for this issue? 💸

  23. ikevin127 commented on Dec 11, 2023

    @ikevin127
    Contributor

    I don't think your RCA is correct. I tried to edit a message that is at an index different than 0 with text only, this bug does not occur.

    The catch is to open the Emoji picker, as soon as you do that and save the edited comment, the scroll jumps - you don't even need to change anything while editing the comment, just opening the emoji picker is enough to make the scroll jump upon saving the edited comment when the FlatList layout size is updated.

    I believe this bug happens only in mWeb Safari. Both of your proposals haven't explained why it happens only in mWeb Safari, but not mWeb Chrome.

    It does happen on Chrome too, not just Safari. I tried with both real device and simulator and it's consistent between browsers.

    To be more specific on what causes the scroll to jump:

    Flatlist / emoji picker interactions

    _

    In React Native, a FlatList component uses a ScrollView internally to manage its layout and scrolling. When an external component like the emoji picker modal appears and is later closed, here's a high-level overview of what happens:

    1. Modal Presentation: When the emoji picker modal is presented, especially if it's a half-screen modal that hovers above the main content, it typically does not immediately affect the underlying FlatList scrolling position. The modal is overlaid on top of the current screen's content, and as such, the FlatList sits in the background unaltered.

    2. Layout Change: If the modal affects the layout by displacing or resizing the main content (such as when triggering adjustments for the virtual keyboard), this can disrupt the layout of FlatList. For example, the keyboard appearing typically causes the screen to resize to fit the remaining space, which would trigger a layout change on the FlatList.

    3. Content Offset Preservation: React Native's ScrollView (and by extension FlatList) normally tries to preserve the content offset when a layout change occurs. In other words, it endeavors to keep the same content in view despite the change in screen real estate. However, this isn't always perfect, especially if the size of the items in the list changes or the list is dynamically loaded.

    4. Modal Dismissal: Once a modal is dismissed, any previously applied layout constraints are removed. If the modal presented the keyboard and this affects the FlatList, dismissing the modal and hiding the keyboard would trigger another layout change as the screen returns to its full height. The FlatList would attempt to restore the scroll position as it was before the modal was presented.

    5. Potential Scroll Issues: Despite React Native's efforts to preserve scroll position, there can be cases where the scroll position "jumps" or doesn't return exactly to the previous state. This behavior can be due to a number of factors, including dynamic content resizing, incomplete state updates before the layout change, or asynchronous events like network loads that alter the list content during the layout transition.

    Considering all these, we also have the focus / blur of the edit composer which when emoji modal is opened from the edit composer, the composer is blurred which causes the main report composer to be rendered back into the FlatList, and when closing the emoji picker the edit composer focuses again which causes the main report composer to hide then when saving the edited comment, the main report composer to be rendered back into the FlatList again.

    So lots of layout changes within the Flatlist but the main culprit is the emoji picker.

    React Native FlatList doesn't have built-in specific handling for when modals appear or disappear; it simply responds to the resulting layout changes. If encountering scroll position issues after an emoji picker modal is dismissed, we may need to manually handle scroll position preservation and restoration, often involving setting variables that track necessary offsets or selected item indices and then executing scroll actions at appropriate times within the component lifecycle.
    _

  24. CortneyOfstad commented on Dec 12, 2023

    @CortneyOfstad
    Contributor

    Hey! Sorry for the confusion here, but we just got an internal update. Per this post, we can close any bugs that are not related to Waves, so closing this out 👍 We will re-address once the waves are completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugDailyEngineeringExternalHelp Wanted

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions