Merge branch '22130-all-processes-paging' refs #22130
[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 import { horizontalMenuDivider, verticalMenuDivider } from './actions/context-menu-divider';
9 import { MultiSelectMenuAction } from 'views-components/multiselect-toolbar/ms-menu-actions';
10
11 export enum ContextMenuKind {
12     API_CLIENT_AUTHORIZATION = "ApiClientAuthorization",
13     ROOT_PROJECT = "RootProject",
14     ROOT_PROJECT_ADMIN = "RootProjectAdmin",
15     PROJECT = "Project",
16     FILTER_GROUP = "FilterGroup",
17     READONLY_PROJECT = "ReadOnlyProject",
18     FROZEN_PROJECT = "FrozenProject",
19     FROZEN_PROJECT_ADMIN = "FrozenProjectAdmin",
20     PROJECT_ADMIN = "ProjectAdmin",
21     FILTER_GROUP_ADMIN = "FilterGroupAdmin",
22     RESOURCE = "Resource",
23     FAVORITE = "Favorite",
24     TRASH = "Trash",
25     COLLECTION_FILES = "CollectionFiles",
26     COLLECTION_FILES_MULTIPLE = "CollectionFilesMultiple",
27     READONLY_COLLECTION_FILES = "ReadOnlyCollectionFiles",
28     READONLY_COLLECTION_FILES_MULTIPLE = "ReadOnlyCollectionFilesMultiple",
29     COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
30     COLLECTION_FILE_ITEM = "CollectionFileItem",
31     COLLECTION_DIRECTORY_ITEM = "CollectionDirectoryItem",
32     READONLY_COLLECTION_FILE_ITEM = "ReadOnlyCollectionFileItem",
33     READONLY_COLLECTION_DIRECTORY_ITEM = "ReadOnlyCollectionDirectoryItem",
34     COLLECTION = "Collection",
35     COLLECTION_ADMIN = "CollectionAdmin",
36     READONLY_COLLECTION = "ReadOnlyCollection",
37     OLD_VERSION_COLLECTION = "OldVersionCollection",
38     TRASHED_COLLECTION = "TrashedCollection",
39     PROCESS = "Process",
40     RUNNING_PROCESS_ADMIN = "RunningProcessAdmin",
41     PROCESS_ADMIN = "ProcessAdmin",
42     RUNNING_PROCESS_RESOURCE = "RunningProcessResource",
43     PROCESS_RESOURCE = "ProcessResource",
44     READONLY_PROCESS_RESOURCE = "ReadOnlyProcessResource",
45     PROCESS_LOGS = "ProcessLogs",
46     REPOSITORY = "Repository",
47     SSH_KEY = "SshKey",
48     VIRTUAL_MACHINE = "VirtualMachine",
49     KEEP_SERVICE = "KeepService",
50     USER = "User",
51     USER_DETAILS = "UserDetails",
52     GROUPS = "Group",
53     GROUP_MEMBER = "GroupMember",
54     PERMISSION_EDIT = "PermissionEdit",
55     LINK = "Link",
56     WORKFLOW = "Workflow",
57     READONLY_WORKFLOW = "ReadOnlyWorkflow",
58     SEARCH_RESULTS = "SearchResults",
59     MULTI = "Multi",
60 }
61
62
63
64 const processOrder = [
65     ContextMenuActionNames.VIEW_DETAILS,
66     ContextMenuActionNames.OPEN_IN_NEW_TAB,
67     ContextMenuActionNames.OUTPUTS,
68     ContextMenuActionNames.API_DETAILS,
69     ContextMenuActionNames.DIVIDER,
70     ContextMenuActionNames.EDIT_PROCESS,
71     ContextMenuActionNames.COPY_AND_RERUN_PROCESS,
72     ContextMenuActionNames.CANCEL,
73     ContextMenuActionNames.REMOVE,
74     ContextMenuActionNames.DIVIDER,
75     ContextMenuActionNames.ADD_TO_FAVORITES,
76     ContextMenuActionNames.ADD_TO_PUBLIC_FAVORITES,
77 ];
78
79 const projectOrder = [
80     ContextMenuActionNames.VIEW_DETAILS,
81     ContextMenuActionNames.OPEN_IN_NEW_TAB,
82     ContextMenuActionNames.COPY_LINK_TO_CLIPBOARD,
83     ContextMenuActionNames.OPEN_WITH_3RD_PARTY_CLIENT,
84     ContextMenuActionNames.API_DETAILS,
85     ContextMenuActionNames.DIVIDER,
86     ContextMenuActionNames.SHARE,
87     ContextMenuActionNames.NEW_PROJECT,
88     ContextMenuActionNames.EDIT_PROJECT,
89     ContextMenuActionNames.MOVE_TO,
90     ContextMenuActionNames.MOVE_TO_TRASH,
91     ContextMenuActionNames.DIVIDER,
92     ContextMenuActionNames.FREEZE_PROJECT,
93     ContextMenuActionNames.ADD_TO_FAVORITES,
94     ContextMenuActionNames.ADD_TO_PUBLIC_FAVORITES,
95 ];
96
97 const collectionOrder = [
98     ContextMenuActionNames.VIEW_DETAILS,
99     ContextMenuActionNames.OPEN_IN_NEW_TAB,
100     ContextMenuActionNames.COPY_LINK_TO_CLIPBOARD,
101     ContextMenuActionNames.OPEN_WITH_3RD_PARTY_CLIENT,
102     ContextMenuActionNames.API_DETAILS,
103     ContextMenuActionNames.DIVIDER,
104     ContextMenuActionNames.SHARE,
105     ContextMenuActionNames.EDIT_COLLECTION,
106     ContextMenuActionNames.MOVE_TO,
107     ContextMenuActionNames.MAKE_A_COPY,
108     ContextMenuActionNames.MOVE_TO_TRASH,
109     ContextMenuActionNames.DIVIDER,
110     ContextMenuActionNames.ADD_TO_FAVORITES,
111     ContextMenuActionNames.ADD_TO_PUBLIC_FAVORITES,
112 ];
113
114 const workflowOrder = [
115     ContextMenuActionNames.VIEW_DETAILS,
116     ContextMenuActionNames.OPEN_IN_NEW_TAB,
117     ContextMenuActionNames.COPY_LINK_TO_CLIPBOARD,
118     ContextMenuActionNames.API_DETAILS,
119     ContextMenuActionNames.DIVIDER,
120     ContextMenuActionNames.RUN_WORKFLOW,
121     ContextMenuActionNames.DELETE_WORKFLOW,
122 ]
123
124 const rootProjectOrder = [
125     ContextMenuActionNames.VIEW_DETAILS,
126     ContextMenuActionNames.USER_ACCOUNT,
127     ContextMenuActionNames.API_DETAILS,
128 ];
129
130 const defaultMultiOrder = [
131     ContextMenuActionNames.MOVE_TO,
132     ContextMenuActionNames.MAKE_A_COPY,
133     ContextMenuActionNames.MOVE_TO_TRASH,
134 ];
135
136 const kindToOrder: Record<string, ContextMenuActionNames[]> = {
137     [ContextMenuKind.MULTI]: defaultMultiOrder,
138
139     [ContextMenuKind.PROCESS]: processOrder,
140     [ContextMenuKind.PROCESS_ADMIN]: processOrder,
141     [ContextMenuKind.PROCESS_RESOURCE]: processOrder,
142     [ContextMenuKind.RUNNING_PROCESS_ADMIN]: processOrder,
143     [ContextMenuKind.RUNNING_PROCESS_RESOURCE]: processOrder,
144
145     [ContextMenuKind.PROJECT]: projectOrder,
146     [ContextMenuKind.PROJECT_ADMIN]: projectOrder,
147     [ContextMenuKind.FROZEN_PROJECT]: projectOrder,
148     [ContextMenuKind.FROZEN_PROJECT_ADMIN]: projectOrder,
149
150     [ContextMenuKind.COLLECTION]: collectionOrder,
151     [ContextMenuKind.COLLECTION_ADMIN]: collectionOrder,
152     [ContextMenuKind.READONLY_COLLECTION]: collectionOrder,
153     [ContextMenuKind.OLD_VERSION_COLLECTION]: collectionOrder,
154
155     [ContextMenuKind.WORKFLOW]: workflowOrder,
156     [ContextMenuKind.READONLY_WORKFLOW]: workflowOrder,
157
158     [ContextMenuKind.GROUPS]: projectOrder,
159
160     [ContextMenuKind.FILTER_GROUP]: projectOrder,
161     [ContextMenuKind.FILTER_GROUP_ADMIN]: projectOrder,
162
163     [ContextMenuKind.ROOT_PROJECT]: rootProjectOrder,
164     [ContextMenuKind.ROOT_PROJECT_ADMIN]: rootProjectOrder,
165 };
166
167 export const menuDirection = {
168     VERTICAL: 'vertical',
169     HORIZONTAL: 'horizontal'
170 }
171
172 export const sortMenuItems = (menuKind: ContextMenuKind, menuItems: ContextMenuAction[], orthagonality: string): ContextMenuAction[] | MultiSelectMenuAction[] => {
173
174     const preferredOrder = kindToOrder[menuKind];
175     //if no specified order, sort by name
176     if (!preferredOrder) return menuItems.sort(sortByProperty("name"));
177
178     const bucketMap = new Map();
179     const leftovers: ContextMenuAction[] = [];
180
181     // if we have multiple dividers, we need each of them to have a different "name" property
182     let count = 0;
183
184     preferredOrder.forEach((name) => {
185         if (name === ContextMenuActionNames.DIVIDER) {
186             count++;
187             bucketMap.set(`${name}-${count}`, orthagonality === menuDirection.VERTICAL ? verticalMenuDivider : horizontalMenuDivider)
188         } else {
189             bucketMap.set(name, null)
190         }
191     });
192     [...menuItems].forEach((item) => {
193         if (bucketMap.has(item.name)) bucketMap.set(item.name, item);
194         else leftovers.push(item);
195     });
196
197     return Array.from(bucketMap.values()).concat(leftovers).filter((item) => item !== null).reduce((acc, val)=>{
198         return acc.at(-1)?.name === "Divider" && val.name === "Divider" ? acc : acc.concat(val)
199     }, []);
200 };