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