22141: polish file chooser layout
[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 } from '@mui/styles';
24 import withStyles from '@mui/styles/withStyles';
25 import { ArvadosTheme } from 'common/custom-theme';
26 import { ResourceKind } from 'models/resource';
27 import { DefaultView } from 'components/default-view/default-view';
28 import { ProjectDetailsComponent } from 'views-components/details-panel/project-details';
29 import { CollectionDetailsAttributes } from 'views/collection-panel/collection-panel';
30
31 export interface ToplevelPickerProps {
32         currentUuids?: string[];
33     pickerId: string;
34     cascadeSelection: boolean;
35     includeCollections?: boolean;
36     includeDirectories?: boolean;
37     includeFiles?: boolean;
38     showSelection?: boolean;
39     options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
40     toggleItemActive?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<ProjectsTreePickerItem>, pickerId: string) => void;
41     toggleItemSelection?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<ProjectsTreePickerItem>, pickerId: string) => void;
42 }
43
44 interface ProjectsTreePickerSearchProps {
45     projectSearch: string;
46     collectionFilter: string;
47 }
48
49 interface ProjectsTreePickerActionProps {
50     onProjectSearch: (value: string) => void;
51     onCollectionFilter: (value: string) => void;
52 }
53
54 const mapStateToProps = (state: RootState, props: ToplevelPickerProps): ProjectsTreePickerSearchProps => {
55     const { search } = getProjectsTreePickerIds(props.pickerId);
56     return {
57         ...props,
58         projectSearch: state.treePickerSearch.projectSearchValues[search] || state.treePickerSearch.collectionFilterValues[search],
59         collectionFilter: state.treePickerSearch.collectionFilterValues[search],
60     };
61 };
62
63 const mapDispatchToProps = (dispatch: Dispatch, props: ToplevelPickerProps): (ProjectsTreePickerActionProps & DispatchProp) => {
64     const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(props.pickerId);
65     const params = {
66         includeCollections: props.includeCollections,
67         includeDirectories: props.includeDirectories,
68         includeFiles: props.includeFiles,
69         options: props.options
70     };
71     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: home, params }));
72     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: shared, params }));
73     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: favorites, params }));
74     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: publicFavorites, params }));
75     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: search, params }));
76
77     return {
78         onProjectSearch: (projectSearchValue: string) => dispatch(treePickerSearchActions.SET_TREE_PICKER_PROJECT_SEARCH({ pickerId: search, projectSearchValue })),
79         onCollectionFilter: (collectionFilterValue: string) => {
80             dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: home, collectionFilterValue }));
81             dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: shared, collectionFilterValue }));
82             dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: favorites, collectionFilterValue }));
83             dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: publicFavorites, collectionFilterValue }));
84             dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: search, collectionFilterValue }));
85         },
86         dispatch
87     }
88 };
89
90 type CssRules = 'pickerHeight' | 'searchFlex' | 'scrolledBox' | 'detailsBox' | 'twoCol';
91
92 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
93     pickerHeight: {
94         height: "100%",
95     },
96     searchFlex: {
97         display: "flex",
98         justifyContent: "space-around",
99         height: "64px",
100     },
101     scrolledBox: {
102         overflow: "scroll",
103         width: "calc(100% - 320px)",
104         marginRight: "8px",
105         height: "100%",
106     },
107     twoCol: {
108         display: "flex",
109         flexDirection: "row",
110         height: "calc(100% - 64px)",
111     },
112     detailsBox: {
113         width: "320px",
114         height: "100%",
115         overflow: "scroll",
116         borderLeft: "1px solid rgba(0, 0, 0, 0.12)",
117         paddingLeft: "4px",
118     }
119 });
120
121 type ProjectsTreePickerCombinedProps = ToplevelPickerProps & ProjectsTreePickerSearchProps & ProjectsTreePickerActionProps & DispatchProp & WithStyles<CssRules>;
122
123 interface SelectionComponentState {
124     activeItem?: ProjectsTreePickerItem;
125 }
126
127 const Details = (props: { res?: ProjectsTreePickerItem }) => {
128     if (props.res && 'kind' in props.res) {
129         switch (props.res.kind) {
130             case ResourceKind.PROJECT:
131                 return <ProjectDetailsComponent project={props.res} hideEdit={true} />
132             case ResourceKind.COLLECTION:
133                 return <CollectionDetailsAttributes item={props.res} />;
134                 /* case ResourceKind.PROCESS:
135                         *     return new ProcessDetails(res);
136                  * case ResourceKind.WORKFLOW:
137                  *     return new WorkflowDetails(res);
138                  * case ResourceKind.USER:
139                  *     return new RootProjectDetails(res); */
140         }
141     } else {
142         //return new FileDetails(res);
143     }
144     return <DefaultView messages={['Select a file or folder to view its details.']} />;
145 };
146
147
148 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(
149     withStyles(styles)(
150         class FileInputComponent extends React.Component<ProjectsTreePickerCombinedProps> {
151             state: SelectionComponentState = {
152             };
153
154             componentDidMount() {
155                 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
156
157                 const preloadParams = this.props.currentUuids ? {
158                     selectedItemUuids: this.props.currentUuids,
159                     includeDirectories: !!this.props.includeDirectories,
160                     includeFiles: !!this.props.includeFiles,
161                     multi: !!this.props.showSelection,
162                 } : undefined;
163                 this.props.dispatch<any>(initProjectsTreePicker(this.props.pickerId, preloadParams));
164
165                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_PROJECT_SEARCH({ pickerId: search, projectSearchValue: "" }));
166                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: search, collectionFilterValue: "" }));
167                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: home, collectionFilterValue: "" }));
168                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: shared, collectionFilterValue: "" }));
169                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: favorites, collectionFilterValue: "" }));
170                 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: publicFavorites, collectionFilterValue: "" }));
171             }
172
173             componentWillUnmount() {
174                 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
175                 // Release all the state, we don't need it to hang around forever.
176                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: search }));
177                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: home }));
178                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: shared }));
179                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: favorites }));
180                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: publicFavorites }));
181             }
182
183             setSelection(event: React.MouseEvent<HTMLElement>,
184                          item: TreeItem<ProjectsTreePickerItem>,
185                          pickerId: string) {
186                 this.setState({activeItem: item.data});
187                 console.log(item.data);
188             }
189
190             render() {
191                 const pickerId = this.props.pickerId;
192                 const onProjectSearch = this.props.onProjectSearch;
193                 const onCollectionFilter = this.props.onCollectionFilter;
194
195                 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(pickerId);
196                 const relatedTreePickers = getRelatedTreePickers(pickerId);
197                 const _this = this;
198                 const p = {
199                     cascadeSelection: this.props.cascadeSelection,
200                     includeCollections: this.props.includeCollections,
201                     includeDirectories: this.props.includeDirectories,
202                     includeFiles: this.props.includeFiles,
203                     showSelection: this.props.showSelection,
204                     options: this.props.options,
205                     toggleItemActive: (event: React.MouseEvent<HTMLElement>,
206                                        item: TreeItem<ProjectsTreePickerItem>,
207                                        pickerId: string): void => {
208                                            _this.setSelection(event, item, pickerId);
209                                            if (_this.props.toggleItemActive) {
210                                                _this.props.toggleItemActive(event, item, pickerId);
211                                            }
212                                        },
213                     toggleItemSelection: this.props.toggleItemSelection,
214                     relatedTreePickers,
215                     disableActivation,
216                 };
217
218
219                 return <>
220                     <div className={this.props.classes.searchFlex}>
221                         <SearchInput value="" label="Project search" selfClearProp='' onSearch={onProjectSearch} debounce={500} width="18rem"  />
222                         {this.props.includeCollections &&
223                          <SearchInput value="" label="Collection search" selfClearProp='' onSearch={onCollectionFilter} debounce={500} width="18rem" />}
224                     </div>
225
226                     <div className={this.props.classes.twoCol}>
227                         <div className={this.props.classes.scrolledBox}>
228                             {this.props.projectSearch ?
229                              <div data-cy="projects-tree-search-picker">
230                                  <SearchProjectsPicker {...p} pickerId={search} />
231                              </div>
232                             :
233                              <>
234                                  <div data-cy="projects-tree-home-tree-picker">
235                                      <HomeTreePicker {...p} pickerId={home} />
236                                  </div>
237                             <div data-cy="projects-tree-shared-tree-picker">
238                                 <SharedTreePicker {...p} pickerId={shared} />
239                             </div>
240                             <div data-cy="projects-tree-public-favourites-tree-picker">
241                                 <PublicFavoritesTreePicker {...p} pickerId={publicFavorites} />
242                             </div>
243                             <div data-cy="projects-tree-favourites-tree-picker">
244                                 <FavoritesTreePicker {...p} pickerId={favorites} />
245                             </div>
246                              </>}
247                         </div>
248
249                         <div className={this.props.classes.detailsBox}>
250                             <Details res={this.state.activeItem} />
251                         </div>
252                     </div>
253                 </>;
254             }
255         }));
256
257 const getRelatedTreePickers = pipe(getProjectsTreePickerIds, values);
258 const disableActivation = [SHARED_PROJECT_ID, FAVORITES_PROJECT_ID];