Skip to content
Discussion options

You must be logged in to vote
import React from 'react'
import useAsyncEffect from '@n1ru4l/use-async-effect'

const useCachedImage = ({id, imageUrl}) => {
  const [blob, setBlob] = React.useState()
  const [error, setError] = React.useState()

  useAsyncEffect(
    function* (_, c) {
      if (!id || !imageUrl) return

      try {
        const cacheRecord = yield* c(
          idb.get(id)
        )

        const cacheStatus =
          !cacheRecord
            ? CACHE_MISS
            : isFresh(cacheRecord.t)
            ? CACHE_FRESH
            : CACHE_STALE

        if (cacheStatus !== CACHE_MISS) {
          setBlob(cacheRecord.blob)
        }

        if (cacheStatus === CACHE_FRESH) return

        const resp…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by llaenowyd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant