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