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 { withStyles, StyleRulesCallback, WithStyles } from '@material-ui/core';
23 import { ArvadosTheme } from 'common/custom-theme';
25 export interface ToplevelPickerProps {
27 includeCollections?: boolean;
28 includeFiles?: boolean;
29 showSelection?: boolean;
30 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
31 toggleItemActive?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<ProjectsTreePickerItem>, pickerId: string) => void;
32 toggleItemSelection?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<ProjectsTreePickerItem>, pickerId: string) => void;
35 interface ProjectsTreePickerSearchProps {
36 projectSearch: string;
37 collectionFilter: string;
40 interface ProjectsTreePickerActionProps {
41 onProjectSearch: (value: string) => void;
42 onCollectionFilter: (value: string) => void;
45 const mapStateToProps = (state: RootState, props: ToplevelPickerProps): ProjectsTreePickerSearchProps => {
46 const { search } = getProjectsTreePickerIds(props.pickerId);
49 projectSearch: state.treePickerSearch.projectSearchValues[search],
50 collectionFilter: state.treePickerSearch.collectionFilterValues[search],
54 const mapDispatchToProps = (dispatch: Dispatch, props: ToplevelPickerProps): (ProjectsTreePickerActionProps & DispatchProp) => {
55 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(props.pickerId);
57 includeCollections: props.includeCollections,
58 includeFiles: props.includeFiles,
59 options: props.options
61 dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: home, params }));
62 dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: shared, params }));
63 dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: favorites, params }));
64 dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: publicFavorites, params }));
65 dispatch(treePickerSearchActions.SET_TREE_PICKER_LOAD_PARAMS({ pickerId: search, params }));
68 onProjectSearch: (projectSearchValue: string) => dispatch(treePickerSearchActions.SET_TREE_PICKER_PROJECT_SEARCH({ pickerId: search, projectSearchValue })),
69 onCollectionFilter: (collectionFilterValue: string) => {
70 dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: home, collectionFilterValue }));
71 dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: shared, collectionFilterValue }));
72 dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: favorites, collectionFilterValue }));
73 dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: publicFavorites, collectionFilterValue }));
74 dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: search, collectionFilterValue }));
80 type CssRules = 'pickerHeight' | 'searchFlex' | 'scrolledBox';
82 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
86 flexDirection: "column",
90 justifyContent: "space-around",
98 type ProjectsTreePickerCombinedProps = ToplevelPickerProps & ProjectsTreePickerSearchProps & ProjectsTreePickerActionProps & DispatchProp & WithStyles<CssRules>;
100 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(
102 class FileInputComponent extends React.Component<ProjectsTreePickerCombinedProps> {
104 componentDidMount() {
105 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
107 this.props.dispatch<any>(initProjectsTreePicker(this.props.pickerId));
109 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_PROJECT_SEARCH({ pickerId: search, projectSearchValue: "" }));
110 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: search, collectionFilterValue: "" }));
111 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: home, collectionFilterValue: "" }));
112 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: shared, collectionFilterValue: "" }));
113 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: favorites, collectionFilterValue: "" }));
114 this.props.dispatch(treePickerSearchActions.SET_TREE_PICKER_COLLECTION_FILTER({ pickerId: publicFavorites, collectionFilterValue: "" }));
117 componentWillUnmount() {
118 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(this.props.pickerId);
119 // Release all the state, we don't need it to hang around forever.
120 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: search }));
121 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: home }));
122 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: shared }));
123 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: favorites }));
124 this.props.dispatch(treePickerActions.RESET_TREE_PICKER({ pickerId: publicFavorites }));
128 const pickerId = this.props.pickerId;
129 const onProjectSearch = this.props.onProjectSearch;
130 const onCollectionFilter = this.props.onCollectionFilter;
132 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(pickerId);
133 const relatedTreePickers = getRelatedTreePickers(pickerId);
135 includeCollections: this.props.includeCollections,
136 includeFiles: this.props.includeFiles,
137 showSelection: this.props.showSelection,
138 options: this.props.options,
139 toggleItemActive: this.props.toggleItemActive,
140 toggleItemSelection: this.props.toggleItemSelection,
144 return <div className={this.props.classes.pickerHeight} >
145 <span className={this.props.classes.searchFlex}>
146 <SearchInput value="" label="Search for a Project" selfClearProp='' onSearch={onProjectSearch} debounce={500} />
147 {this.props.includeCollections &&
148 <SearchInput value="" label="Filter Collections list in Projects" selfClearProp='' onSearch={onCollectionFilter} debounce={500} />}
151 <div className={this.props.classes.scrolledBox}>
152 {this.props.projectSearch ?
153 <div data-cy="projects-tree-search-picker">
154 <SearchProjectsPicker {...p} pickerId={search} />
158 <div data-cy="projects-tree-home-tree-picker">
159 <HomeTreePicker {...p} pickerId={home} />
161 <div data-cy="projects-tree-shared-tree-picker">
162 <SharedTreePicker {...p} pickerId={shared} />
164 <div data-cy="projects-tree-public-favourites-tree-picker">
165 <PublicFavoritesTreePicker {...p} pickerId={publicFavorites} />
167 <div data-cy="projects-tree-favourites-tree-picker">
168 <FavoritesTreePicker {...p} pickerId={favorites} />
176 const getRelatedTreePickers = pipe(getProjectsTreePickerIds, values);
177 const disableActivation = [SHARED_PROJECT_ID, FAVORITES_PROJECT_ID];