21838: Remove stray console logs & unused imports
[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, treePickerSearchSagas
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 { CollectionFileType } from 'models/collection-file';
28 import { DefaultView } from 'components/default-view/default-view';
29 import { ProjectDetailsComponent } from 'views-components/details-panel/project-details';
30 import { CollectionDetailsAttributes } from 'views/collection-panel/collection-panel';
31 import { RootProjectDetailsComponent } from 'views-components/details-panel/root-project-details';
32 import { DetailsAttribute } from 'components/details-attribute/details-attribute';
33 import { formatFileSize } from 'common/formatters';
34
35 export interface ToplevelPickerProps {
36     currentUuids?: string[];
37     pickerId: string;
38     cascadeSelection: boolean;
39     includeCollections?: boolean;
40     includeDirectories?: boolean;
41     includeFiles?: boolean;
42     showSelection?: boolean;
43     options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
44     toggleItemActive?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<ProjectsTreePickerItem>, pickerId: string) => void;
45     toggleItemSelection?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<ProjectsTreePickerItem>, pickerId: string) => void;
46 }
47
48 interface ProjectsTreePickerSearchProps {
49     projectSearch: string;
50     collectionFilter: string;
51 }
52
53 interface ProjectsTreePickerActionProps {
54     onProjectSearch: (value: string) => void;
55     onCollectionFilter: (value: string) => void;
56 }
57
58 const mapStateToProps = (state: RootState, props: ToplevelPickerProps): ProjectsTreePickerSearchProps => {
59     const { search } = getProjectsTreePickerIds(props.pickerId);
60     return {
61         ...props,
62         projectSearch: state.treePickerSearch.projectSearchValues[search] || state.treePickerSearch.collectionFilterValues[search],
63         collectionFilter: state.treePickerSearch.collectionFilterValues[search],
64     };
65 };
66
67 const mapDispatchToProps = (dispatch: Dispatch, props: ToplevelPickerProps): (ProjectsTreePickerActionProps & DispatchProp) => {
68     const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(props.pickerId);
69     const params = {
70         includeCollections: props.includeCollections,
71         includeDirectories: props.includeDirectories,
72         includeFiles: props.includeFiles,
73         options: props.options
74     };
75     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: home, params }));
76     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: shared, params }));
77     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: favorites, params }));
78     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: publicFavorites, params }));
79     dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: search, params }));
80
81     return {
82         onProjectSearch: (projectSearchValue: string) => dispatch(treePickerSearchSagas.SET_PROJECT_SEARCH({ pickerId: search, projectSearchValue })),
83         onCollectionFilter: (collectionFilterValue: string) => {
84             dispatch(treePickerSearchSagas.SET_COLLECTION_FILTER({ pickerMainId: props.pickerId, 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) {
129         if ('kind' in props.res) {
130             switch (props.res.kind) {
131                 case ResourceKind.PROJECT:
132                     return <ProjectDetailsComponent project={props.res} hideEdit={true} />
133                 case ResourceKind.COLLECTION:
134                     return <CollectionDetailsAttributes item={props.res} />;
135                 case ResourceKind.USER:
136                     return <RootProjectDetailsComponent rootProject={props.res} />;
137                     // case ResourceKind.PROCESS:
138                     //                         return new ProcessDetails(res);
139                     // case ResourceKind.WORKFLOW:
140                     //     return new WorkflowDetails(res);
141             }
142         } else if ('type' in props.res) {
143             if (props.res.type === CollectionFileType.FILE) {
144                 return <>
145                 <DetailsAttribute label='Type' value="File" />
146                 <DetailsAttribute label='Size' value={formatFileSize(props.res.size)} />
147                 </>;
148             } else {
149                 return <DetailsAttribute label='Type' value="Directory" />
150             }
151         }
152     }
153     return <DefaultView messages={['Select a file or folder to view its details.']} />;
154 };
155
156
157 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(
158     withStyles(styles)(
159         class FileInputComponent extends React.Component<ProjectsTreePickerCombinedProps> {
160             state: SelectionComponentState = {
161             };
162
163             componentDidMount() {
164                 const { search } = getProjectsTreePickerIds(this.props.pickerId);
165
166                 const preloadParams = this.props.currentUuids ? {
167                     selectedItemUuids: this.props.currentUuids,
168                     includeDirectories: !!this.props.includeDirectories,
169                     includeFiles: !!this.props.includeFiles,
170                     multi: !!this.props.showSelection,
171                 } : undefined;
172                 this.props.dispatch<any>(initProjectsTreePicker(this.props.pickerId, preloadParams));
173
174                 this.props.dispatch(treePickerSearchSagas.SET_PROJECT_SEARCH({ pickerId: search, projectSearchValue: "" }));
175                 this.props.dispatch(treePickerSearchSagas.SET_COLLECTION_FILTER({ pickerMainId: this.props.pickerId, collectionFilterValue: "" }));
176             }
177
178             componentWillUnmount() {
179                 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
180                 // Release all the state, we don't need it to hang around forever.
181                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: search }));
182                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: home }));
183                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: shared }));
184                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: favorites }));
185                 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: publicFavorites }));
186             }
187
188             setSelection(event: React.MouseEvent<HTMLElement>,
189                          item: TreeItem<ProjectsTreePickerItem>,
190                          pickerId: string) {
191                 this.setState({activeItem: item.data});
192             }
193
194             render() {
195                 const pickerId = this.props.pickerId;
196                 const onProjectSearch = this.props.onProjectSearch;
197                 const onCollectionFilter = this.props.onCollectionFilter;
198
199                 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(pickerId);
200                 const relatedTreePickers = getRelatedTreePickers(pickerId);
201                 const _this = this;
202                 const p = {
203                     cascadeSelection: this.props.cascadeSelection,
204                     includeCollections: this.props.includeCollections,
205                     includeDirectories: this.props.includeDirectories,
206                     includeFiles: this.props.includeFiles,
207                     showSelection: this.props.showSelection,
208                     options: this.props.options,
209                     toggleItemActive: (event: React.MouseEvent<HTMLElement>,
210                                        item: TreeItem<ProjectsTreePickerItem>,
211                                        pickerId: string): void => {
212                                            _this.setSelection(event, item, pickerId);
213                                            if (_this.props.toggleItemActive) {
214                                                _this.props.toggleItemActive(event, item, pickerId);
215                                            }
216                                        },
217                     toggleItemSelection: this.props.toggleItemSelection,
218                     relatedTreePickers,
219                     disableActivation,
220                 };
221
222
223                 return <>
224                     <div className={this.props.classes.searchFlex}>
225                         <span data-cy="picker-dialog-project-search"><SearchInput value="" label="Project search" selfClearProp='' onSearch={onProjectSearch} debounce={500} width="18rem"  /></span>
226                 {this.props.includeCollections &&
227                  <span data-cy="picker-dialog-collection-search" ><SearchInput value="" label="Collection search" selfClearProp='' onSearch={onCollectionFilter} debounce={500} width="18rem" /></span>}
228                 </div>
229
230                 <div className={this.props.classes.twoCol}>
231                     <div className={this.props.classes.scrolledBox}>
232                         {this.props.projectSearch ?
233                          <div data-cy="projects-tree-search-picker">
234                              <SearchProjectsPicker {...p} pickerId={search} />
235                          </div>
236                         :
237                          <>
238                              <div data-cy="projects-tree-home-tree-picker">
239                                  <HomeTreePicker {...p} pickerId={home} />
240                              </div>
241                         <div data-cy="projects-tree-shared-tree-picker">
242                             <SharedTreePicker {...p} pickerId={shared} />
243                         </div>
244                         <div data-cy="projects-tree-public-favourites-tree-picker">
245                             <PublicFavoritesTreePicker {...p} pickerId={publicFavorites} />
246                         </div>
247                         <div data-cy="projects-tree-favourites-tree-picker">
248                             <FavoritesTreePicker {...p} pickerId={favorites} />
249                         </div>
250                          </>}
251                     </div>
252
253                     <div className={this.props.classes.detailsBox} data-cy="picker-dialog-details">
254                         <Details res={this.state.activeItem} />
255                     </div>
256                 </div>
257                 </>;
258             }
259 }));
260
261 const getRelatedTreePickers = pipe(getProjectsTreePickerIds, values);
262 const disableActivation = [SHARED_PROJECT_ID, FAVORITES_PROJECT_ID];