21448: combined contextmenunames and mstoolbaractionnames since they were mostly...
[arvados.git] / services / workbench2 / src / views-components / multiselect-toolbar / ms-menu-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from 'redux';
6 import { IconType } from 'components/icon/icon';
7 import { ResourcesState } from 'store/resources/resources';
8 import { FavoritesState } from 'store/favorites/favorites-reducer';
9 import { ContextMenuResource } from 'store/context-menu/context-menu-actions';
10 import { AddFavoriteIcon, AdvancedIcon, DetailsIcon, OpenIcon, PublicFavoriteIcon, RemoveFavoriteIcon, ShareIcon } from 'components/icon/icon';
11 import { checkFavorite } from 'store/favorites/favorites-reducer';
12 import { toggleFavorite } from 'store/favorites/favorites-actions';
13 import { favoritePanelActions } from 'store/favorite-panel/favorite-panel-action';
14 import { openInNewTabAction } from 'store/open-in-new-tab/open-in-new-tab.actions';
15 import { toggleDetailsPanel } from 'store/details-panel/details-panel-action';
16 import { openAdvancedTabDialog } from 'store/advanced-tab/advanced-tab';
17 import { openSharingDialog } from 'store/sharing-dialog/sharing-dialog-actions';
18 import { togglePublicFavorite } from "store/public-favorites/public-favorites-actions";
19 import { publicFavoritePanelActions } from "store/public-favorites-panel/public-favorites-action";
20 import { PublicFavoritesState } from 'store/public-favorites/public-favorites-reducer';
21 import { ContextMenuActionNames } from 'views-components/context-menu/context-menu-action-set';
22
23 export type MultiSelectMenuAction = {
24     name: string;
25     icon: IconType;
26     hasAlts: boolean;
27     altName?: string;
28     altIcon?: IconType;
29     isForMulti: boolean;
30     useAlts?: (uuid: string | null, iconProps: {resources: ResourcesState, favorites: FavoritesState, publicFavorites: PublicFavoritesState}) => boolean;
31     execute(dispatch: Dispatch, resources: ContextMenuResource[], state?: any): void;
32     adminOnly?: boolean;
33 };
34
35 export type MultiSelectMenuActionSet = MultiSelectMenuAction[][];
36
37 const { ADD_TO_FAVORITES, ADD_TO_PUBLIC_FAVORITES, OPEN_IN_NEW_TAB, VIEW_DETAILS, API_DETAILS, SHARE } = ContextMenuActionNames;
38
39 const msToggleFavoriteAction: MultiSelectMenuAction = {
40     name: ADD_TO_FAVORITES,
41     icon: AddFavoriteIcon,
42     hasAlts: true,
43     altName: 'Remove from Favorites',
44     altIcon: RemoveFavoriteIcon,
45     isForMulti: false,
46     useAlts: (uuid: string, iconProps) => {
47         return checkFavorite(uuid, iconProps.favorites);
48     },
49     execute: (dispatch, resources) => {
50         dispatch<any>(toggleFavorite(resources[0])).then(() => {
51             dispatch(favoritePanelActions.REQUEST_ITEMS());
52         });
53     },
54 };
55
56 const msOpenInNewTabMenuAction: MultiSelectMenuAction  = {
57     name: OPEN_IN_NEW_TAB,
58     icon: OpenIcon,
59     hasAlts: false,
60     isForMulti: false,
61     execute: (dispatch, resources) => {
62         dispatch<any>(openInNewTabAction(resources[0]));
63     },
64 };
65
66 const msViewDetailsAction: MultiSelectMenuAction  = {
67     name: VIEW_DETAILS,
68     icon: DetailsIcon,
69     hasAlts: false,
70     isForMulti: false,
71     execute: (dispatch) => {
72         dispatch<any>(toggleDetailsPanel());
73     },
74 };
75
76 const msAdvancedAction: MultiSelectMenuAction  = {
77     name: API_DETAILS,
78     icon: AdvancedIcon,
79     hasAlts: false,
80     isForMulti: false,
81     execute: (dispatch, resources) => {
82         dispatch<any>(openAdvancedTabDialog(resources[0].uuid));
83     },
84 };
85
86 const msShareAction: MultiSelectMenuAction  = {
87     name: SHARE,
88     icon: ShareIcon,
89     hasAlts: false,
90     isForMulti: false,
91     execute: (dispatch, resources) => {
92         dispatch<any>(openSharingDialog(resources[0].uuid));
93     },
94 };
95
96 const msTogglePublicFavoriteAction: MultiSelectMenuAction = {
97     name: ADD_TO_PUBLIC_FAVORITES,
98     icon: PublicFavoriteIcon,
99     hasAlts: true,
100     altName: 'Remove from public favorites',
101     altIcon: PublicFavoriteIcon,
102     isForMulti: false,
103     useAlts: (uuid: string, iconProps) => {
104         return iconProps.publicFavorites[uuid] === true
105     },
106     execute: (dispatch, resources) => {
107         dispatch<any>(togglePublicFavorite(resources[0])).then(() => {
108             dispatch(publicFavoritePanelActions.REQUEST_ITEMS());
109         });
110     },
111 };
112
113 export const msCommonActionSet = [
114     msToggleFavoriteAction,
115     msOpenInNewTabMenuAction,
116     msViewDetailsAction,
117     msAdvancedAction,
118     msShareAction,
119     msTogglePublicFavoriteAction
120 ];