7e14fb6ec12941b35218546e661b2321c420a9ff
[arvados.git] / services / workbench2 / src / views-components / context-menu / menu-item-sort.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ContextMenuAction } from './context-menu-action-set';
6 import { ContextMenuActionNames } from 'views-components/context-menu/context-menu-action-set';
7 import { sortByProperty } from 'common/array-utils'; 
8
9 export enum ContextMenuKind {
10     API_CLIENT_AUTHORIZATION = "ApiClientAuthorization",
11     ROOT_PROJECT = "RootProject",
12     PROJECT = "Project",
13     FILTER_GROUP = "FilterGroup",
14     READONLY_PROJECT = "ReadOnlyProject",
15     FROZEN_PROJECT = "FrozenProject",
16     FROZEN_PROJECT_ADMIN = "FrozenProjectAdmin",
17     PROJECT_ADMIN = "ProjectAdmin",
18     FILTER_GROUP_ADMIN = "FilterGroupAdmin",
19     RESOURCE = "Resource",
20     FAVORITE = "Favorite",
21     TRASH = "Trash",
22     COLLECTION_FILES = "CollectionFiles",
23     COLLECTION_FILES_MULTIPLE = "CollectionFilesMultiple",
24     READONLY_COLLECTION_FILES = "ReadOnlyCollectionFiles",
25     READONLY_COLLECTION_FILES_MULTIPLE = "ReadOnlyCollectionFilesMultiple",
26     COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
27     COLLECTION_FILE_ITEM = "CollectionFileItem",
28     COLLECTION_DIRECTORY_ITEM = "CollectionDirectoryItem",
29     READONLY_COLLECTION_FILE_ITEM = "ReadOnlyCollectionFileItem",
30     READONLY_COLLECTION_DIRECTORY_ITEM = "ReadOnlyCollectionDirectoryItem",
31     COLLECTION = "Collection",
32     COLLECTION_ADMIN = "CollectionAdmin",
33     READONLY_COLLECTION = "ReadOnlyCollection",
34     OLD_VERSION_COLLECTION = "OldVersionCollection",
35     TRASHED_COLLECTION = "TrashedCollection",
36     PROCESS = "Process",
37     RUNNING_PROCESS_ADMIN = "RunningProcessAdmin",
38     PROCESS_ADMIN = "ProcessAdmin",
39     RUNNING_PROCESS_RESOURCE = "RunningProcessResource",
40     PROCESS_RESOURCE = "ProcessResource",
41     READONLY_PROCESS_RESOURCE = "ReadOnlyProcessResource",
42     PROCESS_LOGS = "ProcessLogs",
43     REPOSITORY = "Repository",
44     SSH_KEY = "SshKey",
45     VIRTUAL_MACHINE = "VirtualMachine",
46     KEEP_SERVICE = "KeepService",
47     USER = "User",
48     GROUPS = "Group",
49     GROUP_MEMBER = "GroupMember",
50     PERMISSION_EDIT = "PermissionEdit",
51     LINK = "Link",
52     WORKFLOW = "Workflow",
53     READONLY_WORKFLOW = "ReadOnlyWorkflow",
54     SEARCH_RESULTS = "SearchResults",
55 }
56
57 const processOrder = [
58     ContextMenuActionNames.VIEW_DETAILS,
59     ContextMenuActionNames.OPEN_IN_NEW_TAB,
60     ContextMenuActionNames.OUTPUTS,
61     ContextMenuActionNames.API_DETAILS,
62     ContextMenuActionNames.EDIT_PROCESS,
63     ContextMenuActionNames.COPY_AND_RERUN_PROCESS,
64     ContextMenuActionNames.MOVE_TO,
65     ContextMenuActionNames.REMOVE,
66     ContextMenuActionNames.ADD_TO_FAVORITES,
67     ContextMenuActionNames.ADD_TO_PUBLIC_FAVORITES,
68 ];
69
70 const kindToOrder: Record<string, ContextMenuActionNames[]> = {
71     [ContextMenuKind.PROCESS_RESOURCE]: processOrder,
72 };
73
74 export const sortMenuItems = (menuKind: ContextMenuKind, menuItems: ContextMenuAction[]) => {
75     const order = kindToOrder[menuKind];
76     //if no specified order, sort by name
77     if (!order) return menuItems.sort(sortByProperty("name"));
78
79     const bucketMap = new Map();
80     const leftovers: ContextMenuAction[] = [];
81
82     order.forEach((name) => bucketMap.set(name, null));
83     menuItems.forEach((item) => {
84         if (bucketMap.has(item.name)) bucketMap.set(item.name, item);
85         else leftovers.push(item);
86     });
87     
88     return Array.from(bucketMap.values()).concat(leftovers).filter((item) => item !== null);
89 };