@@ -2,7 +2,7 @@ import { arrayMax, findClosestMatch } from '@dvcol/common-utils/common/math';
2
2
import { getShortLocale } from '@dvcol/common-utils/common/navigator' ;
3
3
import { defineStore , storeToRefs } from 'pinia' ;
4
4
5
- import { computed , reactive , type Ref , ref } from 'vue' ;
5
+ import { computed , reactive , ref } from 'vue' ;
6
6
7
7
import type { TmdbConfiguration , TmdbImage } from '@dvcol/tmdb-http-client/models' ;
8
8
@@ -232,66 +232,58 @@ export const useImageStore = defineStore(ImageStoreConstants.Store, () => {
232
232
const buildImageUrl = ( { url, baseUrl, type, size } : { url : string ; baseUrl : string ; type : ImageQuery [ 'type' ] ; size : number | 'original' } ) =>
233
233
`${ baseUrl } ${ getImageSize ( type , size ) } ${ url } ` ;
234
234
235
- const setResponseValue = (
236
- { image, baseUrl, type, size } : { image : ImageStoreMedias ; baseUrl : string ; type : ImageQuery [ 'type' ] ; size : number | 'original' } ,
237
- response : Ref < ImageStoreMedias | undefined > = ref ( ) ,
238
- ) : Ref < ImageStoreMedias | undefined > => {
239
- if ( typeof image === 'string' ) response . value = buildImageUrl ( { url : image , baseUrl, type, size } ) ;
240
- else {
241
- response . value = {
242
- poster : image . poster ? buildImageUrl ( { url : image . poster , baseUrl, type, size } ) : undefined ,
243
- backdrop : image . backdrop ? buildImageUrl ( { url : image . backdrop , baseUrl, type, size } ) : undefined ,
244
- } ;
245
- }
246
- return response ;
235
+ const getResponseValue = ( {
236
+ image,
237
+ baseUrl,
238
+ type,
239
+ size,
240
+ } : {
241
+ image ?: ImageStoreMedias ;
242
+ baseUrl ?: string ;
243
+ type : ImageQuery [ 'type' ] ;
244
+ size : number | 'original' ;
245
+ } ) : ImageStoreMedias | undefined => {
246
+ if ( ! baseUrl || ! image ) return undefined ;
247
+ if ( typeof image === 'string' ) return buildImageUrl ( { url : image , baseUrl, type, size } ) ;
248
+
249
+ return {
250
+ poster : image . poster ? buildImageUrl ( { url : image . poster , baseUrl, type, size } ) : undefined ,
251
+ backdrop : image . backdrop ? buildImageUrl ( { url : image . backdrop , baseUrl, type, size } ) : undefined ,
252
+ } ;
247
253
} ;
248
254
249
255
const getImageResponse = ( {
250
256
query,
251
257
size = 300 ,
252
- response = ref ( ) ,
253
258
} : {
254
259
query : ImageQuery ;
255
260
size ?: number | 'original' ;
256
- response ?: Ref < ImageStoreMedias | undefined > ;
257
261
} ) : {
258
- response : Ref < ImageStoreMedias | undefined > ;
259
262
key : string ;
260
263
type : ImageQuery [ 'type' ] ;
261
- image ?: string | ImageStoreMedias ;
264
+ image ?: ImageStoreMedias ;
262
265
baseUrl ?: string ;
263
266
} => {
264
267
const { key, type } = getKeyAndType ( query ) ;
265
268
const image = images [ type ] [ key ] ;
266
269
const baseUrl = tmdbConfig . value ?. images ?. secure_base_url ;
267
- if ( image && baseUrl ) setResponseValue ( { image, baseUrl, type, size } , response ) ;
268
- return { response, key, type, image, baseUrl } ;
270
+ return { key, type, baseUrl, image : getResponseValue ( { image, baseUrl, type, size } ) } ;
269
271
} ;
270
272
271
- const getImageUrl = async ( {
272
- query,
273
- size = 300 ,
274
- response = ref ( ) ,
275
- fetch = true ,
276
- } : {
277
- query : ImageQuery ;
278
- size ?: number | 'original' ;
279
- response ?: Ref < ImageStoreMedias | undefined > ;
280
- fetch ?: boolean ;
281
- } ) => {
273
+ const getImageUrl = async ( { query, size = 300 , fetch = true } : { query : ImageQuery ; size ?: number | 'original' ; fetch ?: boolean } ) => {
282
274
if ( ! tmdbConfig . value ) throw new Error ( 'TmdbConfiguration not initialized' ) ;
283
275
if ( ! tmdbConfig . value ?. images ?. secure_base_url ) throw new Error ( 'TmdbConfiguration missing secure_base_url' ) ;
284
276
285
- const { image, key, type, baseUrl = tmdbConfig . value . images . secure_base_url } = getImageResponse ( { query, size, response } ) ;
277
+ const { image, key, type, baseUrl = tmdbConfig . value . images . secure_base_url } = getImageResponse ( { query, size } ) ;
286
278
287
- if ( image || ! fetch ) return response ;
279
+ if ( image || ! fetch ) return image ;
288
280
289
281
const result = await fetchImageUrl ( key , query ) ;
290
- if ( ! result ?. image ) return response ;
291
- return setResponseValue ( { image : result . image , baseUrl, type, size } , response ) ;
282
+ if ( ! result ?. image ) return undefined ;
283
+ return getResponseValue ( { image : result . image , baseUrl, type, size } ) ;
292
284
} ;
293
285
294
- return { initImageStore, getImageUrl, getImageResponse , imageSizes, clearState, buildImageUrl } ;
286
+ return { initImageStore, getImageUrl, imageSizes, clearState, buildImageUrl } ;
295
287
} ) ;
296
288
297
289
export const useImageStoreRefs = ( ) => storeToRefs ( useImageStore ( ) ) ;
0 commit comments