Files
twenty/packages/twenty-front/src/modules/ui/layout/dropdown/hooks/useDropdown.ts
Thomas Trompette 6bae6fcdce Migrate record table to scope map (#3363)
* Migrate record table to scope map

* Update record scope id to record id

* Remove todos and fix edit mode

* Fix perf

* Fix tests

* Fix tests

---------

Co-authored-by: Thomas Trompette <thomast@twenty.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
2024-01-11 17:44:40 +01:00

63 lines
1.4 KiB
TypeScript

import { useRecoilState } from 'recoil';
import { useDropdownStates } from '@/ui/layout/dropdown/hooks/internal/useDropdownStates';
import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope';
export const useDropdown = (dropdownId?: string) => {
const {
scopeId,
dropdownHotkeyScopeState,
dropdownWidthState,
isDropdownOpenState,
} = useDropdownStates({
dropdownScopeId: `${dropdownId}-scope`,
});
const {
setHotkeyScopeAndMemorizePreviousScope,
goBackToPreviousHotkeyScope,
} = usePreviousHotkeyScope();
const [dropdownHotkeyScope] = useRecoilState(dropdownHotkeyScopeState());
const [dropdownWidth, setDropdownWidth] =
useRecoilState(dropdownWidthState());
const [isDropdownOpen, setIsDropdownOpen] = useRecoilState(
isDropdownOpenState(),
);
const closeDropdown = () => {
goBackToPreviousHotkeyScope();
setIsDropdownOpen(false);
};
const openDropdown = () => {
setIsDropdownOpen(true);
if (dropdownHotkeyScope) {
setHotkeyScopeAndMemorizePreviousScope(
dropdownHotkeyScope.scope,
dropdownHotkeyScope.customScopes,
);
}
};
const toggleDropdown = () => {
if (isDropdownOpen) {
closeDropdown();
} else {
openDropdown();
}
};
return {
scopeId,
isDropdownOpen: isDropdownOpen,
closeDropdown,
toggleDropdown,
openDropdown,
dropdownWidth,
setDropdownWidth,
};
};