Files
twenty_crm/front/src/modules/ui/dropdown/hooks/useDropdownButton.ts
Charles Bochet 7ce03ffdd1 Refactor tests command menu (#1702)
* Fix tests

* Refactor tests command menu

* Improve tests

* Fix optimistic render breaking tests
2023-09-21 11:53:36 -07:00

58 lines
1.7 KiB
TypeScript

import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope';
import { useRecoilScopedFamilyState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedFamilyState';
import { dropdownButtonHotkeyScopeScopedFamilyState } from '../states/dropdownButtonHotkeyScopeScopedFamilyState';
import { isDropdownButtonOpenScopedFamilyState } from '../states/isDropdownButtonOpenScopedFamilyState';
import { DropdownRecoilScopeContext } from '../states/recoil-scope-contexts/DropdownRecoilScopeContext';
export const useDropdownButton = ({ dropdownId }: { dropdownId: string }) => {
const {
setHotkeyScopeAndMemorizePreviousScope,
goBackToPreviousHotkeyScope,
} = usePreviousHotkeyScope();
const [isDropdownButtonOpen, setIsDropdownButtonOpen] =
useRecoilScopedFamilyState(
isDropdownButtonOpenScopedFamilyState,
dropdownId,
DropdownRecoilScopeContext,
);
const [dropdownButtonHotkeyScope] = useRecoilScopedFamilyState(
dropdownButtonHotkeyScopeScopedFamilyState,
dropdownId,
DropdownRecoilScopeContext,
);
const closeDropdownButton = () => {
goBackToPreviousHotkeyScope();
setIsDropdownButtonOpen(false);
};
const openDropdownButton = () => {
setIsDropdownButtonOpen(true);
if (dropdownButtonHotkeyScope) {
setHotkeyScopeAndMemorizePreviousScope(
dropdownButtonHotkeyScope.scope,
dropdownButtonHotkeyScope.customScopes,
);
}
};
const toggleDropdownButton = () => {
if (isDropdownButtonOpen) {
closeDropdownButton();
} else {
openDropdownButton();
}
};
return {
isDropdownButtonOpen,
closeDropdownButton,
toggleDropdownButton,
openDropdownButton,
};
};