Skip to content

[C+ Checklist Needs Completion] [$250] The focused background isn't updated when the selected option is updated from BE side #54089

Closed

Description

@m-natarajan

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.75-6
Reproducible in staging?: Y
Reproducible in production?: Y
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: @DylanDylann
Slack conversation (hyperlinked to channel name): expensify_bugs

Action Performed:

  1. In the device 1, go to Settings> Preferences > Priority mode
  2. In the device 2, go to Settings> Preferences > Priority mode
  3. In the device 2, click on another option

Expected Result:

The selected option is updated in the device 1 but the focus background isn't updated

Actual Result:

The focused background isn't updated in the device 1

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
  • MacOS: Chrome / Safari
  • MacOS: Desktop

Screenshots/Videos

Add any screenshot/video evidence
Screen.Recording.2024-12-12.at.11.29.54.mov
Recording.844.mp4

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~021867633562881780913
  • Upwork Job ID: 1867633562881780913
  • Last Price Increase: 2024-12-13
  • Automatic offers:
    • DylanDylann | Reviewer | 105417829
    • daledah | Contributor | 105417830
Issue OwnerCurrent Issue Owner: @michaelkwardrop

Activity

  1. added
    BugSomething is broken. Auto assigns a BugZero manager.
    on Dec 13, 2024
  2. melvin-bot commented on Dec 13, 2024

    @melvin-bot

    Triggered auto assignment to @michaelkwardrop (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.

  3. nyomanjyotisa commented on Dec 13, 2024

    @nyomanjyotisa
    Member

    Proposal

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

    The focused background isn't updated when the selected option is updated from BE side

    What is the root cause of that problem?

    The focus is not updated when the selected priority mode changes, causing the UI to stay focused on the old selection.

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

    We should manage the focus using a state variable (focusedKey) that gets updated when the priorityMode changes. This ensures the UI focus stays in sync with the selected priority mode.

        const [focusedKey, setFocusedKey] = useState(priorityMode);
    
        useEffect(() => {
            setFocusedKey(priorityMode);
        }, [priorityMode]);
    

    And change the SelectionList to this

                <SelectionList
                    sections={[{data: priorityModes}]}
                    ListItem={RadioListItem}
                    onSelectRow={(mode) => {
                        updateMode(mode);
                        setFocusedKey(mode.keyForList);
                    }}
                    shouldSingleExecuteRowSelect
                    initiallyFocusedOptionKey={focusedKey}
                    key={focusedKey}
                />
    

    What specific scenarios should we cover in automated tests to prevent reintroducing this issue in the future?

    What alternative solutions did you explore? (Optional)

    POC

    New-Expensify.mp4
  4. linhvovan29546 commented on Dec 13, 2024

    @linhvovan29546
    Contributor

    Edited by proposal-police: This proposal was edited at 2024-12-13 02:47:13 UTC.

    Proposal

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

    The focused background isn't updated when the selected option is updated from BE side

    What is the root cause of that problem?

    In BaseSelectionList, the getItemBackgroundColorStyle function is used to get the background color for an item. This function only applies a background color if isFocused is true. When updates are received from the backend, only the isSelected state of priorityModes is updated, but isFocused is not. As a result, the focused background does not change.

    isFocused && StyleUtils.getItemBackgroundColorStyle(!!item.isSelected, !!isFocused, !!item.isDisabled, theme.activeComponentBG, theme.hoverComponentBG),

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

    Out dated

    In PreferencesPage, we should use a useEffect with priorityModes as a dependency to determine the focusedIndex and then call ref.current.setFocusedIndex(focusedIndex).

    useEffect(() => {
            const handleFocus = () => {
                const focusedIndex = priorityModes.findIndex((mode) => mode.isSelected)
                refSelectionList.current?.setFocusedIndex(focusedIndex)
            }
            handleFocus()
        }, [priorityModes])
    .....
                <SelectionList
                    ref={refSectionList}
                    //    other props
                 />
    

    We also need to export the setFocusedIndex method from BaseSelectionList using useImperativeHandle to enable this functionality.

    useImperativeHandle(ref, () => ({scrollAndHighlightItem, clearInputAfterSelect, updateAndScrollToFocusedIndex, updateExternalTextInputFocus, scrollToIndex}), [
    scrollAndHighlightItem,
    clearInputAfterSelect,
    updateAndScrollToFocusedIndex,
    updateExternalTextInputFocus,
    scrollToIndex,
    ]);

    New proposal:

    In the BaseSelectionList add a new prop shouldAutoUpdateFocusedIndex, with a default value of false. This prop will be set to true only in the specific places where the focused index needs to be updated automatically, in order to avoid regression.
    Use useEffect with flattenedSections as a dependency to update focusedIndex

        const isRenderRef = useRef(false)
    
        useEffect(() => {
            if (!shouldAutoUpdateFocusedIndex) return
            if (!isRenderRef.current) {
                isRenderRef.true
                return
            }
            const handleUpdateFocusedIndex = () => {
                const focusedIndex = flattenedSections.allOptions.findIndex((section) => section.isSelected)
                setFocusedIndex(focusedIndex)
            }
            handleUpdateFocusedIndex()
        }, [shouldAutoUpdateFocusedIndex, flattenedSections.allOptions])
    

    What specific scenarios should we cover in automated tests to prevent reintroducing this issue in the future?

    Edited at 2024-12-14 04:46:13 UTC.
    I don't see any test files for this, so I think we need to implement UI tests for the BaseSelectionList component, including the following actions: render the BaseSelectionList with the correct selected status, handling item press, and rerender the BaseSelectionList with the updated selected status.

    N/A

    What alternative solutions did you explore? (Optional)

    N/A

  5. daledah commented on Dec 13, 2024

    @daledah
    Contributor

    Edited by proposal-police: This proposal was edited at 2024-12-19 16:20:50 UTC.

    Proposal

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

    The focused background isn't updated in the device 1

    What is the root cause of that problem?

    In BaseSelectionList, we don't have logics to change focusedIndex when there's update from BE.

    This Bug also appears on similar pages: Language and Theme settings.

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

    Add an useEffect to change focusedIndex when list data is updated to after here:

        useEffect(() => {
            const selectedItemIndex = flattenedSections.allOptions.findIndex((option) => option.isSelected);
            if (selectedItemIndex === -1 || selectedItemIndex === focusedIndex) {
                return;
            }
            setFocusedIndex(selectedItemIndex);
        }, [flattenedSections]);

    What specific scenarios should we cover in automated tests to prevent reintroducing this issue in the future?

    For this specific issue, we can introduce these test cases for BaseSelectionList:

    1. Item press
        it('should handle item press correctly', () => {
            render(<RenderBaseSelectionList sections={[{data: SECTIONS}]} />);
            fireEvent.press(screen.getByTestId('base-list-item-2'));
            expect(mockOnSelectRow).toHaveBeenCalledWith({
                ...SECTIONS.at(1),
                shouldAnimateInHighlight: false,
            });
        });
    1. Changes to data from BE
        it('should update focused item when sections are updated from BE', () => {
            const updatedSections = SECTIONS.map((section) => ({
                ...section,
                isSelected: section.keyForList === '2',
            }));
            const {rerender} = render(<RenderBaseSelectionList sections={[{data: SECTIONS}]} />);
            fireEvent.press(screen.getByTestId('base-list-item-1'));
            rerender(<RenderBaseSelectionList sections={[{data: updatedSections}]} />);
            expect(screen.getByTestId('base-list-item-2')).toHaveAccessibilityValue({
                selected: true,
            });
        });
    1. Optionally, we can test for keyboard control as well, but this test will require to install an additional package @testing-library/user-event:
        it('should handle keyboard navigation correctly', async () => {
            render(<RenderBaseSelectionList sections={[{data: SECTIONS}]} />);
            await userEvent.keyboard('{arrowdown}');
            expect(screen.getByTestId('base-list-item-2', {})).toHaveAccessibilityValue({
                selected: true,
            });
        });

    What alternative solutions did you explore? (Optional)

    We can pass key to SelectionList in PriorityModePage to force re-render when priority mode is changed, however this can cause unwanted extra render count and make the UI briefly flashes but still noticeable.

  6. DylanDylann commented on Dec 13, 2024

    @DylanDylann
    Contributor

    hey contributors, please make sure that your proposal also fix this problem

  7. DylanDylann commented on Dec 13, 2024

    @DylanDylann
    Contributor
  8. linhvovan29546 commented on Dec 13, 2024

    @linhvovan29546
    Contributor

    Updated proposal to fix this problem

    #54089 (comment)

  9. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on Dec 13, 2024
  10. melvin-bot commented on Dec 13, 2024

    @melvin-bot
  11. changed the title [-]The focused background isn't updated when the selected option is updated from BE side[/-] [+][$250] The focused background isn't updated when the selected option is updated from BE side[/+] on Dec 13, 2024
  12. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Dec 13, 2024
  13. 32 remaining items

  14. melvin-bot commented on Jan 10, 2025

    @melvin-bot

    @DylanDylann @michaelkwardrop @DylanDylann 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]

  15. srikarparsi commented on Jan 13, 2025

    @srikarparsi
    Contributor

    Hi @daledah and @DylanDylann, do you think you could take a look at this regression please which seems to come from this PR?

  16. DylanDylann commented on Jan 13, 2025

    @DylanDylann
    Contributor

    @srikarparsi Yes it is a regression from our prev PR. @daledah Let's create a PR to fix it, I believe that the fix is small

  17. michaelkwardrop commented on Jan 17, 2025

    @michaelkwardrop
    Contributor

    Payment summary:

    Contributor: @daledah $125 - paid via UpWork ✅
    C+: @DylanDylann $125 - paying via UpWork (once checklist is completed)

    50% regression penalty #54931

  18. changed the title [-][HOLD for payment 2025-01-17] [$250] The focused background isn't updated when the selected option is updated from BE side[/-] [+][C+ Checklist Needs Completion] [$250] The focused background isn't updated when the selected option is updated from BE side[/+] on Jan 17, 2025
  19. DylanDylann commented on Jan 20, 2025

    @DylanDylann
    Contributor

    BugZero Checklist:

    • [Contributor] Classify the bug:
    Bug classification

    Source of bug:

    • 1a. Result of the original design (eg. a case wasn't considered)
    • 1b. Mistake during implementation
    • 1c. Backend bug
    • 1z. Other:

    Where bug was reported:

    • 2a. Reported on production (eg. bug slipped through the normal regression and PR testing process on staging)
    • 2b. Reported on staging (eg. found during regression or PR testing)
    • 2d. Reported on a PR
    • 2z. Other:

    Who reported the bug:

    • 3a. Expensify user
    • 3b. Expensify employee
    • 3c. Contributor
    • 3d. QA
    • 3z. Other:
    • [Contributor] 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: No. This is a new improvement, not a bug

    • [Contributor] If the regression was CRITICAL (e.g. interrupts a core flow) A discussion in #expensify-open-source 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:

    • [Contributor] If it was decided to create a regression test for the bug, please propose the regression test steps using the template below to ensure the same bug will not reach production again.

    • [BugZero Assignee] Create a GH issue for creating/updating the regression test once above steps have been agreed upon.

      Link to issue:

    Regression Test Proposal

    Test:

    1. In the device 1, go to Settings > Preferences > Priority mode
    2. In the device 2, go to Settings > Preferences > Priority mode
    3. In the device 2, click on another option
    4. Verify that: In device 1, selected option is updated and focused background is displayed correctly.

    Do we agree 👍 or 👎

  20. michaelkwardrop commented on Jan 23, 2025

    @michaelkwardrop
    Contributor

    UPDATE

    Payment summary:

    Contributor: @daledah $125 - paid via UpWork ✅
    C+: @DylanDylann $125 - paid via UpWork ✅

    50% regression penalty #54931

  21. michaelkwardrop commented on Jan 23, 2025

    @michaelkwardrop
    Contributor

    Job has been closed in Upwork, closing this out. Great work.

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

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionBugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributor

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions