Skip to content

Commit

Permalink
fix esc doesn't close modal if sidebar LHN isn't mounted
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardoj committed Aug 21, 2024
1 parent 685bb4c commit 6847ec9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
34 changes: 34 additions & 0 deletions src/libs/Navigation/AppNavigator/AuthScreens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import getOnboardingModalScreenOptions from '@libs/Navigation/getOnboardingModal
import Navigation from '@libs/Navigation/Navigation';
import type {AuthScreensParamList, CentralPaneName, CentralPaneScreensParamList} from '@libs/Navigation/types';
import NetworkConnection from '@libs/NetworkConnection';
import onyxSubscribe from '@libs/onyxSubscribe';
import * as Pusher from '@libs/Pusher/pusher';
import PusherConnectionManager from '@libs/PusherConnectionManager';
import * as ReportUtils from '@libs/ReportUtils';
Expand Down Expand Up @@ -215,6 +216,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
() => getOnboardingModalScreenOptions(isSmallScreenWidth, styles, StyleUtils, isMediumOrLargerScreenWidth),
[StyleUtils, isSmallScreenWidth, isMediumOrLargerScreenWidth, styles],
);
const modal = useRef<OnyxTypes.Modal>({});

let initialReportID: string | undefined;
const isInitialRender = useRef(true);
Expand Down Expand Up @@ -283,6 +285,36 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie

Timing.end(CONST.TIMING.HOMEPAGE_INITIAL_RENDER);

const unsubscribeOnyxModal = onyxSubscribe({
key: ONYXKEYS.MODAL,
callback: (modalArg) => {
if (modalArg === null || typeof modalArg !== 'object') {
return;
}
modal.current = modalArg;
},
});

const shortcutConfig = CONST.KEYBOARD_SHORTCUTS.ESCAPE;
const unsubscribeEscapeKey = KeyboardShortcut.subscribe(
shortcutConfig.shortcutKey,
() => {
if (modal.current.willAlertModalBecomeVisible) {
return;
}

if (modal.current.disableDismissOnEscape) {
return;
}

Navigation.dismissModal();
},
shortcutConfig.descriptionKey,
shortcutConfig.modifiers,
true,
true,
);

// Listen to keyboard shortcuts for opening certain pages
const unsubscribeShortcutsOverviewShortcut = KeyboardShortcut.subscribe(
shortcutsOverviewShortcutConfig.shortcutKey,
Expand Down Expand Up @@ -333,6 +365,8 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
);

return () => {
unsubscribeEscapeKey();
unsubscribeOnyxModal();
unsubscribeShortcutsOverviewShortcut();
unsubscribeSearchShortcut();
unsubscribeChatShortcut();
Expand Down
47 changes: 2 additions & 45 deletions src/pages/home/sidebar/SidebarLinks.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {memo, useCallback, useEffect, useMemo, useRef} from 'react';
import React, {memo, useCallback, useEffect, useMemo} from 'react';
import {InteractionManager, StyleSheet, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import type {EdgeInsets} from 'react-native-safe-area-context';
Expand All @@ -9,15 +9,12 @@ import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import KeyboardShortcut from '@libs/KeyboardShortcut';
import Navigation from '@libs/Navigation/Navigation';
import onyxSubscribe from '@libs/onyxSubscribe';
import * as ReportActionContextMenu from '@pages/home/report/ContextMenu/ReportActionContextMenu';
import * as App from '@userActions/App';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Modal, Report} from '@src/types/onyx';
import type {Report} from '@src/types/onyx';

type SidebarLinksProps = {
/** Toggles the navigation menu open and closed */
Expand Down Expand Up @@ -46,7 +43,6 @@ type SidebarLinksProps = {
function SidebarLinks({onLinkClick, insets, optionListItems, isLoading, priorityMode = CONST.PRIORITY_MODE.DEFAULT, isActiveReport}: SidebarLinksProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const modal = useRef<Modal>({});
const {updateLocale} = useLocalize();
const {shouldUseNarrowLayout} = useResponsiveLayout();

Expand All @@ -61,46 +57,7 @@ function SidebarLinks({onLinkClick, insets, optionListItems, isLoading, priority
});
});

const unsubscribeOnyxModal = onyxSubscribe({
key: ONYXKEYS.MODAL,
callback: (modalArg) => {
if (modalArg === null || typeof modalArg !== 'object') {
return;
}
modal.current = modalArg;
},
});

const shortcutConfig = CONST.KEYBOARD_SHORTCUTS.ESCAPE;
const unsubscribeEscapeKey = KeyboardShortcut.subscribe(
shortcutConfig.shortcutKey,
() => {
if (modal.current.willAlertModalBecomeVisible) {
return;
}

if (modal.current.disableDismissOnEscape) {
return;
}

Navigation.dismissModal();
},
shortcutConfig.descriptionKey,
shortcutConfig.modifiers,
true,
true,
);

ReportActionContextMenu.hideContextMenu(false);

return () => {
if (unsubscribeEscapeKey) {
unsubscribeEscapeKey();
}
if (unsubscribeOnyxModal) {
unsubscribeOnyxModal();
}
};
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);

Expand Down

0 comments on commit 6847ec9

Please sign in to comment.