Skip to content

Commit

Permalink
Fix SearchRouter opening from keyboard shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
Kicu committed Oct 4, 2024
1 parent 078420e commit e21ed95
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
25 changes: 22 additions & 3 deletions src/components/Search/SearchRouter/SearchRouterContext.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, {useContext, useMemo, useState} from 'react';
import React, {useContext, useMemo, useRef, useState} from 'react';
import type ChildrenProps from '@src/types/utils/ChildrenProps';

const defaultSearchContext = {
isSearchRouterDisplayed: false,
openSearchRouter: () => {},
closeSearchRouter: () => {},
toggleSearchRouter: () => {},
};

type SearchRouterContext = typeof defaultSearchContext;
Expand All @@ -13,15 +14,33 @@ const Context = React.createContext<SearchRouterContext>(defaultSearchContext);

function SearchRouterContextProvider({children}: ChildrenProps) {
const [isSearchRouterDisplayed, setIsSearchRouterDisplayed] = useState(false);
const searchRouterDisplayedRef = useRef(false);

const routerContext = useMemo(() => {
const openSearchRouter = () => setIsSearchRouterDisplayed(true);
const closeSearchRouter = () => setIsSearchRouterDisplayed(false);
const openSearchRouter = () => {
setIsSearchRouterDisplayed(true);
searchRouterDisplayedRef.current = true;
};
const closeSearchRouter = () => {
setIsSearchRouterDisplayed(false);
searchRouterDisplayedRef.current = false;
};

// There are callbacks that live outside of React render-loop and interact with SearchRouter
// So we need a function that is based on ref to correctly open/close it
const toggleSearchRouter = () => {
if (searchRouterDisplayedRef.current) {
closeSearchRouter();
} else {
openSearchRouter();
}
};

return {
isSearchRouterDisplayed,
openSearchRouter,
closeSearchRouter,
toggleSearchRouter,
};
}, [isSearchRouterDisplayed, setIsSearchRouterDisplayed]);

Expand Down
12 changes: 4 additions & 8 deletions src/libs/Navigation/AppNavigator/AuthScreens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
const screenOptions = getRootNavigatorScreenOptions(shouldUseNarrowLayout, styles, StyleUtils);
const {canUseDefaultRooms} = usePermissions();
const {activeWorkspaceID} = useActiveWorkspace();
const {openSearchRouter} = useSearchRouterContext();
const {toggleSearchRouter} = useSearchRouterContext();

const onboardingModalScreenOptions = useMemo(() => screenOptions.onboardingModalNavigator(onboardingIsMediumOrLargerScreenWidth), [screenOptions, onboardingIsMediumOrLargerScreenWidth]);
const onboardingScreenOptions = useMemo(
Expand Down Expand Up @@ -372,13 +372,9 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
const unsubscribeSearchShortcut = KeyboardShortcut.subscribe(
searchShortcutConfig.shortcutKey,
() => {
Modal.close(
Session.checkIfActionIsAllowed(() => {
openSearchRouter();
}),
true,
true,
);
Session.checkIfActionIsAllowed(() => {
toggleSearchRouter();
})();
},
shortcutsOverviewShortcutConfig.descriptionKey,
shortcutsOverviewShortcutConfig.modifiers,
Expand Down

0 comments on commit e21ed95

Please sign in to comment.