Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix command menu selection #10248

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hasUserSelectedCommandState } from '@/command-menu/states/hasUserSelectedCommandState';
import { useSelectableList } from '@/ui/layout/selectable-list/hooks/useSelectableList';
import { useEffect } from 'react';
import { useRecoilValue } from 'recoil';
Expand All @@ -13,18 +14,26 @@ export const CommandMenuDefaultSelectionEffect = ({

const selectedItemId = useRecoilValue(selectedItemIdState);

const hasUserSelectedCommand = useRecoilValue(hasUserSelectedCommandState);

useEffect(() => {
if (
isDefined(selectedItemId) &&
selectableItemIds.includes(selectedItemId)
selectableItemIds.includes(selectedItemId) &&
hasUserSelectedCommand
) {
return;
}

if (selectableItemIds.length > 0) {
setSelectedItemId(selectableItemIds[0]);
}
}, [selectableItemIds, selectedItemId, setSelectedItemId]);
}, [
hasUserSelectedCommand,
selectableItemIds,
selectedItemId,
setSelectedItemId,
]);

return null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { COMMAND_MENU_SEARCH_BAR_PADDING } from '@/command-menu/constants/Comman
import { RESET_CONTEXT_TO_SELECTION } from '@/command-menu/constants/ResetContextToSelection';
import { useCommandMenuOnItemClick } from '@/command-menu/hooks/useCommandMenuOnItemClick';
import { useResetPreviousCommandMenuContext } from '@/command-menu/hooks/useResetPreviousCommandMenuContext';
import { hasUserSelectedCommandState } from '@/command-menu/states/hasUserSelectedCommandState';
import { SelectableItem } from '@/ui/layout/selectable-list/components/SelectableItem';
import { SelectableList } from '@/ui/layout/selectable-list/components/SelectableList';
import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope';
import { ScrollWrapper } from '@/ui/utilities/scroll/components/ScrollWrapper';
import styled from '@emotion/styled';
import { useSetRecoilState } from 'recoil';
import { isDefined } from 'twenty-shared';
import { MOBILE_VIEWPORT } from 'twenty-ui';

Expand Down Expand Up @@ -75,6 +77,10 @@ export const CommandMenuList = ({
const { resetPreviousCommandMenuContext } =
useResetPreviousCommandMenuContext();

const setHasUserSelectedCommand = useSetRecoilState(
hasUserSelectedCommandState,
);

return (
<>
<CommandMenuDefaultSelectionEffect
Expand Down Expand Up @@ -109,6 +115,9 @@ export const CommandMenuList = ({
});
}
}}
onSelect={() => {
setHasUserSelectedCommand(true);
}}
>
{children}
{commandGroups.map(({ heading, items }) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@/command-menu/states/commandMenuNavigationStackState';
import { commandMenuPageState } from '@/command-menu/states/commandMenuPageState';
import { commandMenuPageInfoState } from '@/command-menu/states/commandMenuPageTitle';
import { hasUserSelectedCommandState } from '@/command-menu/states/hasUserSelectedCommandState';
import { CommandMenuPages } from '@/command-menu/types/CommandMenuPages';
import { contextStoreCurrentViewTypeComponentState } from '@/context-store/states/contextStoreCurrentViewTypeComponentState';
import { contextStoreFiltersComponentState } from '@/context-store/states/contextStoreFiltersComponentState';
Expand Down Expand Up @@ -60,6 +61,7 @@ export const useCommandMenu = () => {

set(isCommandMenuOpenedState, true);
setHotkeyScopeAndMemorizePreviousScope(AppHotkeyScope.CommandMenuOpen);
set(hasUserSelectedCommandState, false);
},
[
copyContextStoreStates,
Expand Down Expand Up @@ -89,6 +91,7 @@ export const useCommandMenu = () => {
set(commandMenuSearchState, '');
set(commandMenuNavigationStackState, []);
resetSelectedItem();
set(hasUserSelectedCommandState, false);
goBackToPreviousHotkeyScope();

emitRightDrawerCloseEvent();
Expand Down Expand Up @@ -173,6 +176,7 @@ export const useCommandMenu = () => {
});

set(commandMenuNavigationStackState, newNavigationStack);
set(hasUserSelectedCommandState, false);
};
},
[closeCommandMenu],
Expand Down Expand Up @@ -201,6 +205,8 @@ export const useCommandMenu = () => {
title: newNavigationStackItem?.pageTitle,
Icon: newNavigationStackItem?.pageIcon,
});

set(hasUserSelectedCommandState, false);
};
}, []);

Expand Down Expand Up @@ -270,6 +276,8 @@ export const useCommandMenu = () => {
title: undefined,
Icon: undefined,
});

set(hasUserSelectedCommandState, false);
};
},
[copyContextStoreStates],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createState } from 'twenty-ui';

export const hasUserSelectedCommandState = createState({
key: 'hasUserSelectedCommandState',
defaultValue: false,
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export const SelectableList = ({
selectableItemIdArray,
selectableItemIdMatrix,
onEnter,
onSelect,
}: SelectableListProps) => {
useSelectableListHotKeys(selectableListId, hotkeyScope);
useSelectableListHotKeys(selectableListId, hotkeyScope, onSelect);

const { setSelectableItemIds, setSelectableListOnEnter, setSelectedItemId } =
useSelectableList(selectableListId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Direction = 'up' | 'down' | 'left' | 'right';
export const useSelectableListHotKeys = (
scopeId: string,
hotkeyScope: string,
onSelect?: (itemId: string) => void,
) => {
const findPosition = (
selectableItemIds: string[][],
Expand Down Expand Up @@ -105,14 +106,20 @@ export const useSelectableListHotKeys = (
if (isNonEmptyString(nextId)) {
set(isSelectedItemIdSelector(nextId), true);
set(selectedItemIdState, nextId);
onSelect?.(nextId);
}

if (isNonEmptyString(selectedItemId)) {
set(isSelectedItemIdSelector(selectedItemId), false);
}
}
},
[isSelectedItemIdSelector, selectableItemIdsState, selectedItemIdState],
[
isSelectedItemIdSelector,
onSelect,
selectableItemIdsState,
selectedItemIdState,
],
);

useScopedHotkeys(Key.ArrowUp, () => handleSelect('up'), hotkeyScope, []);
Expand Down
Loading