Refactor actions (#8761)
Closes #8737 - Refactored actions by creating hooks to add the possibility to register actions programatically. - Small fixes from #8610 review - Fixed shortcuts display inside the command menu - Removed `actionMenuEntriesComponentState` and introduced `actionMenuEntriesComponentSelector`
This commit is contained in:
@ -9,24 +9,17 @@ import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousH
|
||||
import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
|
||||
import { COMMAND_MENU_COMMANDS } from '@/command-menu/constants/CommandMenuCommands';
|
||||
import { contextStoreCurrentObjectMetadataIdComponentState } from '@/context-store/states/contextStoreCurrentObjectMetadataIdComponentState';
|
||||
import { contextStoreCurrentViewIdComponentState } from '@/context-store/states/contextStoreCurrentViewIdComponentState';
|
||||
import { contextStoreFiltersComponentState } from '@/context-store/states/contextStoreFiltersComponentState';
|
||||
import { contextStoreNumberOfSelectedRecordsComponentState } from '@/context-store/states/contextStoreNumberOfSelectedRecordsComponentState';
|
||||
import { contextStoreTargetedRecordsRuleComponentState } from '@/context-store/states/contextStoreTargetedRecordsRuleComponentState';
|
||||
import { mainContextStoreComponentInstanceIdState } from '@/context-store/states/mainContextStoreComponentInstanceId';
|
||||
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
||||
import { ALL_ICONS } from '@ui/display/icon/providers/internal/AllIcons';
|
||||
import { sortByProperty } from '~/utils/array/sortByProperty';
|
||||
import { commandMenuCommandsState } from '../states/commandMenuCommandsState';
|
||||
import { isCommandMenuOpenedState } from '../states/isCommandMenuOpenedState';
|
||||
import { Command, CommandType } 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,
|
||||
@ -161,36 +154,6 @@ export const useCommandMenu = () => {
|
||||
[closeCommandMenu, openCommandMenu],
|
||||
);
|
||||
|
||||
const addToCommandMenu = useCallback(
|
||||
(addCommand: Command[]) => {
|
||||
setCommands((prev) => [...prev, ...addCommand]);
|
||||
},
|
||||
[setCommands],
|
||||
);
|
||||
|
||||
const setObjectsInCommandMenu = (menuItems: ObjectMetadataItem[]) => {
|
||||
const formattedItems = [
|
||||
...[
|
||||
...menuItems.map(
|
||||
(item) =>
|
||||
({
|
||||
id: item.id,
|
||||
to: `/objects/${item.namePlural}`,
|
||||
label: `Go to ${item.labelPlural}`,
|
||||
type: CommandType.Navigate,
|
||||
firstHotKey: item.shortcut ? 'G' : undefined,
|
||||
secondHotKey: item.shortcut,
|
||||
Icon: ALL_ICONS[
|
||||
(item?.icon as keyof typeof ALL_ICONS) ?? 'IconArrowUpRight'
|
||||
],
|
||||
}) as Command,
|
||||
),
|
||||
].sort(sortByProperty('label', 'asc')),
|
||||
COMMAND_MENU_COMMANDS.settings,
|
||||
];
|
||||
setCommands(formattedItems);
|
||||
};
|
||||
|
||||
const onItemClick = useCallback(
|
||||
(onClick?: () => void, to?: string) => {
|
||||
toggleCommandMenu();
|
||||
@ -211,8 +174,6 @@ export const useCommandMenu = () => {
|
||||
openCommandMenu,
|
||||
closeCommandMenu,
|
||||
toggleCommandMenu,
|
||||
addToCommandMenu,
|
||||
onItemClick,
|
||||
setObjectsInCommandMenu,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user