Skip to content

Commit

Permalink
Merge pull request #44399 from gijoe0295/gijoe/44078
Browse files Browse the repository at this point in the history
fix: Copying a message with a reply doesn't refocus on the composer
  • Loading branch information
luacmartins authored Jul 9, 2024
2 parents 5ce4e60 + 546ab06 commit d576e28
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/components/FocusTrap/FocusTrapForModal/index.web.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import FocusTrap from 'focus-trap-react';
import React from 'react';
import sharedTrapStack from '@components/FocusTrap/sharedTrapStack';
import ReportActionComposeFocusManager from '@libs/ReportActionComposeFocusManager';
import type FocusTrapForModalProps from './FocusTrapForModalProps';

function FocusTrapForModal({children, active}: FocusTrapForModalProps) {
Expand All @@ -12,6 +13,12 @@ function FocusTrapForModal({children, active}: FocusTrapForModalProps) {
clickOutsideDeactivates: true,
initialFocus: false,
fallbackFocus: document.body,
setReturnFocus: (element) => {
if (ReportActionComposeFocusManager.isFocused()) {
return false;
}
return element;
},
}}
>
{children}
Expand Down
17 changes: 17 additions & 0 deletions src/libs/Navigation/isReportOpenInRHP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type {NavigationState} from '@react-navigation/native';
import NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';

const isReportOpenInRHP = (state: NavigationState | undefined): boolean => {
const lastRoute = state?.routes?.at(-1);
if (!lastRoute) {
return false;
}
const params = lastRoute.params;
if (params && 'screen' in params && typeof params.screen === 'string' && params.screen === SCREENS.RIGHT_MODAL.SEARCH_REPORT) {
return true;
}
return !!(lastRoute.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR && lastRoute.state?.routes?.some((route) => route?.name === SCREENS.RIGHT_MODAL.SEARCH_REPORT));
};

export default isReportOpenInRHP;
3 changes: 2 additions & 1 deletion src/libs/Navigation/linkTo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {NavigationContainerRef, NavigationState, PartialState} from '@react
import {findFocusedRoute} from '@react-navigation/native';
import {omitBy} from 'lodash';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import isReportOpenInRHP from '@libs/Navigation/isReportOpenInRHP';
import extractPolicyIDsFromState from '@libs/Navigation/linkingConfig/extractPolicyIDsFromState';
import {isCentralPaneName} from '@libs/NavigationUtils';
import shallowCompare from '@libs/ObjectUtils';
Expand Down Expand Up @@ -68,7 +69,7 @@ export default function linkTo(navigation: NavigationContainerRef<RootStackParam

const action: StackNavigationAction = getActionFromState(stateFromPath, linkingConfig.config);

const isReportInRhpOpened = lastRoute?.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR && lastRoute?.state?.routes?.some((route) => route?.name === SCREENS.RIGHT_MODAL.SEARCH_REPORT);
const isReportInRhpOpened = isReportOpenInRHP(rootState);

// If action type is different than NAVIGATE we can't change it to the PUSH safely
if (action?.type === CONST.NAVIGATION.ACTION_TYPE.NAVIGATE) {
Expand Down
11 changes: 7 additions & 4 deletions src/libs/ReportActionComposeFocusManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React from 'react';
import type {MutableRefObject} from 'react';
import type {TextInput} from 'react-native';
import ROUTES from '@src/ROUTES';
import Navigation from './Navigation/Navigation';
import SCREENS from '@src/SCREENS';
import getTopmostRouteName from './Navigation/getTopmostRouteName';
import isReportOpenInRHP from './Navigation/isReportOpenInRHP';
import navigationRef from './Navigation/navigationRef';

type FocusCallback = (shouldFocusForNonBlurInputOnTapOutside?: boolean) => void;

Expand Down Expand Up @@ -31,8 +33,9 @@ function onComposerFocus(callback: FocusCallback | null, isMainComposer = false)
* Request focus on the ReportActionComposer
*/
function focus(shouldFocusForNonBlurInputOnTapOutside?: boolean) {
/** Do not trigger the refocusing when the active route is not the report route, */
if (!Navigation.isActiveRoute(ROUTES.REPORT_WITH_ID.getRoute(Navigation.getTopmostReportId() ?? '-1'))) {
/** Do not trigger the refocusing when the active route is not the report screen */
const navigationState = navigationRef.getState();
if (!navigationState || (!isReportOpenInRHP(navigationState) && getTopmostRouteName(navigationState) !== SCREENS.REPORT)) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/perf-test/SidebarLinks.perf-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ jest.mock('../../src/libs/Navigation/Navigation', () => ({
isNavigationReady: jest.fn(() => Promise.resolve()),
isDisplayedInModal: jest.fn(() => false),
}));
jest.mock('../../src/libs/Navigation/navigationRef', () => ({
getState: () => ({
routes: [],
}),
}));
jest.mock('@components/Icon/Expensicons');

jest.mock('@react-navigation/native');
Expand Down

0 comments on commit d576e28

Please sign in to comment.