Files
twenty/front/src/modules/hotkeys/hooks/useSetHotkeysScope.ts
T
Lucas Bordeau 5f98b70c6a Fix/scope hotkeys (#581)
* WIP

* asd

* Fix

* Fix lint

* Removed console log

* asd

* Removed isDefined

* Fix/debounce company card onchange (#580)

* Add internal state and debounce for editable text card

* Use debounce for date fields too

* Update refetch

* Nit

* Removed comments

* Ménage

---------

Co-authored-by: Emilien Chauvet <emilien.chauvet.enpc@gmail.com>
2023-07-11 01:53:46 +00:00

60 lines
1.8 KiB
TypeScript

import { useRecoilCallback } from 'recoil';
import { isDefined } from '@/utils/type-guards/isDefined';
import { DEFAULT_HOTKEYS_SCOPE_CUSTOM_SCOPES } from '../constants';
import { currentHotkeysScopeState } from '../states/internal/currentHotkeysScopeState';
import { CustomHotkeysScopes } from '../types/internal/CustomHotkeysScope';
export function useSetHotkeysScope() {
return useRecoilCallback(
({ snapshot, set }) =>
async (hotkeysScopeToSet: string, customScopes?: CustomHotkeysScopes) => {
const currentHotkeysScope = await snapshot.getPromise(
currentHotkeysScopeState,
);
function isCustomScopesEqual(
customScopesA: CustomHotkeysScopes | undefined,
customScopesB: CustomHotkeysScopes | undefined,
) {
return (
customScopesA?.commandMenu === customScopesB?.commandMenu &&
customScopesA?.goto === customScopesB?.goto
);
}
if (currentHotkeysScope.scope === hotkeysScopeToSet) {
if (!isDefined(customScopes)) {
if (
isCustomScopesEqual(
currentHotkeysScope?.customScopes,
DEFAULT_HOTKEYS_SCOPE_CUSTOM_SCOPES,
)
) {
return;
}
} else {
if (
isCustomScopesEqual(
currentHotkeysScope?.customScopes,
customScopes,
)
) {
return;
}
}
}
set(currentHotkeysScopeState, {
scope: hotkeysScopeToSet,
customScopes: {
commandMenu: customScopes?.commandMenu ?? true,
goto: customScopes?.goto ?? false,
},
});
},
[],
);
}