|
|
@ -6,12 +6,12 @@ import ApiContext from './ApiContext'
|
|
|
|
import PhotoCredit from './PhotoCredit'
|
|
|
|
import PhotoCredit from './PhotoCredit'
|
|
|
|
|
|
|
|
|
|
|
|
function RandomImage(props) {
|
|
|
|
function RandomImage(props) {
|
|
|
|
const { current: cache } = React.useRef([])
|
|
|
|
const cacheRef = React.useRef([])
|
|
|
|
const [cacheIndex, updateIndex] = React.useState(0)
|
|
|
|
const [cacheIndex, updateIndex] = React.useState(0)
|
|
|
|
const api = React.useContext(ApiContext)
|
|
|
|
const api = React.useContext(ApiContext)
|
|
|
|
|
|
|
|
|
|
|
|
const [selectImage, { loading: selecting }] = useAsyncCallback(() => {
|
|
|
|
const [selectImage, { loading: selecting }] = useAsyncCallback(() => {
|
|
|
|
const image = cache[cacheIndex]
|
|
|
|
const image = cacheRef.current[cacheIndex]
|
|
|
|
|
|
|
|
|
|
|
|
return api.unsplash.download(image.id).then(blob => props.onChange(blob, image))
|
|
|
|
return api.unsplash.download(image.id).then(blob => props.onChange(blob, image))
|
|
|
|
})
|
|
|
|
})
|
|
|
@ -20,20 +20,23 @@ function RandomImage(props) {
|
|
|
|
api.unsplash.random
|
|
|
|
api.unsplash.random
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const needsFetch = !error && !updating && (!imgs || cacheIndex > cache.length - 2)
|
|
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!error && !updating && (!imgs || cacheIndex > cache.length - 2)) {
|
|
|
|
if (needsFetch) {
|
|
|
|
updateCache()
|
|
|
|
updateCache()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [error, updating, imgs, cacheIndex, cache.length, updateCache])
|
|
|
|
}, [needsFetch, updateCache])
|
|
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (imgs) {
|
|
|
|
if (imgs) {
|
|
|
|
cache.push(...imgs)
|
|
|
|
cacheRef.current.push(...imgs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [cache, imgs])
|
|
|
|
}, [imgs])
|
|
|
|
|
|
|
|
|
|
|
|
const loading = updating || selecting
|
|
|
|
const loading = updating || selecting
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const cache = cacheRef.current
|
|
|
|
const photographer = cache[cacheIndex] && cache[cacheIndex].photographer
|
|
|
|
const photographer = cache[cacheIndex] && cache[cacheIndex].photographer
|
|
|
|
const bgImage = cache[cacheIndex] && cache[cacheIndex].dataURL
|
|
|
|
const bgImage = cache[cacheIndex] && cache[cacheIndex].dataURL
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|