af643c89fc20597000791cf694570f24eb07b14d
[arvados-workbench2.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 export class SearchQueriesService {
6     private recentQueries: string[] = this.getRecentQueries();
7     private savedQueries: string[] = this.getSavedQueries();
8
9     saveRecentQuery(query: string) {
10         if (this.recentQueries.length >= 5) {
11             this.recentQueries.shift();
12             this.recentQueries.push(query);
13         } else {
14             this.recentQueries.push(query);
15         }
16         localStorage.setItem('recentQueries', JSON.stringify(this.recentQueries));
17     }
18
19     getRecentQueries() {
20         return JSON.parse(localStorage.getItem('recentQueries') || '[]') as string[];
21     }
22
23     saveQuery(query: string) {
24         this.savedQueries.push(query);
25         localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries));
26     }
27
28     getSavedQueries() {
29         return JSON.parse(localStorage.getItem('savedQueries') || '[]') as string[];
30     }
31
32     deleteSavedQuery(id: number) {
33         const queryToDelete = this.savedQueries[id];
34         const restQueries = this.savedQueries.filter(query => query !== queryToDelete);
35         return localStorage.setItem('savedQueries', JSON.stringify(restQueries));
36     }
37 }