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, WithStyles } from '@material-ui/core';
24 import { ArvadosTheme } from 'common/custom-theme';
26 export interface ToplevelPickerProps {
27 currentUuids?: string[];
29 cascadeSelection: boolean;
30 includeCollections?: boolean;
31 includeDirectories?: boolean;
32 includeFiles?: boolean;
33 showSelection?: boolean;
34 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
35 toggleItemActive?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<ProjectsTreePickerItem>, pickerId: string) => void;
36 toggleItemSelection?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<ProjectsTreePickerItem>, pickerId: string) => void;
39 interface ProjectsTreePickerSearchProps {
40 projectSearch: string;
41 collectionFilter: string;
44 interface ProjectsTreePickerActionProps {
45 onProjectSearch: (value: string) => void;
46 onCollectionFilter: (value: string) => void;
49 const mapStateToProps = (state: RootState, props: ToplevelPickerProps): ProjectsTreePickerSearchProps => {
50 const { search } = getProjectsTreePickerIds(props.pickerId);
53 projectSearch: state.treePickerSearch.projectSearchValues[search],
54 collectionFilter: state.treePickerSearch.collectionFilterValues[search],
58 const mapDispatchToProps = (dispatch: Dispatch, props: ToplevelPickerProps): (ProjectsTreePickerActionProps & DispatchProp) => {
59 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(props.pickerId);
61 includeCollections: props.includeCollections,
62 includeDirectories: props.includeDirectories,
63 includeFiles: props.includeFiles,
64 options: props.options
66 dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: home, params }));
67 dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: shared, params }));
68 dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: favorites, params }));
69 dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: publicFavorites, params }));
70 dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: search, params }));
73 onProjectSearch: (projectSearchValue: string) => dispatch(treePickerSearchActions.SET_TREE_PICKER_PROJECT_SEARCH({ pickerId: search, projectSearchValue })),
74 onCollectionFilter: (collectionFilterValue: string) => {
75 dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: home, collectionFilterValue }));
76 dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: shared, collectionFilterValue }));
77 dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: favorites, collectionFilterValue }));
78 dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: publicFavorites, collectionFilterValue }));
79 dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: search, collectionFilterValue }));
85 type CssRules = 'pickerHeight' | 'searchFlex' | 'scrolledBox';
87 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
90 flexDirection: "column",
95 justifyContent: "space-around",
103 type ProjectsTreePickerCombinedProps = ToplevelPickerProps & ProjectsTreePickerSearchProps & ProjectsTreePickerActionProps & DispatchProp & WithStyles<CssRules>;
105 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(
107 class FileInputComponent extends React.Component<ProjectsTreePickerCombinedProps> {
109 componentDidMount() {
110 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
112 const preloadParams = this.props.currentUuids ? {
113 selectedItemUuids: this.props.currentUuids,
114 includeDirectories: !!this.props.includeDirectories,
115 includeFiles: !!this.props.includeFiles,
116 multi: !!this.props.showSelection,
118 this.props.dispatch<any>(initProjectsTreePicker(this.props.pickerId, preloadParams));
120 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_PROJECT_SEARCH({ pickerId: search, projectSearchValue: "" }));
121 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: search, collectionFilterValue: "" }));
122 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: home, collectionFilterValue: "" }));
123 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: shared, collectionFilterValue: "" }));
124 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: favorites, collectionFilterValue: "" }));
125 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: publicFavorites, collectionFilterValue: "" }));
128 componentWillUnmount() {
129 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
130 // Release all the state, we don't need it to hang around forever.
131 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: search }));
132 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: home }));
133 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: shared }));
134 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: favorites }));
135 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: publicFavorites }));
139 const pickerId = this.props.pickerId;
140 const onProjectSearch = this.props.onProjectSearch;
141 const onCollectionFilter = this.props.onCollectionFilter;
143 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(pickerId);
144 const relatedTreePickers = getRelatedTreePickers(pickerId);
146 cascadeSelection: this.props.cascadeSelection,
147 includeCollections: this.props.includeCollections,
148 includeDirectories: this.props.includeDirectories,
149 includeFiles: this.props.includeFiles,
150 showSelection: this.props.showSelection,
151 options: this.props.options,
152 toggleItemActive: this.props.toggleItemActive,
153 toggleItemSelection: this.props.toggleItemSelection,
157 return <div className={this.props.classes.pickerHeight} >
158 <span className={this.props.classes.searchFlex}>
159 <SearchInput value="" label="Search for a Project" selfClearProp='' onSearch={onProjectSearch} debounce={500} />
160 {this.props.includeCollections &&
161 <SearchInput value="" label="Filter Collections list in Projects" selfClearProp='' onSearch={onCollectionFilter} debounce={500} />}
164 <div className={this.props.classes.scrolledBox}>
165 {this.props.projectSearch ?
166 <div data-cy="projects-tree-search-picker">
167 <SearchProjectsPicker {...p} pickerId={search} />
171 <div data-cy="projects-tree-home-tree-picker">
172 <HomeTreePicker {...p} pickerId={home} />
174 <div data-cy="projects-tree-shared-tree-picker">
175 <SharedTreePicker {...p} pickerId={shared} />
177 <div data-cy="projects-tree-public-favourites-tree-picker">
178 <PublicFavoritesTreePicker {...p} pickerId={publicFavorites} />
180 <div data-cy="projects-tree-favourites-tree-picker">
181 <FavoritesTreePicker {...p} pickerId={favorites} />
189 const getRelatedTreePickers = pipe(getProjectsTreePickerIds, values);
190 const disableActivation = [SHARED_PROJECT_ID, FAVORITES_PROJECT_ID];