1
- import { TraktApiExtended , type TraktClientPagination , type TraktHistory , type TraktHistoryGetQuery } from '@dvcol/trakt-http-client/models' ;
1
+ import { TraktApiExtended } from '@dvcol/trakt-http-client/models' ;
2
+
2
3
import { defineStore , storeToRefs } from 'pinia' ;
3
4
import { computed , reactive , ref , watch } from 'vue' ;
4
5
6
+ import type { TraktApiIds , TraktClientPagination , TraktHistory , TraktHistoryGetQuery } from '@dvcol/trakt-http-client/models' ;
7
+
5
8
import type { ErrorDictionary } from '~/utils/retry.utils' ;
6
9
7
10
import { PageSize } from '~/models/page-size.model' ;
@@ -13,6 +16,13 @@ import { storage } from '~/utils/browser/browser-storage.utils';
13
16
import { debounceLoading , useBelowThreshold , useLoadingPlaceholder , useSearchFilter } from '~/utils/store.utils' ;
14
17
import { clearProxy } from '~/utils/vue.utils' ;
15
18
19
+ type IdDictionary = Record < string , Partial < TraktApiIds > > ;
20
+
21
+ type HistoryDictionary = {
22
+ movie ?: IdDictionary ;
23
+ episode ?: IdDictionary ;
24
+ } ;
25
+
16
26
const HistoryStoreConstants = {
17
27
Store : 'data.history' ,
18
28
} as const ;
@@ -29,6 +39,7 @@ export const useHistoryStore = defineStore(HistoryStoreConstants.Store, () => {
29
39
const loading = ref ( true ) ;
30
40
const pageSize = ref ( PageSize . p100 ) ;
31
41
const history = ref < TraktHistory [ ] > ( [ ] ) ;
42
+ const historyDictionary = reactive < HistoryDictionary > ( { } ) ;
32
43
const pagination = ref < TraktClientPagination > ( ) ;
33
44
const extended = ref ( false ) ;
34
45
@@ -66,6 +77,7 @@ export const useHistoryStore = defineStore(HistoryStoreConstants.Store, () => {
66
77
historyStart . value = undefined ;
67
78
historyEnd . value = undefined ;
68
79
clearProxy ( historyErrors ) ;
80
+ clearProxy ( historyDictionary ) ;
69
81
} ;
70
82
71
83
const belowThreshold = useBelowThreshold ( threshold , pagination ) ;
@@ -117,6 +129,10 @@ export const useHistoryStore = defineStore(HistoryStoreConstants.Store, () => {
117
129
await saveState ( ) ;
118
130
} ;
119
131
132
+ const getHistory = ( id : number , type : TraktHistory [ 'type' ] ) => computed ( ( ) => historyDictionary [ type ] ?. [ id ] ) ;
133
+ const getMovieHistory = ( id : number ) => getHistory ( id , 'movie' ) ;
134
+ const getEpisodeHistory = ( id : number ) => getHistory ( id , 'episode' ) ;
135
+
120
136
const initHistoryStore = async ( ) => {
121
137
await restoreState ( ) ;
122
138
@@ -125,6 +141,19 @@ export const useHistoryStore = defineStore(HistoryStoreConstants.Store, () => {
125
141
searchHistory . value = '' ;
126
142
await saveState ( ) ;
127
143
} ) ;
144
+
145
+ watch ( history , newHistory => {
146
+ clearProxy ( historyDictionary ) ;
147
+ newHistory ?. forEach ( h => {
148
+ if ( h . type === 'movie' && 'movie' in h ) {
149
+ if ( ! historyDictionary . movie ) historyDictionary . movie = { } ;
150
+ historyDictionary . movie [ h . movie . ids . trakt ] = h . movie . ids ;
151
+ } else if ( h . type === 'episode' && 'episode' in h ) {
152
+ if ( ! historyDictionary . episode ) historyDictionary . episode = { } ;
153
+ historyDictionary . episode [ h . episode . ids . trakt ] = h . episode . ids ;
154
+ }
155
+ } ) ;
156
+ } ) ;
128
157
} ;
129
158
130
159
return {
@@ -141,6 +170,9 @@ export const useHistoryStore = defineStore(HistoryStoreConstants.Store, () => {
141
170
historyStart,
142
171
historyEnd,
143
172
setHistoryRange,
173
+ getHistory,
174
+ getMovieHistory,
175
+ getEpisodeHistory,
144
176
clearState,
145
177
initHistoryStore,
146
178
extended : computed ( {
0 commit comments