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) => ({
98 justifyContent: "space-around",
103 width: "calc(100% - 320px)",
109 flexDirection: "row",
110 height: "calc(100% - 64px)",
116 borderLeft: "1px solid rgba(0, 0, 0, 0.12)",
121 type ProjectsTreePickerCombinedProps = ToplevelPickerProps & ProjectsTreePickerSearchProps & ProjectsTreePickerActionProps & DispatchProp & WithStyles<CssRules>;
123 interface SelectionComponentState {
124 activeItem?: ProjectsTreePickerItem;
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); */
142 //return new FileDetails(res);
144 return <DefaultView messages={['Select a file or folder to view its details.']} />;
148 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(
150 class FileInputComponent extends React.Component<ProjectsTreePickerCombinedProps> {
151 state: SelectionComponentState = {
154 componentDidMount() {
155 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
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,
163 this.props.dispatch<any>(initProjectsTreePicker(this.props.pickerId, preloadParams));
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: "" }));
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 }));
183 setSelection(event: React.MouseEvent<HTMLElement>,
184 item: TreeItem<ProjectsTreePickerItem>,
186 this.setState({activeItem: item.data});
187 console.log(item.data);
191 const pickerId = this.props.pickerId;
192 const onProjectSearch = this.props.onProjectSearch;
193 const onCollectionFilter = this.props.onCollectionFilter;
195 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(pickerId);
196 const relatedTreePickers = getRelatedTreePickers(pickerId);
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);
213 toggleItemSelection: this.props.toggleItemSelection,
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" />}
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} />
234 <div data-cy="projects-tree-home-tree-picker">
235 <HomeTreePicker {...p} pickerId={home} />
237 <div data-cy="projects-tree-shared-tree-picker">
238 <SharedTreePicker {...p} pickerId={shared} />
240 <div data-cy="projects-tree-public-favourites-tree-picker">
241 <PublicFavoritesTreePicker {...p} pickerId={publicFavorites} />
243 <div data-cy="projects-tree-favourites-tree-picker">
244 <FavoritesTreePicker {...p} pickerId={favorites} />
249 <div className={this.props.classes.detailsBox}>
250 <Details res={this.state.activeItem} />
257 const getRelatedTreePickers = pipe(getProjectsTreePickerIds, values);
258 const disableActivation = [SHARED_PROJECT_ID, FAVORITES_PROJECT_ID];