Merge branch '19421-restore-old-redirect-key' into main. Closes #19421
[arvados.git] / src / services / search-service / search-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { SearchBarAdvancedFormData } from 'models/search-bar';
6
7 export class SearchService {
8     private recentQueries = this.getRecentQueries();
9     private savedQueries: SearchBarAdvancedFormData[] = this.getSavedQueries();
10
11     saveRecentQuery(query: string) {
12         if (this.recentQueries.length >= MAX_NUMBER_OF_RECENT_QUERIES) {
13             this.recentQueries.shift();
14         }
15         this.recentQueries.push(query);
16         localStorage.setItem('recentQueries', JSON.stringify(this.recentQueries));
17     }
18
19     getRecentQueries(): string[] {
20         return JSON.parse(localStorage.getItem('recentQueries') || '[]');
21     }
22
23     saveQuery(data: SearchBarAdvancedFormData) {
24         this.savedQueries.push({...data});
25         localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries));
26     }
27
28     editSavedQueries(data: SearchBarAdvancedFormData) {
29         const itemIndex = this.savedQueries.findIndex(item => item.queryName === data.queryName);
30         this.savedQueries[itemIndex] = {...data};
31         localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries));
32     }
33
34     getSavedQueries() {
35         return JSON.parse(localStorage.getItem('savedQueries') || '[]') as SearchBarAdvancedFormData[];
36     }
37
38     deleteSavedQuery(id: number) {
39         this.savedQueries.splice(id, 1);
40         localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries));
41     }
42 }
43
44 const MAX_NUMBER_OF_RECENT_QUERIES = 5;