Expensify / App

Welcome to New Expensify: a complete re-imagination of financial collaboration, centered around chat. Help us build the next generation of Expensify by sharing feedback and contributing to the code.
https://new.expensify.com
MIT License
3.59k stars 2.93k forks source link

[HOLD for payment 2024-12-07] [$250] The up-to-edit feature doesn't work when the message contains an embedded image. #52319

Open m-natarajan opened 3 weeks ago

m-natarajan commented 3 weeks ago

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: 9.0.59-3 Reproducible in staging?: Needs Reproduction Reproducible in production?: Needs Reproduction If this was caught on HybridApp, is this reproducible on New Expensify Standalone?: 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: @quinthar Slack conversation (hyperlinked to channel name): ts_external_expensify_bugs

Action Performed:

  1. Navigate to any chat
  2. Compose any message and add a attachment
  3. Send the message
  4. Press the up arrow on the keyboard to edit

    Expected Result:

    User able to edit the sent message

    Actual Result:

    Up to edit feature doesn't work

    Workaround:

    Unknown

    Platforms:

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

    • [ ] Android: Standalone
    • [ ] Android: HybridApp
    • [ ] Android: mWeb Chrome
    • [ ] iOS: Standalone
    • [ ] iOS: HybridApp
    • [ ] iOS: mWeb Safari
    • [x] MacOS: Chrome / Safari
    • [ ] MacOS: Desktop

Screenshots/Videos

Add any screenshot/video evidence

https://github.com/user-attachments/assets/c85f012b-ca8c-497b-9acf-142f1c5b9a4a

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~021856262688043659757
  • Upwork Job ID: 1856262688043659757
  • Last Price Increase: 2024-11-12
  • Automatic offers:
    • daledah | Contributor | 104956482
Issue OwnerCurrent Issue Owner: @jliexpensify
melvin-bot[bot] commented 3 weeks ago

