1
1
<script setup lang="ts">
2
+ import { sentenceCase } from ' @dvcol/common-utils/common/string' ;
2
3
import {
3
4
type TraktEpisodeExtended ,
4
5
TraktRatingType ,
@@ -13,7 +14,13 @@ import type { RatingItem } from '~/models/rating.model';
13
14
14
15
import PanelStatistics from ' ~/components/common/panel/PanelStatistics.vue' ;
15
16
import PanelTrailers from ' ~/components/common/panel/PanelTrailers.vue' ;
16
- import { DataSource } from ' ~/models/source.model' ;
17
+ import {
18
+ DataSource ,
19
+ getUrlFromSource ,
20
+ isKnownSource ,
21
+ normalizeSource ,
22
+ sortSources ,
23
+ } from ' ~/models/source.model' ;
17
24
import { ResolveExternalLinks } from ' ~/settings/external.links' ;
18
25
import { useRatingsStore } from ' ~/stores/data/ratings.store' ;
19
26
@@ -49,8 +56,15 @@ const { mode, episode, season, show } = toRefs(props);
49
56
50
57
const { getShowLoading, getSeasonsLoading, getEpisodesLoading } = useShowStore ();
51
58
52
- const { loadRatings, getRatings, getLoading, addRating, removeRating } =
53
- useRatingsStore ();
59
+ const {
60
+ loadRatings,
61
+ getRatings,
62
+ fetchRating,
63
+ getRating,
64
+ getLoading,
65
+ addRating,
66
+ removeRating,
67
+ } = useRatingsStore ();
54
68
55
69
const { enableRatings } = useExtensionSettingsStoreRefs ();
56
70
@@ -82,25 +96,28 @@ const scoreLoading = computed(() => {
82
96
return getLoading (TraktRatingType .Shows );
83
97
});
84
98
99
+ const rType = computed <TraktRatingTypes >(() => {
100
+ if (mode .value === ' episode' ) return TraktRatingType .Episodes ;
101
+ if (mode .value === ' season' ) return TraktRatingType .Seasons ;
102
+ return TraktRatingType .Shows ;
103
+ });
104
+
85
105
const scoreIds = computed (() => {
86
106
if (! enableRatings .value ) return {};
87
107
88
108
let id: string | undefined ;
89
- let type: TraktRatingTypes | undefined ;
109
+ const type: TraktRatingTypes | undefined = rType . value ;
90
110
91
- if (! showId .value ) return { id , type };
111
+ if (! showId .value ) return { id };
92
112
if (mode .value === ' show' ) {
93
113
id = showId .value .toString ();
94
- type = TraktRatingType .Shows ;
95
114
return { id , type };
96
115
}
97
116
if (mode .value === ' season' ) {
98
- type = TraktRatingType .Seasons ;
99
117
if (seasonId .value ) id = ` ${showId .value }-${seasonId .value } ` ;
100
118
return { id , type };
101
119
}
102
120
if (mode .value === ' episode' ) {
103
- type = TraktRatingType .Episodes ;
104
121
if (episodeId .value ) id = ` ${showId .value }-${episodeId .value } ` ;
105
122
return { id , type };
106
123
}
@@ -176,35 +193,66 @@ const trailers = computed(() => {
176
193
}));
177
194
});
178
195
179
- const ratings = computed <RatingItem []>(() => {
180
- const _ratings: RatingItem [] = [];
181
- _ratings .push ({
182
- name: i18n (' trakt' , ' common' , ' source' , ' name' ),
183
- icon: DataSource .Trakt ,
184
- rating: {
185
- votes: votes .value ,
186
- rating: rating .value ,
187
- url: ratingUrl .value ,
188
- loading: ratingLoading .value ,
189
- },
190
- });
191
- if (! simklShow .value ?.ratings ) return _ratings ;
192
- if (mode .value !== ' show' ) return _ratings ;
193
- Object .entries (simklShow .value .ratings ).forEach (([key , value ]) => {
194
- _ratings .push ({
195
- name: i18n (key , ' common' , ' source' , ' name' ),
196
+ const extended = computed <[string , RatingItem ][]>(() => {
197
+ if (! showId .value ) return [];
198
+ if (! enableRatings .value ) return [];
199
+ const _query = { id: showId .value , season: seasonNb .value , episode: episodeNb .value };
200
+ const _ratings = getRating (rType .value , _query );
201
+ if (! _ratings ) return [];
202
+ return Object .entries (_ratings ).map (([key , value ]) => [
203
+ key ,
204
+ {
205
+ name: isKnownSource (key )
206
+ ? i18n (key , ' common' , ' source' , ' name' )
207
+ : sentenceCase (key .replaceAll (' _' , ' ' )),
196
208
icon: key ,
197
209
rating: {
198
210
votes: value .votes ,
199
- rating: value .rating ,
200
- loading: simklShowLoading .value ,
201
- url:
202
- key === ' mal' ? ` https://myanimelist.net/anime/${simklShow .value } ` : undefined ,
211
+ rating: normalizeSource (key , value .rating ),
212
+ loading: getLoading (rType .value , _query ),
213
+ url: key === ' trakt' ? ratingUrl .value : undefined ,
203
214
},
204
- });
205
- });
215
+ },
216
+ ]);
217
+ });
206
218
207
- return _ratings ;
219
+ const ratings = computed <RatingItem []>(() => {
220
+ const _ratings: Map <string , RatingItem > = new Map (extended .value );
221
+ if (! _ratings .has (DataSource .Trakt )) {
222
+ _ratings .set (DataSource .Trakt , {
223
+ name: i18n (' trakt' , ' common' , ' source' , ' name' ),
224
+ icon: DataSource .Trakt ,
225
+ rating: {
226
+ votes: votes .value ,
227
+ rating: rating .value ,
228
+ url: ratingUrl .value ,
229
+ loading: ratingLoading .value ,
230
+ },
231
+ });
232
+ }
233
+ if (mode .value === ' show' && simklShow .value ?.ratings ) {
234
+ Object .entries (simklShow .value .ratings ).forEach (([key , value ]) => {
235
+ _ratings .set (key , {
236
+ name: isKnownSource (key )
237
+ ? i18n (key , ' common' , ' source' , ' name' )
238
+ : sentenceCase (key .replaceAll (' _' , ' ' )),
239
+ icon: key ,
240
+ rating: {
241
+ votes: value .votes ,
242
+ rating: normalizeSource (key , value .rating ),
243
+ loading: simklShowLoading .value ,
244
+ url: getUrlFromSource (key , simklShow .value ?.ids , {
245
+ season: seasonNb .value ,
246
+ episode: episodeNb .value ,
247
+ type: mode .value ,
248
+ }),
249
+ },
250
+ });
251
+ });
252
+ }
253
+ return Array .from (_ratings .values ())
254
+ .filter (r => r .name === DataSource .Trakt || r .rating .rating )
255
+ .sort ((a , b ) => sortSources (a .name , b .name ));
208
256
});
209
257
210
258
const onScoreEdit = async (_score : TraktSyncRatingValue ) => {
@@ -255,6 +303,15 @@ onMounted(() => {
255
303
if (! id || ! type ) return ;
256
304
loadRatings (type , id );
257
305
});
306
+ watch ([showId , seasonNb , episodeNb ], () => {
307
+ if (! showId .value ) return ;
308
+ if (! enableRatings .value ) return ;
309
+ fetchRating (rType .value , {
310
+ id: showId .value ,
311
+ season: seasonNb .value ,
312
+ episode: episodeNb .value ,
313
+ });
314
+ });
258
315
});
259
316
</script >
260
317
0 commit comments