- Created a state `hasUserSelectedCommandState` : This state is set to `true` when the user selects an element in the command menu list. It is set to false upon redirection or when the command menu is closed. - Modified `CommandMenuDefaultSelectionEffect` to have the expected selection behavior for the command menu
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import { hasUserSelectedCommandState } from '@/command-menu/states/hasUserSelectedCommandState';
|
|
import { useSelectableList } from '@/ui/layout/selectable-list/hooks/useSelectableList';
|
|
import { useEffect } from 'react';
|
|
import { useRecoilValue } from 'recoil';
|
|
import { isDefined } from 'twenty-shared';
|
|
|
|
export const CommandMenuDefaultSelectionEffect = ({
|
|
selectableItemIds,
|
|
}: {
|
|
selectableItemIds: string[];
|
|
}) => {
|
|
const { setSelectedItemId, selectedItemIdState } =
|
|
useSelectableList('command-menu-list');
|
|
|
|
const selectedItemId = useRecoilValue(selectedItemIdState);
|
|
|
|
const hasUserSelectedCommand = useRecoilValue(hasUserSelectedCommandState);
|
|
|
|
useEffect(() => {
|
|
if (
|
|
isDefined(selectedItemId) &&
|
|
selectableItemIds.includes(selectedItemId) &&
|
|
hasUserSelectedCommand
|
|
) {
|
|
return;
|
|
}
|
|
|
|
if (selectableItemIds.length > 0) {
|
|
setSelectedItemId(selectableItemIds[0]);
|
|
}
|
|
}, [
|
|
hasUserSelectedCommand,
|
|
selectableItemIds,
|
|
selectedItemId,
|
|
setSelectedItemId,
|
|
]);
|
|
|
|
return null;
|
|
};
|