Skip to content

Commit

Permalink
feat(ad): add local storage for fetching billboard content
Browse files Browse the repository at this point in the history
  • Loading branch information
zeckli authored and gitwoz committed Oct 14, 2024
1 parent a8d9496 commit 92366f9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/common/enums/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export const STORAGE_KEY_PUSH = '__PUSH'

export const STORAGE_KEY_AGENT_HASH = '__AGENT_HASH'

export const STORAGE_KEY_BILLBOARD = '__BILLBOARD'

export const STORAGE_KEY_CIRCLE_BANNER = '__CIRCLE_BANNER'

export const STORAGE_KEY_SEARCH_HISTORY = '__SEARCH_HISTORY'
Expand Down
2 changes: 1 addition & 1 deletion src/components/Billboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const Billboard = ({ tokenId, type }: BillboardProps) => {
})
}
>
<img src={data.contentURI} alt="ad" />
<img src={data.contentURI} alt="ad" loading="lazy" />
</a>

<button
Expand Down
51 changes: 37 additions & 14 deletions src/components/Hook/useBillboard.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { readContract } from '@wagmi/core'
import { useEffect, useState } from 'react'

import { BillboardOperatorABI, BillboardRegistryABI } from '~/common/utils'
import { STORAGE_KEY_BILLBOARD } from '~/common/enums'
import {
BillboardOperatorABI,
BillboardRegistryABI,
storage,
} from '~/common/utils'

// custom hook level enums
enum BillboardQueryStatus {
enum QueryStatus {
IDLE = 'idle',
LOADING = 'loading',
LOADED = 'loaded',
Expand All @@ -24,22 +29,35 @@ export const useBillboard = ({
operatorAddress,
registryAddress,
}: Props) => {
const [status, setStatus] = useState<BillboardQueryStatus>(
BillboardQueryStatus.IDLE
)
const [data, setData] = useState<Record<string, any>>({})
const [status, setStatus] = useState<QueryStatus>(QueryStatus.IDLE)

const isLoading = status === BillboardQueryStatus.LOADING
const isError = status === BillboardQueryStatus.ERROR
const data = storage.get(STORAGE_KEY_BILLBOARD) as Record<string, any>
const ttl = 3 * 60 * 1000

const isLoading = status === QueryStatus.LOADING
const isError = status === QueryStatus.ERROR

const resetData = () => {
storage.set(STORAGE_KEY_BILLBOARD, {
contentURI: '',
redirectURI: '',
expired: Date.now() + ttl,
})
}

// fetch board cotent if data is expired
useEffect(() => {
if (isLoading) {
return
}

if (data?.expired >= Date.now()) {
return
}

;(async () => {
try {
setStatus(BillboardQueryStatus.LOADING)
setStatus(QueryStatus.LOADING)

const tokenId = BigInt(id)
const currEpoch = await readContract({
Expand Down Expand Up @@ -71,15 +89,20 @@ export const useBillboard = ({
})

if (bid && bid.isWon) {
setData({
...(bid.contentURI ? { contentURI: bid.contentURI } : {}),
...(bid.redirectURI ? { redirectURI: bid.redirectURI } : {}),
storage.set(STORAGE_KEY_BILLBOARD, {
contentURI: bid.contentURI,
redirectURI: bid.redirectURI,
expired: Date.now() + ttl,
})
} else {
// if no running ad or it hasn't been cleared yet
resetData()
}

setStatus(BillboardQueryStatus.LOADED)
setStatus(QueryStatus.LOADED)
} catch (error) {
setStatus(BillboardQueryStatus.ERROR)
resetData()
setStatus(QueryStatus.ERROR)
}
})()
}, [])
Expand Down

0 comments on commit 92366f9

Please sign in to comment.