@@ -10,6 +10,7 @@ import { type ListScrollItem, ListScrollItemType } from '~/models/list-scroll.mo
10
10
import { NotificationService } from '~/services/notification.service' ;
11
11
import { TraktService } from '~/services/trakt.service' ;
12
12
import { storage } from '~/utils/browser/browser-storage.utils' ;
13
+ import { debounce } from '~/utils/debounce.utils' ;
13
14
import { debounceLoading , useLoadingPlaceholder } from '~/utils/store.utils' ;
14
15
15
16
export type SearchResult = Omit < TraktSearchResult , 'type' > & {
@@ -33,26 +34,64 @@ export const useSearchStore = defineStore('data.search', () => {
33
34
34
35
const searchResults = ref < SearchResult [ ] > ( [ ] ) ;
35
36
37
+ const history = ref < Set < string > > ( new Set ( ) ) ;
38
+
39
+ const saveHistory = debounce ( ( ) => storage . local . set ( 'data.search.history' , [ ...history . value ] ) , 1000 ) ;
40
+ const saveSearch = debounce ( ( ) => storage . local . set ( 'data.search.last' , { value : search . value , date : Date . now ( ) } ) , 1000 ) ;
41
+
42
+ const addToHistory = ( value : string = search . value ) => {
43
+ const newArray = [ ...history . value , value ] . filter ( Boolean ) ;
44
+ // Keep only the last 100 elements
45
+ if ( newArray . length > 100 ) {
46
+ history . value = new Set ( newArray . slice ( - 100 ) ) ;
47
+ } else {
48
+ history . value = new Set ( newArray ) ;
49
+ }
50
+ return Promise . all ( [ saveHistory ( ) , saveSearch ( ) ] ) ;
51
+ } ;
52
+
36
53
const clearState = ( ) => {
37
54
types . value = DefaultSearchType ;
38
55
query . value = false ;
39
56
pagination . value = undefined ;
40
57
search . value = '' ;
58
+ history . value = new Set ( ) ;
41
59
} ;
42
60
43
61
const saveState = async ( ) =>
44
62
storage . local . set ( 'data.search' , {
45
63
types : [ ...types . value ] ,
46
64
query : query . value ,
65
+ pageSize : pageSize . value ,
66
+ search : {
67
+ value : search . value ,
68
+ date : new Date ( ) ,
69
+ } ,
47
70
} ) ;
48
71
49
72
const restoreState = async ( ) => {
50
- const restored = await storage . local . get < {
51
- types : TraktSearchType [ ] ;
52
- query : boolean ;
53
- } > ( 'data.search' ) ;
54
- if ( restored ?. types ) types . value = restored . types ;
55
- if ( restored ?. query ) query . value = restored . query ;
73
+ const [ _state , _history , _search ] = await Promise . all ( [
74
+ storage . local . get < {
75
+ types : TraktSearchType [ ] ;
76
+ query : boolean ;
77
+ pageSize : number ;
78
+ } > ( 'data.search' ) ,
79
+ storage . local . get < string [ ] > ( 'data.search.history' ) ,
80
+ storage . local . get < {
81
+ value : string ;
82
+ date : Date ;
83
+ } > ( 'data.search.last' ) ,
84
+ ] ) ;
85
+
86
+ if ( _state ?. types ) types . value = _state . types ;
87
+ if ( _state ?. query ) query . value = _state . query ;
88
+ if ( _state ?. pageSize ) pageSize . value = _state . pageSize ;
89
+ if ( _history ) history . value = new Set ( _history ) ;
90
+
91
+ if ( _search ?. date ) {
92
+ const day = new Date ( _search . date ) . toLocaleDateString ( ) ;
93
+ if ( day === new Date ( ) . toLocaleDateString ( ) ) search . value = _search . value ;
94
+ }
56
95
} ;
57
96
58
97
const loadingPlaceholder = useLoadingPlaceholder < SearchResult > ( pageSize ) ;
@@ -105,7 +144,10 @@ export const useSearchStore = defineStore('data.search', () => {
105
144
const initSearchStore = async ( ) => {
106
145
await restoreState ( ) ;
107
146
108
- watch ( search , ( ) => fetchSearchResults ( ) ) ;
147
+ watch ( search , async ( ) => {
148
+ await fetchSearchResults ( ) ;
149
+ await addToHistory ( ) . catch ( e => console . error ( 'Failed to save search history' , e ) ) ;
150
+ } ) ;
109
151
110
152
watch ( pageSize , async ( ) => {
111
153
await fetchSearchResults ( ) ;
@@ -122,6 +164,7 @@ export const useSearchStore = defineStore('data.search', () => {
122
164
types,
123
165
query,
124
166
search,
167
+ history,
125
168
clearState,
126
169
initSearchStore,
127
170
fetchSearchResults,
0 commit comments