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