Skip to content

Commit

Permalink
Merge pull request #406 from poap-xyz/release/v1.17.0
Browse files Browse the repository at this point in the history
Release v1.17.0
  • Loading branch information
jm42 authored Nov 24, 2024
2 parents 0748e58 + c92bd6d commit 27a9752
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 289 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/poap-family",
"version": "1.16.3",
"version": "1.17.0",
"author": {
"name": "POAP",
"url": "https://poap.xyz"
Expand Down
6 changes: 1 addition & 5 deletions src/components/Status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ import 'styles/status.css'

function Status({
loading,
caching,
error,
}: {
loading?: boolean
caching?: boolean
error?: boolean
}) {
if (!loading && !caching && !error) {
if (!loading && !error) {
return null
}
return (
<div
className={clsx('status',
loading && 'status-loading',
caching && 'status-caching',
error && 'status-error',
)}
>
{loading && 'Loading'}
{caching && 'Caching'}
{error && 'Error'}
</div>
)
Expand Down
128 changes: 70 additions & 58 deletions src/hooks/useEventInCommon.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import { useCallback, useEffect, useState } from 'react'
import { AbortedError } from 'models/error'
import { filterInCommon } from 'models/in-common'
import { Drop } from 'models/drop'
import { POAP } from 'models/poap'
import { Progress } from 'models/http'
import { CountProgress, DownloadProgress } from 'models/http'
import { InCommon } from 'models/api'
import { getInCommonEventsWithProgress, putEventInCommon } from 'loaders/api'
import { filterInCommon } from 'models/in-common'
import { getInCommonEventsWithEvents, getInCommonEventsWithProgress } from 'loaders/api'
import { scanAddress } from 'loaders/poap'

function useEventInCommon(eventId: number, owners: string[], force: boolean = false): {
function useEventInCommon(
eventId: number,
owners: string[],
force: boolean = false,
local: boolean = false,
stream: boolean = false,
): {
completedEventInCommon: boolean
loadingEventInCommon: boolean
loadedInCommonProgress: { progress: number; estimated: number | null; rate: number | null } | null
loadedInCommon: CountProgress | null
loadedInCommonDownload: DownloadProgress | null
loadedOwners: number
ownersErrors: Array<{ address: string; error: Error }>
inCommon: InCommon
events: Record<number, Drop>
caching: boolean
cachingError: Error | null
cachedTs: number | null
fetchEventInCommon: () => () => void
retryAddress: (address: string) => void
} {
const [completed, setCompleted] = useState<boolean>(false)
const [loading, setLoading] = useState<boolean>(false)
const [loadedProgress, setLoadedProgress] = useState<Progress | null>(null)
const [loadedInCommon, setLoadedInCommon] = useState<CountProgress | null>(null)
const [loadedProgress, setLoadedProgress] = useState<DownloadProgress | null>(null)
const [loadedOwners, setLoadedOwners] = useState<number>(0)
const [errors, setErrors] = useState<Array<{ address: string; error: Error }>>([])
const [inCommon, setInCommon] = useState<InCommon>({})
const [events, setEvents] = useState<Record<number, Drop>>({})
const [caching, setCaching] = useState<boolean>(false)
const [cachingError, setCachingError] = useState<Error | null>(null)
const [cachedTs, setCachedTs] = useState<number | null>(null)

useEffect(
Expand All @@ -39,22 +43,7 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
const inCommonProcessed = filterInCommon(inCommon)

if (Object.keys(inCommonProcessed).length > 0) {
setCaching(true)
setCachingError(null)
putEventInCommon(eventId, inCommonProcessed).then(
() => {
setCaching(false)
setCachedTs(Math.trunc(Date.now() / 1000))
},
(err) => {
console.error(err)
setCaching(false)
setCachingError(new Error(
'Could not cache drop',
{ cause: err }
))
}
)
setCachedTs(Math.trunc(Date.now() / 1000))
}
}
},
Expand All @@ -72,6 +61,21 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
setErrors((prevErrors) => [...prevErrors, { address, error }])
}

function removeError(address: string): void {
setErrors((prevErrors) => {
if (prevErrors == null) {
return []
}
const newErrors = []
for (const { error, address: errorAddress } of prevErrors) {
if (errorAddress !== address) {
newErrors.push({ error, address: errorAddress })
}
}
return newErrors
})
}

const fetchAddressInCommon = useCallback(
async (address: string, abortSignal: AbortSignal) => {
let tokens: POAP[]
Expand Down Expand Up @@ -136,30 +140,46 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
{}
)
setCompleted(false)
if (force) {
if (local) {
fetchOwnersInCommon(controllers).finally(() => {
setCompleted(true)
})
} else {
setLoading(true)
setLoadedOwners(0)
setLoadedInCommon(null)
setLoadedProgress(null)
getInCommonEventsWithProgress(
eventId,
/*abortSignal*/undefined,
/*onProgress*/({ progress, estimated, rate }) => {
if (progress != null) {
setLoadedProgress({
progress,
estimated: estimated ?? null,
rate: rate ?? null,
})
} else {
setLoadedProgress(null)
}
}
;(
stream
? getInCommonEventsWithEvents(
eventId,
/*refresh*/force,
/*onProgress*/(received, total) => {
setLoadedInCommon({
count: received,
total,
})
},
)
: getInCommonEventsWithProgress(
eventId,
/*abortSignal*/undefined,
/*onProgress*/({ progress, estimated, rate }) => {
if (progress != null) {
setLoadedProgress({
progress,
estimated: estimated ?? null,
rate: rate ?? null,
})
} else {
setLoadedProgress(null)
}
},
/*refresh*/force
)
).then(
(result) => {
setLoadedInCommon(null)
setLoadedProgress(null)
if (!result) {
return fetchOwnersInCommon(controllers)
Expand All @@ -173,6 +193,7 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
setCachedTs(result.ts)
},
(err) => {
setLoadedInCommon(null)
setLoadedProgress(null)
console.error(err)
return fetchOwnersInCommon(controllers)
Expand All @@ -187,30 +208,22 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
}
setCompleted(false)
setLoading(false)
setLoadedInCommon(null)
setLoadedProgress(null)
setLoadedOwners(0)
setErrors([])
setInCommon({})
}
},
[eventId, owners, force, fetchOwnersInCommon]
[eventId, owners, force, local, stream, fetchOwnersInCommon]
)

function retryAddress(address: string): () => void {
setErrors((prevErrors) => {
if (prevErrors == null) {
return []
}
const newErrors = []
for (const { error, address: errorAddress } of prevErrors) {
if (errorAddress !== address) {
newErrors.push({ error, address: errorAddress })
}
}
return newErrors
})
removeError(address)
const controller = new AbortController()
fetchAddressInCommon(address, controller.signal)
fetchAddressInCommon(address, controller.signal).catch((err) => {
addError(address, err)
})
return () => {
controller.abort()
}
Expand All @@ -219,13 +232,12 @@ function useEventInCommon(eventId: number, owners: string[], force: boolean = fa
return {
completedEventInCommon: completed,
loadingEventInCommon: loading,
loadedInCommonProgress: loadedProgress,
loadedInCommon,
loadedInCommonDownload: loadedProgress,
loadedOwners,
ownersErrors: errors,
inCommon,
events,
caching,
cachingError,
cachedTs,
fetchEventInCommon,
retryAddress,
Expand Down
Loading

0 comments on commit 27a9752

Please sign in to comment.