1249c0df71
* chore: initial commit * refactor: remove unnecessary wrappers * style: add missing newline * chore: bump bee-js * chore: ignore any cast in fdp * fix: remove cid import * fix: make TextEncoder and TextDecoder available in jest * refactor: dedupe stamp ttl second conversion * refactor: use convenience methods from bee-js * feat: update to bee-js for restored ens support * fix: bump bee-js to get download fix * fix: resolve feed before downloading reference * fix: fix token displays * fix: fix token input modal error message * refactor: remove wallet balance provider * chore: remove dead code * refactor: upcoming bee js 0.15.0 (#692) * chore: initial commit * fix: do not break page when duration seconds is 0 * ci: remove cache * chore: upgrade bee-js * feat: bee-js and bee v2.6 compatibility * chore: switch upcoming/bee-js to ethersphere/bee-js
102 lines
2.6 KiB
TypeScript
102 lines
2.6 KiB
TypeScript
import { PostageBatch } from '@ethersphere/bee-js'
|
|
import { createContext, ReactChild, ReactElement, useContext, useEffect, useState } from 'react'
|
|
import { Context as SettingsContext } from './Settings'
|
|
|
|
export interface EnrichedPostageBatch extends PostageBatch {
|
|
usage: number
|
|
usageText: string
|
|
}
|
|
|
|
interface ContextInterface {
|
|
stamps: EnrichedPostageBatch[] | null
|
|
error: Error | null
|
|
isLoading: boolean
|
|
lastUpdate: number | null
|
|
start: (frequency?: number) => void
|
|
stop: () => void
|
|
refresh: () => Promise<void>
|
|
}
|
|
|
|
const initialValues: ContextInterface = {
|
|
stamps: null,
|
|
error: null,
|
|
isLoading: false,
|
|
lastUpdate: null,
|
|
start: () => {}, // eslint-disable-line
|
|
stop: () => {}, // eslint-disable-line
|
|
refresh: () => Promise.reject(),
|
|
}
|
|
|
|
export const Context = createContext<ContextInterface>(initialValues)
|
|
export const Consumer = Context.Consumer
|
|
|
|
interface Props {
|
|
children: ReactChild
|
|
}
|
|
|
|
export function enrichStamp(postageBatch: PostageBatch): EnrichedPostageBatch {
|
|
const { depth, bucketDepth, utilization } = postageBatch
|
|
|
|
const usage = utilization / Math.pow(2, depth - bucketDepth)
|
|
const usageText = `${Math.ceil(usage * 100)}%`
|
|
|
|
return {
|
|
...postageBatch,
|
|
usage,
|
|
usageText,
|
|
}
|
|
}
|
|
|
|
export function Provider({ children }: Props): ReactElement {
|
|
const { beeApi } = useContext(SettingsContext)
|
|
const [stamps, setStamps] = useState<EnrichedPostageBatch[] | null>(initialValues.stamps)
|
|
const [error, setError] = useState<Error | null>(initialValues.error)
|
|
const [isLoading, setIsLoading] = useState<boolean>(initialValues.isLoading)
|
|
const [lastUpdate, setLastUpdate] = useState<number | null>(initialValues.lastUpdate)
|
|
const [frequency, setFrequency] = useState<number | null>(null)
|
|
|
|
const refresh = async () => {
|
|
if (isLoading) {
|
|
return
|
|
}
|
|
|
|
if (!beeApi) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
setIsLoading(true)
|
|
const stamps = await beeApi.getAllPostageBatch()
|
|
|
|
setStamps(stamps.map(enrichStamp))
|
|
setLastUpdate(Date.now())
|
|
setError(null)
|
|
} catch (e) {
|
|
setError(e as Error)
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
const start = (freq = 30000) => setFrequency(freq)
|
|
const stop = () => setFrequency(null)
|
|
|
|
// Start the update loop
|
|
useEffect(() => {
|
|
refresh()
|
|
|
|
// Start autorefresh only if the frequency is set
|
|
if (frequency) {
|
|
const interval = setInterval(refresh, frequency)
|
|
|
|
return () => clearInterval(interval)
|
|
}
|
|
}, [frequency]) // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
return (
|
|
<Context.Provider value={{ stamps, error, isLoading, lastUpdate, start, stop, refresh }}>
|
|
{children}
|
|
</Context.Provider>
|
|
)
|
|
}
|