Merge branch '21705-go-deps-update-all'
[arvados.git] / services / workbench2 / src / views-components / projects-tree-picker / projects-tree-picker.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { Dispatch } from 'redux';
7 import { connect, DispatchProp } from 'react-redux';
8 import { RootState } from 'store/store';
9 import { values, pipe } from 'lodash/fp';
10 import { HomeTreePicker } from 'views-components/projects-tree-picker/home-tree-picker';
11 import { SharedTreePicker } from 'views-components/projects-tree-picker/shared-tree-picker';
12 import { FavoritesTreePicker } from 'views-components/projects-tree-picker/favorites-tree-picker';
13 import { SearchProjectsPicker } from 'views-components/projects-tree-picker/search-projects-picker';
14 import {
15     getProjectsTreePickerIds, treePickerActions, treePickerSearchActions, initProjectsTreePicker,
16     SHARED_PROJECT_ID, FAVORITES_PROJECT_ID
17 } from 'store/tree-picker/tree-picker-actions';
18 import { TreeItem } from 'components/tree/tree';
19 import { ProjectsTreePickerItem } from 'store/tree-picker/tree-picker-middleware';
20 import { PublicFavoritesTreePicker } from './public-favorites-tree-picker';
21 import { SearchInput } from 'components/search-input/search-input';
22 import { withStyles, StyleRulesCallback, WithStyles } from '@material-ui/core';
23 import { ArvadosTheme } from 'common/custom-theme';
24
25 export interface ToplevelPickerProps {
26     currentUuids?: string[];
27     pickerId: string;
28     cascadeSelection: boolean;
29     includeCollections?: boolean;
30     includeDirectories?: boolean;
31     includeFiles?: boolean;
32     showSelection?: boolean;
33     options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
34     toggleItemActive?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<ProjectsTreePickerItem>, pickerId: string) => void;
35     toggleItemSelection?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<ProjectsTreePickerItem>, pickerId: string) => void;
36 }
37
38 interface ProjectsTreePickerSearchProps {
39     projectSearch: string;
40     collectionFilter: string;
41 }
42
43 interface ProjectsTreePickerActionProps {
44     onProjectSearch: (value: string) => void;
45     onCollectionFilter: (value: string) => void;
46 }
47
48 const mapStateToProps = (state: RootState, props: ToplevelPickerProps): ProjectsTreePickerSearchProps => {
49     const { search } = getProjectsTreePickerIds(props.pickerId);
50     return {
51         ...props,
52         projectSearch: state.treePickerSearch.projectSearchValues[search],
53         collectionFilter: state.treePickerSearch.collectionFilterValues[search],
54     };
55 };
56
57 const mapDispatchToProps = (dispatch: Dispatch, props: ToplevelPickerProps): (ProjectsTreePickerActionProps & DispatchProp) => {
58     const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(props.pickerId);
59     const params = {
60         includeCollections: props.includeCollections,
61         includeDirectories: props.includeDirectories,
62         includeFiles: props.includeFiles,
63         options: props.options
64     };
65     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: home, params }));
66     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: shared, params }));
67     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: favorites, params }));
68     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: publicFavorites, params }));
69     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: search, params }));
70
71     return {
72         onProjectSearch: (projectSearchValue: string) => dispatch(treePickerSearchActions.SET_TREE_PICKER_PROJECT_SEARCH({ pickerId: search, projectSearchValue })),
73         onCollectionFilter: (collectionFilterValue: string) => {
74             dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: home, collectionFilterValue }));
75             dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: shared, collectionFilterValue }));
76             dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: favorites, collectionFilterValue }));
77             dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: publicFavorites, collectionFilterValue }));
78             dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: search, collectionFilterValue }));
79         },
80         dispatch
81     }
82 };
83
84 type CssRules = 'pickerHeight' | 'searchFlex' | 'scrolledBox';
85
86 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
87     pickerHeight: {
88         display: "flex",
89         flexDirection: "column",
90         overflowY: "auto",
91     },
92     searchFlex: {
93         display: "flex",
94         justifyContent: "space-around",
95         paddingBottom: "1em"
96     },
97     scrolledBox: {
98         overflow: "scroll"
99     }
100 });
101
102 type ProjectsTreePickerCombinedProps = ToplevelPickerProps & ProjectsTreePickerSearchProps & ProjectsTreePickerActionProps & DispatchProp & WithStyles<CssRules>;
103
104 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(
105     withStyles(styles)(
106         class FileInputComponent extends React.Component<ProjectsTreePickerCombinedProps> {
107
108             componentDidMount() {
109                 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
110
111                 const preloadParams = this.props.currentUuids ? {
112                     selectedItemUuids: this.props.currentUuids,
113                     includeDirectories: !!this.props.includeDirectories,
114                     includeFiles: !!this.props.includeFiles,
115                     multi: !!this.props.showSelection,
116                 } : undefined;
117                 this.props.dispatch<any>(initProjectsTreePicker(this.props.pickerId, preloadParams));
118
119                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_PROJECT_SEARCH({ pickerId: search, projectSearchValue: "" }));
120                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: search, collectionFilterValue: "" }));
121                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: home, collectionFilterValue: "" }));
122                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: shared, collectionFilterValue: "" }));
123                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: favorites, collectionFilterValue: "" }));
124                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: publicFavorites, collectionFilterValue: "" }));
125             }
126
127             componentWillUnmount() {
128                 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
129                 // Release all the state, we don't need it to hang around forever.
130                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: search }));
131                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: home }));
132                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: shared }));
133                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: favorites }));
134                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: publicFavorites }));
135             }
136
137             render() {
138                 const pickerId = this.props.pickerId;
139                 const onProjectSearch = this.props.onProjectSearch;
140                 const onCollectionFilter = this.props.onCollectionFilter;
141
142                 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(pickerId);
143                 const relatedTreePickers = getRelatedTreePickers(pickerId);
144                 const p = {
145                     cascadeSelection: this.props.cascadeSelection,
146                     includeCollections: this.props.includeCollections,
147                     includeDirectories: this.props.includeDirectories,
148                     includeFiles: this.props.includeFiles,
149                     showSelection: this.props.showSelection,
150                     options: this.props.options,
151                     toggleItemActive: this.props.toggleItemActive,
152                     toggleItemSelection: this.props.toggleItemSelection,
153                     relatedTreePickers,
154                     disableActivation,
155                 };
156                 return <div className={this.props.classes.pickerHeight} >
157                     <span className={this.props.classes.searchFlex}>
158                         <SearchInput value="" label="Search for a Project" selfClearProp='' onSearch={onProjectSearch} debounce={500} />
159                         {this.props.includeCollections &&
160                             <SearchInput value="" label="Filter Collections list in Projects" selfClearProp='' onSearch={onCollectionFilter} debounce={500} />}
161                     </span>
162
163                     <div className={this.props.classes.scrolledBox}>
164                         {this.props.projectSearch ?
165                             <div data-cy="projects-tree-search-picker">
166                                 <SearchProjectsPicker {...p} pickerId={search} />
167                             </div>
168                             :
169                             <>
170                                 <div data-cy="projects-tree-home-tree-picker">
171                                     <HomeTreePicker {...p} pickerId={home} />
172                                 </div>
173                                 <div data-cy="projects-tree-shared-tree-picker">
174                                     <SharedTreePicker {...p} pickerId={shared} />
175                                 </div>
176                                 <div data-cy="projects-tree-public-favourites-tree-picker">
177                                     <PublicFavoritesTreePicker {...p} pickerId={publicFavorites} />
178                                 </div>
179                                 <div data-cy="projects-tree-favourites-tree-picker">
180                                     <FavoritesTreePicker {...p} pickerId={favorites} />
181                                 </div>
182                             </>}
183                     </div>
184                 </div >;
185             }
186         }));
187
188 const getRelatedTreePickers = pipe(getProjectsTreePickerIds, values);
189 const disableActivation = [SHARED_PROJECT_ID, FAVORITES_PROJECT_ID];