Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 13827-structured...
[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 } 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
25 type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'searchBar' | 'view';
26
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
28     return {
29         container: {
30             position: 'relative',
31             width: '100%',
32             borderRadius: theme.spacing.unit / 4
33         },
34         containerSearchViewOpened: {
35             position: 'relative',
36             width: '100%',
37             borderRadius: `${theme.spacing.unit / 4}px ${theme.spacing.unit / 4}px 0 0`
38         },
39         input: {
40             border: 'none',
41             padding: `0px ${theme.spacing.unit}px`
42         },
43         searchBar: {
44             height: '30px'
45         },
46         view: {
47             position: 'absolute',
48             width: '100%'
49         }
50     };
51 };
52
53 type SearchBarDataProps = {
54     searchValue: string;
55     currentView: string;
56     open: boolean;
57 } & SearchBarAutocompleteViewDataProps;
58
59 interface SearchBarActionProps {
60     onSearch: (value: string) => any;
61     debounce?: number;
62     onSetView: (currentView: string) => void;
63     openView: () => void;
64     closeView: () => void;
65     saveQuery: (query: string) => void;
66     loadQueries: () => string[];
67 }
68
69 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
70
71 interface SearchBarState {
72     value: string;
73 }
74
75 interface RenderQueriesProps {
76     text: string | JSX.Element;
77 }
78
79 export const RecentQueriesItem = (props: RenderQueriesProps) => {
80     return <ListItem button>
81         <ListItemText secondary={props.text} />
82     </ListItem>;
83 };
84
85
86 export const RenderSavedQueries = (props: RenderQueriesProps) => {
87     return <ListItem button>
88         <ListItemText secondary={props.text} />
89         <ListItemSecondaryAction>
90             <Tooltip title="Remove">
91                 <IconButton aria-label="Remove">
92                     <RemoveIcon />
93                 </IconButton>
94             </Tooltip>
95         </ListItemSecondaryAction>
96     </ListItem>;
97 };
98
99 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
100
101 export const SearchBarView = withStyles(styles)(
102     class extends React.Component<SearchBarProps> {
103         state: SearchBarState = {
104             value: ""
105         };
106
107         timeout: number;
108
109         render() {
110             const { classes, currentView, openView, closeView, open } = this.props;
111             return <ClickAwayListener onClickAway={() => closeView()}>
112                 <Paper className={open ? classes.containerSearchViewOpened : classes.container} >
113                     <form onSubmit={this.handleSubmit} className={classes.searchBar}>
114                         <Input
115                             className={classes.input}
116                             onChange={this.handleChange}
117                             placeholder="Search"
118                             value={this.state.value}
119                             fullWidth={true}
120                             disableUnderline={true}
121                             onClick={() => openView()}
122                             endAdornment={
123                                 <InputAdornment position="end">
124                                     <Tooltip title='Search'>
125                                         <IconButton>
126                                             <SearchIcon />
127                                         </IconButton>
128                                     </Tooltip>
129                                 </InputAdornment>
130                             } />
131                     </form>
132                     <div className={classes.view}>
133                         {open && this.getView(currentView)}
134                     </div>
135                 </Paper>
136             </ClickAwayListener>;
137         }
138
139         componentDidMount() {
140             this.setState({ value: this.props.searchValue });
141         }
142
143         componentWillReceiveProps(nextProps: SearchBarProps) {
144             if (nextProps.searchValue !== this.props.searchValue) {
145                 this.setState({ value: nextProps.searchValue });
146             }
147         }
148
149         componentWillUnmount() {
150             clearTimeout(this.timeout);
151         }
152
153         getView = (currentView: string) => {
154             switch (currentView) {
155                 case SearchView.BASIC:
156                     return <SearchBarBasicView setView={this.props.onSetView} recentQueries={this.props.loadQueries} />;
157                 case SearchView.ADVANCED:
158                     return <SearchBarAdvancedView setView={this.props.onSetView} />;
159                 case SearchView.AUTOCOMPLETE:
160                     return <SearchBarAutocompleteView 
161                                 searchResults={this.props.searchResults} 
162                                 searchValue={this.props.searchValue} />;
163                 default:
164                     return <SearchBarBasicView setView={this.props.onSetView} recentQueries={this.props.loadQueries} />;
165             }
166         }
167
168         handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
169             event.preventDefault();
170             clearTimeout(this.timeout);
171             this.props.saveQuery(this.state.value);
172             this.props.onSearch(this.state.value);
173             this.props.loadQueries();
174         }
175
176         handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
177             clearTimeout(this.timeout);
178             this.setState({ value: event.target.value });
179             this.timeout = window.setTimeout(
180                 () => this.props.onSearch(this.state.value),
181                 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
182             );
183             if (event.target.value.length > 0) {
184                 this.props.onSetView(SearchView.AUTOCOMPLETE);
185             } else {
186                 this.props.onSetView(SearchView.BASIC);
187             }
188         }
189     }
190 );