1091bc657d
Refactored the `CommandMenu` component to make it more readable and easier to refactor. The file was way too big so I introduced a few hooks and eliminated code duplication. Introduced: - `useMatchCommands` hook to match commands with the search - `useCommandMenuCommands` which returns all command menu commands - `useMatchingCommandMenuCommands` to return the commands matched with the search - `CommandMenuContainer` to simplify the `DefaultLayout` - Unmounted the `CommandMenu` when it wasn't opened to improve performances I also introduced a new behavior: Automatically select the first item when opening the command menu: https://github.com/user-attachments/assets/4b683d49-570e-47c9-8939-99f42ed8691c
28 lines
735 B
TypeScript
28 lines
735 B
TypeScript
import { useSelectableList } from '@/ui/layout/selectable-list/hooks/useSelectableList';
|
|
import { useEffect } from 'react';
|
|
import { useRecoilValue } from 'recoil';
|
|
import { isDefined } from 'twenty-ui';
|
|
|
|
export const CommandMenuDefaultSelectionEffect = ({
|
|
selectableItemIds,
|
|
}: {
|
|
selectableItemIds: string[];
|
|
}) => {
|
|
const { setSelectedItemId, selectedItemIdState } =
|
|
useSelectableList('command-menu-list');
|
|
|
|
const selectedItemId = useRecoilValue(selectedItemIdState);
|
|
|
|
useEffect(() => {
|
|
if (isDefined(selectedItemId)) {
|
|
return;
|
|
}
|
|
|
|
if (selectableItemIds.length > 0) {
|
|
setSelectedItemId(selectableItemIds[0]);
|
|
}
|
|
}, [selectableItemIds, selectedItemId, setSelectedItemId]);
|
|
|
|
return null;
|
|
};
|