File "useUnsplashImages.js"
Full path: /home/disqkgca/aqanet.org/wp-content/plugins/extendify/src/Draft/hooks/useUnsplashImages.js
File size: 575 B
MIME-type: text/x-java
Charset: utf-8
import { fetchImages } from '@shared/lib/unsplash';
import { useUnsplashCacheStore } from '@shared/state/unsplash-cache';
import useSWRImmutable from 'swr/immutable';
const searchImages = async (search) => {
const cache = useUnsplashCacheStore.getState();
if (search === 'unsplash' && !cache.isEmpty() && !cache.hasExpired()) {
return cache.images;
}
return await fetchImages(search);
};
export const useUnsplashImages = (search) => {
const { data, error } = useSWRImmutable(search || 'unsplash', searchImages);
return { data, error, loading: !data && !error };
};