1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
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';
31 export interface ToplevelPickerProps {
32 currentUuids?: 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;
44 interface ProjectsTreePickerSearchProps {
45 projectSearch: string;
46 collectionFilter: string;
49 interface ProjectsTreePickerActionProps {
50 onProjectSearch: (value: string) => void;
51 onCollectionFilter: (value: string) => void;
54 const mapStateToProps = (state: RootState, props: ToplevelPickerProps): ProjectsTreePickerSearchProps => {
55 const { search } = getProjectsTreePickerIds(props.pickerId);
58 projectSearch: state.treePickerSearch.projectSearchValues[search] || state.treePickerSearch.collectionFilterValues[search],
59 collectionFilter: state.treePickerSearch.collectionFilterValues[search],
63 const mapDispatchToProps = (dispatch: Dispatch, props: ToplevelPickerProps): (ProjectsTreePickerActionProps & DispatchProp) => {
64 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(props.pickerId);
66 includeCollections: props.includeCollections,
67 includeDirectories: props.includeDirectories,
68 includeFiles: props.includeFiles,
69 options: props.options
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 }));
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 }));
90 type CssRules = 'pickerHeight' | 'searchFlex' | 'scrolledBox' | 'detailsBox' | 'twoCol';
92 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
99 justifyContent: "space-around",
100 paddingBottom: "2em",
105 width: "calc(100% - 300px)",
106 height: "calc(100% - 45px)",
110 flexDirection: "row",
118 type ProjectsTreePickerCombinedProps = ToplevelPickerProps & ProjectsTreePickerSearchProps & ProjectsTreePickerActionProps & DispatchProp & WithStyles<CssRules>;
120 interface SelectionComponentState {
121 activeItem?: ProjectsTreePickerItem;
124 const Details = (props: { res?: ProjectsTreePickerItem }) => {
125 if (props.res && 'kind' in props.res) {
126 switch (props.res.kind) {
127 case ResourceKind.PROJECT:
128 return <ProjectDetailsComponent project={props.res} hideEdit={true} />
129 case ResourceKind.COLLECTION:
130 return <CollectionDetailsAttributes item={props.res} />;
131 /* case ResourceKind.PROCESS:
132 * return new ProcessDetails(res);
133 * case ResourceKind.WORKFLOW:
134 * return new WorkflowDetails(res);
135 * case ResourceKind.USER:
136 * return new RootProjectDetails(res); */
139 //return new FileDetails(res);
141 return <DefaultView messages={['Select a file or folder to view its details.']} />;
145 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(
147 class FileInputComponent extends React.Component<ProjectsTreePickerCombinedProps> {
148 state: SelectionComponentState = {
151 componentDidMount() {
152 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
154 const preloadParams = this.props.currentUuids ? {
155 selectedItemUuids: this.props.currentUuids,
156 includeDirectories: !!this.props.includeDirectories,
157 includeFiles: !!this.props.includeFiles,
158 multi: !!this.props.showSelection,
160 this.props.dispatch<any>(initProjectsTreePicker(this.props.pickerId, preloadParams));
162 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_PROJECT_SEARCH({ pickerId: search, projectSearchValue: "" }));
163 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: search, collectionFilterValue: "" }));
164 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: home, collectionFilterValue: "" }));
165 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: shared, collectionFilterValue: "" }));
166 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: favorites, collectionFilterValue: "" }));
167 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: publicFavorites, collectionFilterValue: "" }));
170 componentWillUnmount() {
171 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
172 // Release all the state, we don't need it to hang around forever.
173 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: search }));
174 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: home }));
175 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: shared }));
176 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: favorites }));
177 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: publicFavorites }));
180 setSelection(event: React.MouseEvent<HTMLElement>,
181 item: TreeItem<ProjectsTreePickerItem>,
183 this.setState({activeItem: item.data});
184 console.log(item.data);
188 const pickerId = this.props.pickerId;
189 const onProjectSearch = this.props.onProjectSearch;
190 const onCollectionFilter = this.props.onCollectionFilter;
192 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(pickerId);
193 const relatedTreePickers = getRelatedTreePickers(pickerId);
196 cascadeSelection: this.props.cascadeSelection,
197 includeCollections: this.props.includeCollections,
198 includeDirectories: this.props.includeDirectories,
199 includeFiles: this.props.includeFiles,
200 showSelection: this.props.showSelection,
201 options: this.props.options,
202 toggleItemActive: (event: React.MouseEvent<HTMLElement>,
203 item: TreeItem<ProjectsTreePickerItem>,
204 pickerId: string): void => {
205 _this.setSelection(event, item, pickerId);
206 if (_this.props.toggleItemActive) {
207 _this.props.toggleItemActive(event, item, pickerId);
210 toggleItemSelection: this.props.toggleItemSelection,
216 return <div className={this.props.classes.pickerHeight} >
217 <div className={this.props.classes.searchFlex}>
218 <SearchInput value="" label="Project search" selfClearProp='' onSearch={onProjectSearch} debounce={500} width="18rem" />
219 {this.props.includeCollections &&
220 <SearchInput value="" label="Collection search" selfClearProp='' onSearch={onCollectionFilter} debounce={500} width="18rem" />}
223 <div className={this.props.classes.twoCol}>
224 <div className={this.props.classes.scrolledBox}>
225 {this.props.projectSearch ?
226 <div data-cy="projects-tree-search-picker">
227 <SearchProjectsPicker {...p} pickerId={search} />
231 <div data-cy="projects-tree-home-tree-picker">
232 <HomeTreePicker {...p} pickerId={home} />
234 <div data-cy="projects-tree-shared-tree-picker">
235 <SharedTreePicker {...p} pickerId={shared} />
237 <div data-cy="projects-tree-public-favourites-tree-picker">
238 <PublicFavoritesTreePicker {...p} pickerId={publicFavorites} />
240 <div data-cy="projects-tree-favourites-tree-picker">
241 <FavoritesTreePicker {...p} pickerId={favorites} />
246 <div className={this.props.classes.detailsBox}>
247 <Details res={this.state.activeItem} />
254 const getRelatedTreePickers = pipe(getProjectsTreePickerIds, values);
255 const disableActivation = [SHARED_PROJECT_ID, FAVORITES_PROJECT_ID];