* renaming * renaming * create getDropdownScopeInjectors * update useDropdown * create internal hooks folder * update record-table states to be scoped states * update record-table selectors to be scoped selectors * create utils scope injector * refactor record-table wip * refactor record-table wip * wip * inject scopeId in selectors * update intenal hooks * update intenal hooks * update intenal hooks * update intenal hooks * update intenal hooks * update intenal hooks * update internal hooks * update internal hooks * update internal hooks * update internal hooks * update useTableColumns * update states and hooks * refactoring * refactoring * refactoring * refactoring * refactoring * refactoring * refactoring * refactoring * refactoring * fix scopeId not in context * fix lint errors * fix error in story * fix errors: wip * fix errors * fix error * fix jest test * fix scopeId not defined * fix jest test * Bug fixes --------- Co-authored-by: Charles Bochet <charles@twenty.com>
83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { useRecoilCallback, useSetRecoilState } from 'recoil';
|
|
|
|
import { useSelectableList } from '@/ui/layout/selectable-list/hooks/useSelectableList';
|
|
import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope';
|
|
import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope';
|
|
|
|
import { commandMenuCommands } from '../constants/commandMenuCommands';
|
|
import { commandMenuCommandsState } from '../states/commandMenuCommandsState';
|
|
import { isCommandMenuOpenedState } from '../states/isCommandMenuOpenedState';
|
|
import { Command } from '../types/Command';
|
|
|
|
export const useCommandMenu = () => {
|
|
const navigate = useNavigate();
|
|
const setIsCommandMenuOpened = useSetRecoilState(isCommandMenuOpenedState);
|
|
const setCommands = useSetRecoilState(commandMenuCommandsState);
|
|
const { resetSelectedItem } = useSelectableList('command-menu-list');
|
|
const {
|
|
setHotkeyScopeAndMemorizePreviousScope,
|
|
goBackToPreviousHotkeyScope,
|
|
} = usePreviousHotkeyScope();
|
|
|
|
const openCommandMenu = () => {
|
|
setIsCommandMenuOpened(true);
|
|
setHotkeyScopeAndMemorizePreviousScope(AppHotkeyScope.CommandMenuOpen);
|
|
};
|
|
|
|
const closeCommandMenu = () => {
|
|
setIsCommandMenuOpened(false);
|
|
resetSelectedItem();
|
|
goBackToPreviousHotkeyScope();
|
|
};
|
|
|
|
const toggleCommandMenu = useRecoilCallback(({ snapshot }) => async () => {
|
|
const isCommandMenuOpened = snapshot
|
|
.getLoadable(isCommandMenuOpenedState)
|
|
.getValue();
|
|
|
|
if (isCommandMenuOpened) {
|
|
closeCommandMenu();
|
|
} else {
|
|
openCommandMenu();
|
|
}
|
|
});
|
|
|
|
const addToCommandMenu = useCallback(
|
|
(addCommand: Command[]) => {
|
|
setCommands((prev) => [...prev, ...addCommand]);
|
|
},
|
|
[setCommands],
|
|
);
|
|
|
|
const setToIntitialCommandMenu = () => {
|
|
setCommands(commandMenuCommands);
|
|
};
|
|
|
|
const onItemClick = useCallback(
|
|
(onClick?: () => void, to?: string) => {
|
|
toggleCommandMenu();
|
|
|
|
if (onClick) {
|
|
onClick();
|
|
return;
|
|
}
|
|
if (to) {
|
|
navigate(to);
|
|
return;
|
|
}
|
|
},
|
|
[navigate, toggleCommandMenu],
|
|
);
|
|
|
|
return {
|
|
openCommandMenu,
|
|
closeCommandMenu,
|
|
toggleCommandMenu,
|
|
addToCommandMenu,
|
|
onItemClick,
|
|
setToIntitialCommandMenu,
|
|
};
|
|
};
|