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