@@ -10,13 +10,14 @@ import type { TraktWatchlist } from '~/models/trakt/trakt-watchlist.model';
10
10
11
11
import { TraktService } from '~/services/trakt.service' ;
12
12
import { useUserSettingsStoreRefs } from '~/stores/settings/user.store' ;
13
+ import { storage } from '~/utils/browser/browser-storage.utils' ;
13
14
import { debounceLoading , useBelowThreshold , useLoadingPlaceholder , useSearchFilter } from '~/utils/store.utils' ;
14
15
15
16
export type AnyList = TraktListItem | TraktWatchlist | ( TraktCollection & { id : number } ) ;
16
17
export type ListType = {
17
18
type : 'list' | 'collaboration' | 'collection' | 'watchlist' ;
18
19
name : string ;
19
- id ? : number | string ;
20
+ id : number | string ;
20
21
scope ?: 'movies' | 'shows' ;
21
22
} ;
22
23
@@ -29,9 +30,9 @@ export const anyListDateGetter = (item: AnyList) => {
29
30
} ;
30
31
31
32
export const DefaultLists : Record < string , ListType > = {
32
- Watchlist : { type : 'watchlist' , name : 'list_type__watchlist' } ,
33
- MovieCollection : { type : 'collection' , scope : 'movies' , name : 'list_type__collection_movie' } ,
34
- ShowCollection : { type : 'collection' , scope : 'shows' , name : 'list_type__collection_show' } ,
33
+ Watchlist : { type : 'watchlist' , id : 'watchlist' , name : 'list_type__watchlist' } ,
34
+ MovieCollection : { type : 'collection' , id : 'collection-movies' , scope : 'movies' , name : 'list_type__collection_movie' } ,
35
+ ShowCollection : { type : 'collection' , id : 'collection-shows' , scope : 'shows' , name : 'list_type__collection_show' } ,
35
36
} as const ;
36
37
37
38
const DefaultList : ListType [ ] = [ DefaultLists . Watchlist , DefaultLists . MovieCollection , DefaultLists . ShowCollection ] ;
@@ -42,6 +43,22 @@ export const useListsStore = defineStore('data.lists', () => {
42
43
const lists = ref < ListType [ ] > ( DefaultList ) ;
43
44
const activeList = ref < ListType > ( DefaultLists . Watchlist ) ;
44
45
46
+ const saveState = async ( ) =>
47
+ storage . local . set ( 'data.lists' , {
48
+ lists : [ ...lists . value ] ,
49
+ activeList : activeList . value ,
50
+ } ) ;
51
+
52
+ const restoreState = async ( ) => {
53
+ const restored = await storage . local . get < {
54
+ lists : ListType [ ] ;
55
+ activeList : ListType ;
56
+ } > ( 'data.lists' ) ;
57
+ if ( restored ?. lists ) lists . value = restored . lists ;
58
+ if ( restored ?. activeList === activeList . value ) return ;
59
+ if ( restored ?. activeList ) activeList . value = restored . activeList ;
60
+ } ;
61
+
45
62
const clearState = ( ) => {
46
63
lists . value = DefaultList ;
47
64
activeList . value = DefaultLists . Watchlist ;
@@ -73,15 +90,28 @@ export const useListsStore = defineStore('data.lists', () => {
73
90
} ) satisfies ListType ,
74
91
) ,
75
92
] ;
93
+ if ( activeList . value ?. id && ! lists . value . some ( l => activeList . value . id === l ?. id ) ) {
94
+ console . warn ( 'Active List not found, falling back to default' , activeList . value ) ;
95
+ activeList . value = DefaultLists . Watchlist ;
96
+ }
76
97
} catch ( e ) {
77
98
console . error ( 'Failed to fetch lists' ) ;
78
99
throw e ;
79
100
} finally {
80
101
loading . value = false ;
102
+ console . info ( 'Fetched Lists' , loading . value ) ;
81
103
}
82
104
} ;
83
105
84
- return { loading, lists, activeList, fetchLists, clearState } ;
106
+ const initListsStore = async ( ) => {
107
+ await restoreState ( ) ;
108
+
109
+ watch ( activeList , async _value => {
110
+ await saveState ( ) ;
111
+ } ) ;
112
+ } ;
113
+
114
+ return { loading, lists, activeList, fetchLists, clearState, initListsStore } ;
85
115
} ) ;
86
116
87
117
export const useListsStoreRefs = ( ) => storeToRefs ( useListsStore ( ) ) ;
@@ -95,6 +125,13 @@ export const useListStore = defineStore('data.list', () => {
95
125
const searchList = ref ( '' ) ;
96
126
const threshold = ref ( 10 ) ;
97
127
128
+ const saveState = async ( ) => storage . local . set ( 'data.list.page-size' , pageSize . value ) ;
129
+ const restoreState = async ( ) => {
130
+ const restored = await storage . local . get < number > ( 'data.list.page-size' ) ;
131
+ if ( restored === pageSize . value ) return ;
132
+ if ( restored ) pageSize . value = restored ;
133
+ } ;
134
+
98
135
const clearState = ( ) => {
99
136
listItems . value = [ ] ;
100
137
pagination . value = undefined ;
@@ -139,7 +176,6 @@ export const useListStore = defineStore('data.list', () => {
139
176
} else {
140
177
throw new Error ( 'Invalid list type' ) ;
141
178
}
142
- console . info ( 'Fetched List' , list , response ) ;
143
179
const newData = response . data . map ( ( item , index ) => {
144
180
if ( 'id' in item ) return item ;
145
181
return { ...item , id : index } ;
@@ -156,17 +192,34 @@ export const useListStore = defineStore('data.list', () => {
156
192
}
157
193
} ;
158
194
159
- watch ( pageSize , async ( ) => {
160
- await fetchListItems ( ) ;
161
- searchList . value = '' ;
162
- } ) ;
195
+ const initListStore = async ( ) => {
196
+ await restoreState ( ) ;
163
197
164
- watch ( activeList , async ( ) => {
165
- await fetchListItems ( ) ;
166
- searchList . value = '' ;
167
- } ) ;
198
+ watch ( pageSize , async ( ) => {
199
+ await fetchListItems ( ) ;
200
+ searchList . value = '' ;
201
+ await saveState ( ) ;
202
+ } ) ;
168
203
169
- return { loading, listItems, pagination, pageSize, searchList, clearState, belowThreshold, loadingPlaceholder, fetchListItems, filteredListItems } ;
204
+ watch ( activeList , async ( ) => {
205
+ await fetchListItems ( ) ;
206
+ searchList . value = '' ;
207
+ } ) ;
208
+ } ;
209
+
210
+ return {
211
+ loading,
212
+ listItems,
213
+ pagination,
214
+ pageSize,
215
+ searchList,
216
+ clearState,
217
+ belowThreshold,
218
+ loadingPlaceholder,
219
+ fetchListItems,
220
+ filteredListItems,
221
+ initListStore,
222
+ } ;
170
223
} ) ;
171
224
172
225
export const useListStoreRefs = ( ) => storeToRefs ( useListStore ( ) ) ;
0 commit comments