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, 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';
35 export interface ToplevelPickerProps {
36 currentUuids?: 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;
48 interface ProjectsTreePickerSearchProps {
49 projectSearch: string;
50 collectionFilter: string;
53 interface ProjectsTreePickerActionProps {
54 onProjectSearch: (value: string) => void;
55 onCollectionFilter: (value: string) => void;
58 const mapStateToProps = (state: RootState, props: ToplevelPickerProps): ProjectsTreePickerSearchProps => {
59 const { search } = getProjectsTreePickerIds(props.pickerId);
62 projectSearch: state.treePickerSearch.projectSearchValues[search] || state.treePickerSearch.collectionFilterValues[search],
63 collectionFilter: state.treePickerSearch.collectionFilterValues[search],
67 const mapDispatchToProps = (dispatch: Dispatch, props: ToplevelPickerProps): (ProjectsTreePickerActionProps & DispatchProp) => {
68 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(props.pickerId);
70 includeCollections: props.includeCollections,
71 includeDirectories: props.includeDirectories,
72 includeFiles: props.includeFiles,
73 options: props.options
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 }));
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 }));
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 }) => {
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);
142 } else if ('type' in props.res) {
143 if (props.res.type === CollectionFileType.FILE) {
145 <DetailsAttribute label='Type' value="File" />
146 <DetailsAttribute label='Size' value={formatFileSize(props.res.size)} />
149 return <DetailsAttribute label='Type' value="Directory" />
153 return <DefaultView messages={['Select a file or folder to view its details.']} />;
157 export const ProjectsTreePicker = connect(mapStateToProps, mapDispatchToProps)(
159 class FileInputComponent extends React.Component<ProjectsTreePickerCombinedProps> {
160 state: SelectionComponentState = {
163 componentDidMount() {
164 const { search } = getProjectsTreePickerIds(this.props.pickerId);
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,
172 this.props.dispatch<any>(initProjectsTreePicker(this.props.pickerId, preloadParams));
174 this.props.dispatch(treePickerSearchSagas.SET_PROJECT_SEARCH({ pickerId: search, projectSearchValue: "" }));
175 this.props.dispatch(treePickerSearchSagas.SET_COLLECTION_FILTER({ pickerMainId: this.props.pickerId, collectionFilterValue: "" }));
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 }));
188 setSelection(event: React.MouseEvent<HTMLElement>,
189 item: TreeItem<ProjectsTreePickerItem>,
191 this.setState({activeItem: item.data});
195 const pickerId = this.props.pickerId;
196 const onProjectSearch = this.props.onProjectSearch;
197 const onCollectionFilter = this.props.onCollectionFilter;
199 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(pickerId);
200 const relatedTreePickers = getRelatedTreePickers(pickerId);
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);
217 toggleItemSelection: this.props.toggleItemSelection,
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>}
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} />
238 <div data-cy="projects-tree-home-tree-picker">
239 <HomeTreePicker {...p} pickerId={home} />
241 <div data-cy="projects-tree-shared-tree-picker">
242 <SharedTreePicker {...p} pickerId={shared} />
244 <div data-cy="projects-tree-public-favourites-tree-picker">
245 <PublicFavoritesTreePicker {...p} pickerId={publicFavorites} />
247 <div data-cy="projects-tree-favourites-tree-picker">
248 <FavoritesTreePicker {...p} pickerId={favorites} />
253 <div className={this.props.classes.detailsBox} data-cy="picker-dialog-details">
254 <Details res={this.state.activeItem} />
261 const getRelatedTreePickers = pipe(getProjectsTreePickerIds, values);
262 const disableActivation = [SHARED_PROJECT_ID, FAVORITES_PROJECT_ID];