19079: Add search results context menu with clipboard and new tab options
[arvados-workbench2.git] / src / store / search-bar / search-bar-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ofType, unionize, UnionOf } from "common/unionize";
6 import { GroupContentsResource, GroupContentsResourcePrefix } from 'services/groups-service/groups-service';
7 import { Dispatch } from 'redux';
8 import { change, initialize, untouch } from 'redux-form';
9 import { RootState } from 'store/store';
10 import { initUserProject, treePickerActions } from 'store/tree-picker/tree-picker-actions';
11 import { ServiceRepository } from 'services/services';
12 import { FilterBuilder } from "services/api/filter-builder";
13 import { ResourceKind, RESOURCE_UUID_REGEX, COLLECTION_PDH_REGEX } from 'models/resource';
14 import { SearchView } from 'store/search-bar/search-bar-reducer';
15 import { navigateTo, navigateToSearchResults } from 'store/navigation/navigation-action';
16 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
17 import { PropertyValue, SearchBarAdvancedFormData } from 'models/search-bar';
18 import { union } from "lodash";
19 import { getModifiedKeysValues } from "common/objects";
20 import { activateSearchBarProject } from "store/search-bar/search-bar-tree-actions";
21 import { Session } from "models/session";
22 import { searchResultsPanelActions } from "store/search-results-panel/search-results-panel-actions";
23 import { ListResults } from "services/common-service/common-service";
24 import * as parser from './search-query/arv-parser';
25 import { Keywords } from './search-query/arv-parser';
26 import { Vocabulary, getTagKeyLabel, getTagValueLabel } from "models/vocabulary";
27
28 export const searchBarActions = unionize({
29     SET_CURRENT_VIEW: ofType<string>(),
30     OPEN_SEARCH_VIEW: ofType<{}>(),
31     CLOSE_SEARCH_VIEW: ofType<{}>(),
32     SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
33     SET_SEARCH_VALUE: ofType<string>(),
34     SET_SAVED_QUERIES: ofType<SearchBarAdvancedFormData[]>(),
35     SET_RECENT_QUERIES: ofType<string[]>(),
36     UPDATE_SAVED_QUERY: ofType<SearchBarAdvancedFormData[]>(),
37     SET_SELECTED_ITEM: ofType<string>(),
38     MOVE_UP: ofType<{}>(),
39     MOVE_DOWN: ofType<{}>(),
40     SELECT_FIRST_ITEM: ofType<{}>()
41 });
42
43 export type SearchBarActions = UnionOf<typeof searchBarActions>;
44
45 export const SEARCH_BAR_ADVANCED_FORM_NAME = 'searchBarAdvancedFormName';
46
47 export const SEARCH_BAR_ADVANCED_FORM_PICKER_ID = 'searchBarAdvancedFormPickerId';
48
49 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
50
51 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
52
53 export const saveRecentQuery = (query: string) =>
54     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
55         services.searchService.saveRecentQuery(query);
56
57
58 export const loadRecentQueries = () =>
59     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
60         const recentQueries = services.searchService.getRecentQueries();
61         dispatch(searchBarActions.SET_RECENT_QUERIES(recentQueries));
62         return recentQueries;
63     };
64
65 export const searchData = (searchValue: string) =>
66     async (dispatch: Dispatch, getState: () => RootState) => {
67         const currentView = getState().searchBar.currentView;
68         dispatch(searchResultsPanelActions.CLEAR());
69         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
70         if (searchValue.length > 0) {
71             dispatch<any>(searchGroups(searchValue, 5));
72             if (currentView === SearchView.BASIC) {
73                 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
74                 dispatch(navigateToSearchResults(searchValue));
75             }
76         }
77     };
78
79 export const searchAdvancedData = (data: SearchBarAdvancedFormData) =>
80     async (dispatch: Dispatch, getState: () => RootState) => {
81         dispatch<any>(saveQuery(data));
82         const searchValue = getState().searchBar.searchValue;
83         dispatch(searchResultsPanelActions.CLEAR());
84         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
85         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
86         dispatch(navigateToSearchResults(searchValue));
87     };
88
89 export const setSearchValueFromAdvancedData = (data: SearchBarAdvancedFormData, prevData?: SearchBarAdvancedFormData) =>
90     (dispatch: Dispatch, getState: () => RootState) => {
91         const searchValue = getState().searchBar.searchValue;
92         const value = getQueryFromAdvancedData({
93             ...data,
94             searchValue
95         }, prevData);
96         dispatch(searchBarActions.SET_SEARCH_VALUE(value));
97     };
98
99 export const setAdvancedDataFromSearchValue = (search: string, vocabulary: Vocabulary) =>
100     async (dispatch: Dispatch) => {
101         const data = getAdvancedDataFromQuery(search, vocabulary);
102         dispatch<any>(initialize(SEARCH_BAR_ADVANCED_FORM_NAME, data));
103         if (data.projectUuid) {
104             await dispatch<any>(activateSearchBarProject(data.projectUuid));
105             dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID, id: data.projectUuid }));
106         }
107     };
108
109 const saveQuery = (data: SearchBarAdvancedFormData) =>
110     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
111         const savedQueries = services.searchService.getSavedQueries();
112         if (data.saveQuery && data.queryName) {
113             const filteredQuery = savedQueries.find(query => query.queryName === data.queryName);
114             data.searchValue = getState().searchBar.searchValue;
115             if (filteredQuery) {
116                 services.searchService.editSavedQueries(data);
117                 dispatch(searchBarActions.UPDATE_SAVED_QUERY(savedQueries));
118                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been successfully updated', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
119             } else {
120                 services.searchService.saveQuery(data);
121                 dispatch(searchBarActions.SET_SAVED_QUERIES(savedQueries));
122                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been successfully saved', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
123             }
124         }
125     };
126
127 export const deleteSavedQuery = (id: number) =>
128     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
129         services.searchService.deleteSavedQuery(id);
130         const savedSearchQueries = services.searchService.getSavedQueries();
131         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
132         return savedSearchQueries || [];
133     };
134
135 export const editSavedQuery = (data: SearchBarAdvancedFormData) =>
136     (dispatch: Dispatch<any>) => {
137         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED));
138         dispatch(searchBarActions.SET_SEARCH_VALUE(getQueryFromAdvancedData(data)));
139         dispatch<any>(initialize(SEARCH_BAR_ADVANCED_FORM_NAME, data));
140     };
141
142 export const openSearchView = () =>
143     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
144         const savedSearchQueries = services.searchService.getSavedQueries();
145         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
146         dispatch(loadRecentQueries());
147         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
148         dispatch(searchBarActions.SELECT_FIRST_ITEM());
149     };
150
151 export const closeSearchView = () =>
152     (dispatch: Dispatch<any>) => {
153         dispatch(searchBarActions.SET_SELECTED_ITEM(''));
154         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
155     };
156
157 export const closeAdvanceView = () =>
158     (dispatch: Dispatch<any>) => {
159         dispatch(searchBarActions.SET_SEARCH_VALUE(''));
160         dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
161         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
162     };
163
164 export const navigateToItem = (uuid: string) =>
165     (dispatch: Dispatch<any>) => {
166         dispatch(searchBarActions.SET_SELECTED_ITEM(''));
167         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
168         dispatch(navigateTo(uuid));
169     };
170
171 export const changeData = (searchValue: string) =>
172     (dispatch: Dispatch, getState: () => RootState) => {
173         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
174         const currentView = getState().searchBar.currentView;
175         const searchValuePresent = searchValue.length > 0;
176
177         if (currentView === SearchView.ADVANCED) {
178             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE));
179         } else if (searchValuePresent) {
180             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE));
181             dispatch(searchBarActions.SET_SELECTED_ITEM(searchValue));
182         } else {
183             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
184             dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
185             dispatch(searchBarActions.SELECT_FIRST_ITEM());
186         }
187     };
188
189 export const submitData = (event: React.FormEvent<HTMLFormElement>) =>
190     (dispatch: Dispatch, getState: () => RootState) => {
191         event.preventDefault();
192         const searchValue = getState().searchBar.searchValue;
193         dispatch<any>(saveRecentQuery(searchValue));
194         dispatch<any>(loadRecentQueries());
195         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
196         if (RESOURCE_UUID_REGEX.exec(searchValue) || COLLECTION_PDH_REGEX.exec(searchValue)) {
197             dispatch<any>(navigateTo(searchValue));
198         } else {
199             dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
200             dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
201             dispatch(searchResultsPanelActions.CLEAR());
202             dispatch(navigateToSearchResults(searchValue));
203         }
204     };
205
206
207 const searchGroups = (searchValue: string, limit: number) =>
208     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
209         const currentView = getState().searchBar.currentView;
210
211         if (searchValue || currentView === SearchView.ADVANCED) {
212             const { cluster: clusterId } = getAdvancedDataFromQuery(searchValue);
213             const sessions = getSearchSessions(clusterId, getState().auth.sessions);
214             const lists: ListResults<GroupContentsResource>[] = await Promise.all(sessions.map(session => {
215                 const filters = queryToFilters(searchValue, session.apiRevision);
216                 return services.groupsService.contents('', {
217                     filters,
218                     limit,
219                     recursive: true
220                 }, session);
221             }));
222
223             const items = lists.reduce((items, list) => items.concat(list.items), [] as GroupContentsResource[]);
224             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
225         }
226     };
227
228 const buildQueryFromKeyMap = (data: any, keyMap: string[][]) => {
229     let value = data.searchValue;
230
231     const addRem = (field: string, key: string) => {
232         const v = data[key];
233         // Remove previous search expression.
234         if (data.hasOwnProperty(key)) {
235             let pattern: string;
236             if (v === false) {
237                 pattern = `${field.replace(':', '\\:\\s*')}\\s*`;
238             } else if (key.startsWith('prop-')) {
239                 // On properties, only remove key:value duplicates, allowing
240                 // multiple properties with the same key.
241                 const oldValue = key.slice(5).split(':')[1];
242                 pattern = `${field.replace(':', '\\:\\s*')}\\:\\s*${oldValue}\\s*`;
243             } else {
244                 pattern = `${field.replace(':', '\\:\\s*')}\\:\\s*[\\w|\\#|\\-|\\/]*\\s*`;
245             }
246             value = value.replace(new RegExp(pattern), '');
247         }
248         // Re-add it with the current search value.
249         if (v) {
250             const nv = v === true
251                 ? `${field}`
252                 : `${field}:${v}`;
253             // Always append to the end to keep user-entered text at the start.
254             value = value + ' ' + nv;
255         }
256     };
257     keyMap.forEach(km => addRem(km[0], km[1]));
258     return value;
259 };
260
261 export const getQueryFromAdvancedData = (data: SearchBarAdvancedFormData, prevData?: SearchBarAdvancedFormData) => {
262     let value = '';
263
264     const flatData = (data: SearchBarAdvancedFormData) => {
265         const fo = {
266             searchValue: data.searchValue,
267             type: data.type,
268             cluster: data.cluster,
269             projectUuid: data.projectUuid,
270             inTrash: data.inTrash,
271             pastVersions: data.pastVersions,
272             dateFrom: data.dateFrom,
273             dateTo: data.dateTo,
274         };
275         (data.properties || []).forEach(p =>
276             fo[`prop-"${p.keyID || p.key}":"${p.valueID || p.value}"`] = `"${p.valueID || p.value}"`
277             );
278         return fo;
279     };
280
281     const keyMap = [
282         ['type', 'type'],
283         ['cluster', 'cluster'],
284         ['project', 'projectUuid'],
285         [`is:${parser.States.TRASHED}`, 'inTrash'],
286         [`is:${parser.States.PAST_VERSION}`, 'pastVersions'],
287         ['from', 'dateFrom'],
288         ['to', 'dateTo']
289     ];
290     union(data.properties, prevData ? prevData.properties : [])
291         .forEach(p => keyMap.push(
292             [`has:"${p.keyID || p.key}"`, `prop-"${p.keyID || p.key}":"${p.valueID || p.value}"`]
293         ));
294
295     const modified = getModifiedKeysValues(flatData(data), prevData ? flatData(prevData):{});
296     value = buildQueryFromKeyMap(
297         {searchValue: data.searchValue, ...modified} as SearchBarAdvancedFormData, keyMap);
298
299     value = value.trim();
300     return value;
301 };
302
303 export const getAdvancedDataFromQuery = (query: string, vocabulary?: Vocabulary): SearchBarAdvancedFormData => {
304     const { tokens, searchString } = parser.parseSearchQuery(query);
305     const getValue = parser.getValue(tokens);
306     return {
307         searchValue: searchString,
308         type: getValue(Keywords.TYPE) as ResourceKind,
309         cluster: getValue(Keywords.CLUSTER),
310         projectUuid: getValue(Keywords.PROJECT),
311         inTrash: parser.isTrashed(tokens),
312         pastVersions: parser.isPastVersion(tokens),
313         dateFrom: getValue(Keywords.FROM) || '',
314         dateTo: getValue(Keywords.TO) || '',
315         properties: vocabulary
316             ? parser.getProperties(tokens).map(
317                 p => {
318                     return {
319                         keyID: p.key,
320                         key: getTagKeyLabel(p.key, vocabulary),
321                         valueID: p.value,
322                         value: getTagValueLabel(p.key, p.value, vocabulary),
323                     };
324                 })
325             : parser.getProperties(tokens),
326         saveQuery: false,
327         queryName: ''
328     };
329 };
330
331 export const getSearchSessions = (clusterId: string | undefined, sessions: Session[]): Session[] => {
332     return sessions.filter(s => s.loggedIn && (!clusterId || s.clusterId === clusterId));
333 };
334
335 export const queryToFilters = (query: string, apiRevision: number) => {
336     const data = getAdvancedDataFromQuery(query);
337     const filter = new FilterBuilder();
338     const resourceKind = data.type;
339
340     if (data.searchValue) {
341         filter.addFullTextSearch(data.searchValue);
342     }
343
344     if (data.projectUuid) {
345         filter.addEqual('owner_uuid', data.projectUuid);
346     }
347
348     if (data.dateFrom) {
349         filter.addGte('modified_at', buildDateFilter(data.dateFrom));
350     }
351
352     if (data.dateTo) {
353         filter.addLte('modified_at', buildDateFilter(data.dateTo));
354     }
355
356     data.properties.forEach(p => {
357         if (p.value) {
358             if (apiRevision < 20200212) {
359                 filter
360                     .addEqual(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROJECT)
361                     .addEqual(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.COLLECTION)
362                     .addEqual(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROCESS);
363             } else {
364                 filter
365                     .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROJECT)
366                     .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.COLLECTION)
367                     .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROCESS);
368             }
369         }
370         filter.addExists(p.key);
371     });
372
373     return filter
374         .addIsA("uuid", buildUuidFilter(resourceKind))
375         .getFilters();
376 };
377
378 const buildUuidFilter = (type?: ResourceKind): ResourceKind[] => {
379     return type ? [type] : [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS];
380 };
381
382 const buildDateFilter = (date?: string): string => {
383     return date ? date : '';
384 };
385
386 export const initAdvancedFormProjectsTree = () =>
387     (dispatch: Dispatch) => {
388         dispatch<any>(initUserProject(SEARCH_BAR_ADVANCED_FORM_PICKER_ID));
389     };
390
391 export const changeAdvancedFormProperty = (propertyField: string, value: PropertyValue[] | string = '') =>
392     (dispatch: Dispatch) => {
393         dispatch(change(SEARCH_BAR_ADVANCED_FORM_NAME, propertyField, value));
394     };
395
396 export const resetAdvancedFormProperty = (propertyField: string) =>
397     (dispatch: Dispatch) => {
398         dispatch(change(SEARCH_BAR_ADVANCED_FORM_NAME, propertyField, null));
399         dispatch(untouch(SEARCH_BAR_ADVANCED_FORM_NAME, propertyField));
400     };
401
402 export const moveUp = () =>
403     (dispatch: Dispatch) => {
404         dispatch(searchBarActions.MOVE_UP());
405     };
406
407 export const moveDown = () =>
408     (dispatch: Dispatch) => {
409         dispatch(searchBarActions.MOVE_DOWN());
410     };