| @@ -1,4 +1,8 @@ | ||
| 1 | +import { | |
| 2 | + DEFAULT_IMAGE_SEARCH, | |
| 3 | + resolveImageSearch, | |
| 4 | +} from '@shared/lib/image-search-defaults'; | |
| 1 | 5 | import { fetchImages } from '@shared/lib/unsplash'; |
| 2 | 6 | import { useUnsplashCacheStore } from '@shared/state/unsplash-cache'; |
| 3 | 7 | import useSWRImmutable from 'swr/immutable'; |
| 4 | 8 | |
| @@ -3,15 +7,19 @@ | ||
| 3 | 7 | import useSWRImmutable from 'swr/immutable'; |
| 4 | 8 | |
| 5 | 9 | const searchImages = async (search, source = null) => { |
| 6 | 10 | const cache = useUnsplashCacheStore.getState(); |
| 7 | - if (search === 'unsplash' && !cache.isEmpty() && !cache.hasExpired()) { | |
| 11 | + if ( | |
| 12 | + search === DEFAULT_IMAGE_SEARCH && | |
| 13 | + !cache.isEmpty() && | |
| 14 | + !cache.hasExpired() | |
| 15 | + ) { | |
| 8 | 16 | return cache.images; |
| 9 | 17 | } |
| 10 | - return await fetchImages(search, source); | |
| 18 | + return await fetchImages(resolveImageSearch(search), source); | |
| 11 | 19 | }; |
| 12 | 20 | |
| 13 | 21 | export const useUnsplashImages = (search, source = null) => { |
| 14 | - const key = search || 'unsplash'; | |
| 22 | + const key = search || DEFAULT_IMAGE_SEARCH; | |
| 15 | 23 | const { data, error } = useSWRImmutable(key, () => searchImages(key, source)); |
| 16 | 24 | return { data, error, loading: !data && !error }; |
| 17 | 25 | }; |