Triggered auto assignment to @jliexpensify (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details. Please add this bug to a GH project, as outlined in the SO.

Shahidullah-Muffakir commented 3 weeks ago

Edited by proposal-police: This proposal was edited at 2024-11-14 08:30:02 UTC.

Proposal

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

When the last message sent in a chat includes an attachment, pressing the ArrowUp key does not open the edit box, preventing the user from editing that message

What is the root cause of that problem?

When a message with an attachment is sent, the selection.start value is set to the length of the text in that message. Because of this, the condition in the code that triggers the edit box doesn’t pass, which is why the edit box isn’t activated. hence this condition will be false: https://github.com/Expensify/App/blob/9d8571bc9e504aabbb75c690a3bca3918ed565bd/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx#L482

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

when we send the comment without or without attachment, this onClear, is called. https://github.com/Expensify/App/blob/be4d7f50949d52aa9f2cb9a8ac495ef19473edde/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx#L700

we can set the selection.start to 0 as : setSelection({ start: 0, end: 0 }); in this part: https://github.com/Expensify/App/blob/be4d7f50949d52aa9f2cb9a8ac495ef19473edde/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx#L514-L519

the setSelection(e.nativeEvent.selection), won't be executed as the !textInputRef.current?.isFocused(), will be true in the case of sending attachment.

and just to not re-rerun this setSelection({ start: 0, end: 0 }); on the onClear function twice for a text only message, we can add check if the selection.start !== 0.

if(selection.start !== 0){
setSelection({ start: 0, end: 0 });
}

or

if(!textInputRef.current?.isFocused()){
setSelection({ start: 0, end: 0 });
}

The setSelection({ start: 0, end: 0 }); is also being called in the Composer component’s clear function here: https://github.com/Expensify/App/blob/e6b416726fbe7171e84f048a8874c1b9254b6a26/src/components/Composer/implementation/index.tsx#L278 clear function in Composer component is triggered for all messages, regardless of whether they include attachments or are text-only. https://github.com/Expensify/App/blob/e6b416726fbe7171e84f048a8874c1b9254b6a26/src/components/Composer/implementation/index.tsx#L278

In clear, setSelection({ start: 0, end: 0 }) is called to reset the selection, but it only updates the local state selection within the Composer component. Since this reset is only applied to Composer's own internal selection state, it doesn’t affect the selection state in ComposerWithSuggestions. https://github.com/Expensify/App/blob/04402e9f47a77f10b3c2c47fefe0931fbf62e4d4/src/components/Composer/implementation/index.tsx#L63-L68 In ComposerWithSuggestions, we maintain a separate local selection state using which we decide to open the comment edit box if the selection.start <=0,
https://github.com/Expensify/App/blob/e6b416726fbe7171e84f048a8874c1b9254b6a26/src/components/Composer/implementation/index.tsx#L63 This means the selection reset on clear in Composer doesn’t propagate to ComposerWithSuggestions, which is why we need to explicitly update the selection state of ComposerWithSuggestions in the onClear: https://github.com/Expensify/App/blob/be4d7f50949d52aa9f2cb9a8ac495ef19473edde/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx#L700

jliexpensify commented 3 weeks ago

@Expensify/design before I make this External, do you want to review this thread and weigh in and confirm this is something we want to change? Shawn has shared thoughts here already. Thanks!

daledah commented 3 weeks ago

Edited by proposal-police: This proposal was edited at 2024-11-12 04:40:00 UTC.

Proposal

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

Up to edit feature doesn't work

What is the root cause of that problem?

When users send the message along with image, the selection should be reset to 0 (same as what we send the message only).

But we prevent to update selection if the text input is not focused, in case users send message + image, the image modal preview will show that makes the composer is blurred

https://github.com/Expensify/App/blob/64cb4ee92b1f4c66e5deb96e23caf47e25423c00/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx#L512-L522

=> the selection is not reset to 0, then the logic up-to-edit is not triggered

https://github.com/Expensify/App/blob/9d8571bc9e504aabbb75c690a3bca3918ed565bd/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx#L482

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

We should prevent suggestionsRef.current?.onSelectionChange?.(e); when the input is not focused only, since the selection is changed actually -> it should be updated

            setSelection(e.nativeEvent.selection);

            if (!textInputRef.current?.isFocused()) {
                return;
            }
            suggestionsRef.current?.onSelectionChange?.(e);

What alternative solutions did you explore? (Optional)

We can force update the selection in clear function.

Pass clear flag param in

https://github.com/Expensify/App/blob/64cb4ee92b1f4c66e5deb96e23caf47e25423c00/src/components/Composer/implementation/index.tsx#L277

Then use this flag to force update selection in https://github.com/Expensify/App/blob/64cb4ee92b1f4c66e5deb96e23caf47e25423c00/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx#L512

shawnborton commented 3 weeks ago

I think it makes sense to fix this, yes. Basically whether a message has an image or not shouldn't change your ability to hit the up arrow to quickly edit it.

melvin-bot[bot] commented 3 weeks ago

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

jliexpensify commented 3 weeks ago

Ty Shawn, lets do it!

melvin-bot[bot] commented 3 weeks ago

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

jayeshmangwani commented 3 weeks ago

Thanks for the proposal, @Shahidullah-Muffakir. Removing selection.start <= 0 as you suggested would break the current functionality for moving to the first line when pressing the up key in the composer.

Steps to reproduce the issue after applying your change:

  1. Press Command + Space in the composer to add a new line.
  2. Press the up key.
  3. Nothing happens, whereas in the current version of the app, pressing the up key in the composer allows the cursor to move to the first line.
Shahidullah-Muffakir commented 3 weeks ago

Thanks for the proposal, @Shahidullah-Muffakir. Removing selection.start <= 0 as you suggested would break the current functionality for moving to the first line when pressing the up key in the composer.

Steps to reproduce the issue after applying your change:

  1. Press Command + Space in the composer to add a new line.
  2. Press the up key.
  3. Nothing happens, whereas in the current version of the app, pressing the up key in the composer allows the cursor to move to the first line.

@jayeshmangwani , Oh, got it, thanks for explaining that! I could reproduce it now.

Given that, what do you think of the alternative approach I mentioned in my proposal: setting selection.start to 0 whenever a message is sent with an attachment?

Or when we send the comment without or without attachment, this onClear, is called. https://github.com/Expensify/App/blob/be4d7f50949d52aa9f2cb9a8ac495ef19473edde/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx#L700

we can set the selection.start to 0 here as well as: setSelection((prevState) => ({ ...prevState, start: 0, end: 0 }));

https://github.com/user-attachments/assets/60273dcc-7c61-48ee-a581-17ee8484bb3c

jayeshmangwani commented 2 weeks ago

@daledah I tried adding setSelection before the isFocused condition, but I feel this could introduce a regression. Although the issue doesn't consistently reproduce, I've managed to replicate it a few times. Here are the steps:

  1. Go to any workspace chat.
  2. Type @ to open the mention suggestion.
  3. Navigate to the LHN and submit an expense to that workspace (from step 1).
  4. Observe that when returning to the workspace chat, a modal opens—though it shouldn't.

https://github.com/user-attachments/assets/e9570064-3b7b-435d-b8be-39a65b6f5eb3

jayeshmangwani commented 2 weeks ago

@Shahidullah-Muffakir I have a question for this comment: if we add the setSelection((prevState) => ({ ...prevState, start: 0, end: 0 })); in onClear, are we not setting setSelection twice? We’re already calling setSelection(e.nativeEvent.selection) in ComposerWithSuggestions.

Shahidullah-Muffakir commented 2 weeks ago

@Shahidullah-Muffakir I have a question for this comment: if we add the setSelection((prevState) => ({ ...prevState, start: 0, end: 0 })); in onClear, are we not setting setSelection twice? We’re already calling setSelection(e.nativeEvent.selection) in ComposerWithSuggestions.

@jayeshmangwani , in this part: https://github.com/Expensify/App/blob/be4d7f50949d52aa9f2cb9a8ac495ef19473edde/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx#L514-L519

the setSelection(e.nativeEvent.selection), won't be executed as the !textInputRef.current?.isFocused(), will be true in the case of sending attachment.

and just to not re-rerun this setSelection((prevState) => ({ ...prevState, start: 0, end: 0 })); on the onClear function twice for a text only message, we can add check if the input is not focused then run it.

if(selection.start !== 0){
 setSelection((prevState) => ({ ...prevState, start: 0, end: 0 }));
}

or

if(!textInputRef.current?.isFocused()){
 setSelection((prevState) => ({ ...prevState, start: 0, end: 0 }));
}

Also, I noticed that setSelection((prevState) => ({ ...prevState, start: 0, end: 0 })); is also being called in the Composer component’s onClear function here: https://github.com/Expensify/App/blob/e6b416726fbe7171e84f048a8874c1b9254b6a26/src/components/Composer/implementation/index.tsx#L278

jayeshmangwani commented 2 weeks ago

and just to not re-rerun this setSelection((prevState) => ({ ...prevState, start: 0, end: 0 })); on the onClear function twice for a text only message, we can add check if the input is not focused then run it.

Thanks! Let me test this change.

jayeshmangwani commented 2 weeks ago

called in the Composer component’s onClear function

Just to confirm, is onClear triggered only for text only message?

Shahidullah-Muffakir commented 2 weeks ago

Just to confirm, is onClear triggered only for text only message?

No, clear is triggered for all messages, regardless of whether they include attachments or are text-only. https://github.com/Expensify/App/blob/e6b416726fbe7171e84f048a8874c1b9254b6a26/src/components/Composer/implementation/index.tsx#L278

In clear, setSelection({ start: 0, end: 0 }) is called to reset the selection, but it only updates the local state selection within the Composer component. Since this reset is only applied to Composer's own internal selection state, it doesn’t affect the selection state in ComposerWithSuggestions. https://github.com/Expensify/App/blob/04402e9f47a77f10b3c2c47fefe0931fbf62e4d4/src/components/Composer/implementation/index.tsx#L63-L68 In ComposerWithSuggestions, we maintain a separate local selection state using which we decide to open the comment edit box if the selection.start <=0,
https://github.com/Expensify/App/blob/e6b416726fbe7171e84f048a8874c1b9254b6a26/src/components/Composer/implementation/index.tsx#L63 This means the selection reset on clear in Composer doesn’t propagate to ComposerWithSuggestions, which is why we need to explicitly update the selection state of ComposerWithSuggestions in the onClear: https://github.com/Expensify/App/blob/be4d7f50949d52aa9f2cb9a8ac495ef19473edde/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx#L700

jayeshmangwani commented 2 weeks ago

@Shahidullah-Muffakir While testing your changes, I discovered another bug related to the up-to-edit feature (reproducing on staging as well), which I believe we should address within this issue. Could you confirm if the root cause is the same for message with attachment as well as the bug described below, and whether your proposal will fix it?

Steps to reproduce:

  1. Type a message in the chat, then tap outside the composer to remove focus.
  2. Press the Send button.
  3. Tap on the composer again.
  4. Press the up key. (Expected: It should open the edit mode for the last message, but currently it does nothing.)

Let me know if your solution will cover this case as well.

https://github.com/user-attachments/assets/4b7891ad-8c9b-4bfa-8dbd-ec4f3a526470

Shahidullah-Muffakir commented 2 weeks ago

@Shahidullah-Muffakir While testing your changes, I discovered another bug related to the up-to-edit feature (reproducing on staging as well), which I believe we should address within this issue. Could you confirm if the root cause is the same for message with attachment as well as the bug described below, and whether your proposal will fix it?

Steps to reproduce:

  1. Type a message in the chat, then tap outside the composer to remove focus.
  2. Press the Send button.
  3. Tap on the composer again.
  4. Press the up key. (Expected: It should open the edit mode for the last message, but currently it does nothing.)

Let me know if your solution will cover this case as well.

edit-focus-bug.mov

@jayeshmangwani , Yes it has the same root cause as the issue in the OP, and the proposed solution will work for this as well.

https://github.com/user-attachments/assets/9b61ae19-46a6-423d-8d40-ea579a1c3e4d

jayeshmangwani commented 2 weeks ago

@Shahidullah-Muffakir Cool, please add your comments https://github.com/Expensify/App/issues/52319#issuecomment-2470952844 https://github.com/Expensify/App/issues/52319#issuecomment-2474203557 https://github.com/Expensify/App/issues/52319#issuecomment-2475588671 to the main proposal in one place and update the proposal so I can review it again.

Shahidullah-Muffakir commented 2 weeks ago

@jayeshmangwani, Proposal updated, Thank you.

jayeshmangwani commented 2 weeks ago

Hey @Shahidullah-Muffakir, thanks for updating the proposal, and sorry for the back-and-forth. Since this is the main component of the app, I need to test some parts thoroughly, which is why it's taking a bit longer to approve the proposal.

Also, from your proposal, it seems to work fine in testing, but I don't entirely agree with setting setSelection({ start: 0, end: 0 }) in onClear. The reasons are:

  1. In Composer/implementation/index.tsx, when we send the message and clear the text, only the clear function is called, and at that time onSelectionChange won't trigger. So, setting the selection to 0 will only happen from one place.
  2. For the ComposerWithSuggestions component, when the text is cleared, both onSelectionChange and onClear are called. So, setting setSelection in both places feels a bit odd right now. We should either set setSelection from onSelectionChange or from onClear.
Shahidullah-Muffakir commented 2 weeks ago

No problem, @jayeshmangwani. Regarding the setSelection({ start: 0, end: 0 }) placement, I see your point about the redundancy in calling setSelection in both onClear and onSelectionChange within ComposerWithSuggestions. However, the Composer component itself also handles selection changes in both clear and onSelectionChange. As we are passing onSelectionChange as a prop to Composer component from ComposerWithSuggestions https://github.com/Expensify/App/blob/04402e9f47a77f10b3c2c47fefe0931fbf62e4d4/src/components/Composer/implementation/index.tsx#L277 What do you think about this? I’ll also keep looking .

jayeshmangwani commented 2 weeks ago

Composer component itself also handles selection changes in both clear and onSelectionChange

Yes, but in the Composer component, onSelectionChange is not triggered when we press the send button. On the other hand, in ComposerWithSuggestions, both clear and onSelectionChange are called when pressing send or when clearing the composer.

Shahidullah-Muffakir commented 2 weeks ago

Yes, but in the Composer component, onSelectionChange is not triggered when we press the send button.

When we send message the clear function of Composer component is being called, inside that function onSelectionChange is also being called.

daledah commented 2 weeks ago

@jayeshmangwani The issue you mentioned here still happens on staging. I don't think it's my regression

https://github.com/user-attachments/assets/390bc577-c7c2-43d4-a125-6c3463eb12b0

melvin-bot[bot] commented 2 weeks ago

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

jayeshmangwani commented 2 weeks ago

Yes @daledah, I'm also able to reproduce this on staging, so it doesn't appear to be caused by your changes.

jayeshmangwani commented 2 weeks ago

Thanks to everyone for the proposals above!

I've tested a few cases, and calling setSelection before the !textInputRef.current?.isFocused()condition in onSelectionChange resolves the issue without causing any problems during testing. Based on this, I suggest we proceed with @daledah's Proposal.

🎀 👀 🎀 C+ reviewed

melvin-bot[bot] commented 2 weeks ago

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

melvin-bot[bot] commented 2 weeks ago

📣 @daledah 🎉 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 📖

daledah commented 2 weeks ago

@jayeshmangwani PR is ready.

melvin-bot[bot] commented 3 days ago

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

melvin-bot[bot] commented 3 days ago

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

If no regressions arise, payment will be issued on 2024-12-07. :confetti_ball:

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

melvin-bot[bot] commented 3 days ago

@jayeshmangwani @jliexpensify @jayeshmangwani The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed. Please copy/paste the BugZero Checklist from here into a new comment on this GH and complete it. If you have the K2 extension, you can simply click: [this button]