refs #14080 Merge branch 'origin/14080-cwl-graphs'
[arvados-workbench2.git] / src / views-components / search-bar / search-bar-view.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import {
7     IconButton,
8     Paper,
9     StyleRulesCallback,
10     withStyles,
11     WithStyles,
12     Tooltip,
13     InputAdornment, Input,
14     ListItem, ListItemText, ListItemSecondaryAction,
15     ClickAwayListener
16 } from '@material-ui/core';
17 import SearchIcon from '@material-ui/icons/Search';
18 import { RemoveIcon, EditSavedQueryIcon } from '~/components/icon/icon';
19 import { SearchView } from '~/store/search-bar/search-bar-reducer';
20 import { SearchBarBasicView } from '~/views-components/search-bar/search-bar-basic-view';
21 import { SearchBarAdvancedView } from '~/views-components/search-bar/search-bar-advanced-view';
22 import { SearchBarAutocompleteView, SearchBarAutocompleteViewDataProps } from '~/views-components/search-bar/search-bar-autocomplete-view';
23 import { ArvadosTheme } from '~/common/custom-theme';
24 import { SearchBarAdvanceFormData } from '~/models/search-bar';
25
26 type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'view';
27
28 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
29     return {
30         container: {
31             position: 'relative',
32             width: '100%',
33             borderRadius: theme.spacing.unit / 2
34         },
35         containerSearchViewOpened: {
36             position: 'relative',
37             width: '100%',
38             borderRadius: `${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px 0 0`
39         },
40         input: {
41             border: 'none',
42             padding: `0px ${theme.spacing.unit}px`
43         },
44         view: {
45             position: 'absolute',
46             width: '100%',
47             zIndex: 1
48         }
49     };
50 };
51
52 type SearchBarDataProps = {
53     searchValue: string;
54     currentView: string;
55     isPopoverOpen: boolean;
56     savedQueries: SearchBarAdvanceFormData[];
57 } & SearchBarAutocompleteViewDataProps;
58
59 interface SearchBarActionProps {
60     onSearch: (value: string) => any;
61     debounce?: number;
62     onSetView: (currentView: string) => void;
63     closeView: () => void;
64     saveRecentQuery: (query: string) => void;
65     loadRecentQueries: () => string[];
66     saveQuery: (data: SearchBarAdvanceFormData) => void;
67     deleteSavedQuery: (id: number) => void;
68     openSearchView: () => void;
69     navigateTo: (uuid: string) => void;
70     editSavedQuery: (data: SearchBarAdvanceFormData, id: number) => void;
71 }
72
73 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
74
75 interface SearchBarState {
76     value: string;
77 }
78
79 interface RenderRecentQueriesProps {
80     text: string;
81     onSearch: (searchValue: string) => void;
82 }
83
84 export const RenderRecentQueries = (props: RenderRecentQueriesProps) => {
85     return <ListItem button>
86         <ListItemText secondary={props.text} onClick={() => props.onSearch(props.text)} />
87     </ListItem>;
88 };
89
90 interface RenderAutocompleteItemsProps {
91     text: string | JSX.Element;
92     navigateTo: (uuid: string) => void;
93     uuid: string;
94 }
95
96 export const RenderAutocompleteItems = (props: RenderAutocompleteItemsProps) => {
97     return <ListItem button>
98         <ListItemText secondary={props.text} onClick={() => props.navigateTo(props.uuid)} />
99     </ListItem>;
100 };
101
102 interface RenderSavedQueriesProps {
103     text: string;
104     id: number;
105     deleteSavedQuery: (id: number) => void;
106     onSearch: (searchValue: string) => void;
107     editSavedQuery: (data: SearchBarAdvanceFormData, id: number) => void;
108     data: SearchBarAdvanceFormData;
109 }
110
111 export const RenderSavedQueries = (props: RenderSavedQueriesProps) => {
112     return <ListItem button>
113         <ListItemText secondary={props.text} onClick={() => props.onSearch(props.text)} />
114         <ListItemSecondaryAction>
115             <Tooltip title="Edit">
116                 <IconButton aria-label="Edit" onClick={() => props.editSavedQuery(props.data, props.id)}>
117                     <EditSavedQueryIcon />
118                 </IconButton>
119             </Tooltip>
120             <Tooltip title="Remove">
121                 <IconButton aria-label="Remove" onClick={() => props.deleteSavedQuery(props.id)}>
122                     <RemoveIcon />
123                 </IconButton>
124             </Tooltip>
125         </ListItemSecondaryAction>
126     </ListItem>;
127 };
128
129 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
130
131 export const SearchBarView = withStyles(styles)(
132     class extends React.Component<SearchBarProps> {
133         state: SearchBarState = {
134             value: ""
135         };
136
137         timeout: number;
138
139         render() {
140             const { classes, currentView, openSearchView, closeView, isPopoverOpen } = this.props;
141             return <ClickAwayListener onClickAway={closeView}>
142                 <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
143                     <form onSubmit={this.handleSubmit}>
144                         <Input
145                             className={classes.input}
146                             onChange={this.handleChange}
147                             placeholder="Search"
148                             value={this.state.value}
149                             fullWidth={true}
150                             disableUnderline={true}
151                             onClick={openSearchView}
152                             endAdornment={
153                                 <InputAdornment position="end">
154                                     <Tooltip title='Search'>
155                                         <IconButton>
156                                             <SearchIcon />
157                                         </IconButton>
158                                     </Tooltip>
159                                 </InputAdornment>
160                             } />
161                     </form>
162                     <div className={classes.view}>
163                         {isPopoverOpen && this.getView(currentView)}
164                     </div>
165                 </Paper >
166             </ClickAwayListener>;
167         }
168
169         componentDidMount() {
170             this.setState({ value: this.props.searchValue });
171         }
172
173         componentWillReceiveProps(nextProps: SearchBarProps) {
174             if (nextProps.searchValue !== this.props.searchValue) {
175                 this.setState({ value: nextProps.searchValue });
176             }
177         }
178
179         componentWillUnmount() {
180             clearTimeout(this.timeout);
181         }
182
183         getView = (currentView: string) => {
184             const { onSetView, loadRecentQueries, savedQueries, deleteSavedQuery, searchValue, searchResults, saveQuery, onSearch, navigateTo, editSavedQuery } = this.props;
185             switch (currentView) {
186                 case SearchView.BASIC:
187                     return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} onSearch={onSearch} editSavedQuery={editSavedQuery} />;
188                 case SearchView.ADVANCED:
189                     return <SearchBarAdvancedView setView={onSetView} saveQuery={saveQuery} />;
190                 case SearchView.AUTOCOMPLETE:
191                     return <SearchBarAutocompleteView
192                         navigateTo={navigateTo}
193                         searchResults={searchResults}
194                         searchValue={searchValue} />;
195                 default:
196                     return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} onSearch={onSearch} editSavedQuery={editSavedQuery} />;
197             }
198         }
199
200         handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
201             event.preventDefault();
202             clearTimeout(this.timeout);
203             this.props.saveRecentQuery(this.state.value);
204             this.props.onSearch(this.state.value);
205             this.props.loadRecentQueries();
206         }
207
208         handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
209             clearTimeout(this.timeout);
210             this.setState({ value: event.target.value });
211             this.timeout = window.setTimeout(
212                 () => this.props.onSearch(this.state.value),
213                 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
214             );
215             if (event.target.value.length > 0) {
216                 this.props.onSetView(SearchView.AUTOCOMPLETE);
217             } else {
218                 this.props.onSetView(SearchView.BASIC);
219             }
220         }
221     }
222 );