1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
13 InputAdornment, Input,
14 ListItem, ListItemText, ListItemSecondaryAction,
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';
26 type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'view';
28 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
33 borderRadius: theme.spacing.unit / 2
35 containerSearchViewOpened: {
38 borderRadius: `${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px 0 0`
42 padding: `0px ${theme.spacing.unit}px`
52 type SearchBarDataProps = {
55 isPopoverOpen: boolean;
56 savedQueries: SearchBarAdvanceFormData[];
57 } & SearchBarAutocompleteViewDataProps;
59 interface SearchBarActionProps {
60 onSearch: (value: string) => any;
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;
73 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
75 interface SearchBarState {
79 interface RenderRecentQueriesProps {
81 onSearch: (searchValue: string) => void;
84 export const RenderRecentQueries = (props: RenderRecentQueriesProps) => {
85 return <ListItem button>
86 <ListItemText secondary={props.text} onClick={() => props.onSearch(props.text)} />
90 interface RenderAutocompleteItemsProps {
91 text: string | JSX.Element;
92 navigateTo: (uuid: string) => void;
96 export const RenderAutocompleteItems = (props: RenderAutocompleteItemsProps) => {
97 return <ListItem button>
98 <ListItemText secondary={props.text} onClick={() => props.navigateTo(props.uuid)} />
102 interface RenderSavedQueriesProps {
105 deleteSavedQuery: (id: number) => void;
106 onSearch: (searchValue: string) => void;
107 editSavedQuery: (data: SearchBarAdvanceFormData, id: number) => void;
108 data: SearchBarAdvanceFormData;
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 />
120 <Tooltip title="Remove">
121 <IconButton aria-label="Remove" onClick={() => props.deleteSavedQuery(props.id)}>
125 </ListItemSecondaryAction>
129 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
131 export const SearchBarView = withStyles(styles)(
132 class extends React.Component<SearchBarProps> {
133 state: SearchBarState = {
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}>
145 className={classes.input}
146 onChange={this.handleChange}
148 value={this.state.value}
150 disableUnderline={true}
151 onClick={openSearchView}
153 <InputAdornment position="end">
154 <Tooltip title='Search'>
162 <div className={classes.view}>
163 {isPopoverOpen && this.getView(currentView)}
166 </ClickAwayListener>;
169 componentDidMount() {
170 this.setState({ value: this.props.searchValue });
173 componentWillReceiveProps(nextProps: SearchBarProps) {
174 if (nextProps.searchValue !== this.props.searchValue) {
175 this.setState({ value: nextProps.searchValue });
179 componentWillUnmount() {
180 clearTimeout(this.timeout);
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} />;
196 return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} onSearch={onSearch} editSavedQuery={editSavedQuery} />;
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();
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
215 if (event.target.value.length > 0) {
216 this.props.onSetView(SearchView.AUTOCOMPLETE);
218 this.props.onSetView(SearchView.BASIC);