fix: bee data auto-refresh (#436)
This commit is contained in:
@@ -6,17 +6,24 @@ import { Waiting } from '../../components/Waiting'
|
|||||||
import { Context } from '../../providers/Bee'
|
import { Context } from '../../providers/Bee'
|
||||||
import { ROUTES } from '../../routes'
|
import { ROUTES } from '../../routes'
|
||||||
|
|
||||||
export default function Settings(): ReactElement {
|
const STARTED_UPGRADE_AT = 'started-upgrade-at'
|
||||||
const [startedAt] = useState(Date.now())
|
|
||||||
|
export default function LightModeRestart(): ReactElement {
|
||||||
|
const [startedAt] = useState(Number.parseInt(localStorage.getItem(STARTED_UPGRADE_AT) ?? Date.now().toFixed()))
|
||||||
const { apiHealth, nodeInfo } = useContext(Context)
|
const { apiHealth, nodeInfo } = useContext(Context)
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
localStorage.setItem(STARTED_UPGRADE_AT, startedAt.toFixed())
|
||||||
|
}, [startedAt])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (Date.now() - startedAt < 45_000) {
|
if (Date.now() - startedAt < 45_000) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (apiHealth && nodeInfo?.beeMode === BeeModes.LIGHT) {
|
if (apiHealth && nodeInfo?.beeMode === BeeModes.LIGHT) {
|
||||||
|
localStorage.removeItem(STARTED_UPGRADE_AT)
|
||||||
navigate(ROUTES.INFO)
|
navigate(ROUTES.INFO)
|
||||||
}
|
}
|
||||||
}, [startedAt, navigate, nodeInfo, apiHealth])
|
}, [startedAt, navigate, nodeInfo, apiHealth])
|
||||||
|
|||||||
@@ -66,7 +66,6 @@ interface ContextInterface {
|
|||||||
chainState: ChainState | null
|
chainState: ChainState | null
|
||||||
latestBeeRelease: LatestBeeRelease | null
|
latestBeeRelease: LatestBeeRelease | null
|
||||||
isLoading: boolean
|
isLoading: boolean
|
||||||
isRefreshing: boolean
|
|
||||||
lastUpdate: number | null
|
lastUpdate: number | null
|
||||||
start: (frequency?: number) => void
|
start: (frequency?: number) => void
|
||||||
stop: () => void
|
stop: () => void
|
||||||
@@ -104,7 +103,6 @@ const initialValues: ContextInterface = {
|
|||||||
chainState: null,
|
chainState: null,
|
||||||
latestBeeRelease: null,
|
latestBeeRelease: null,
|
||||||
isLoading: true,
|
isLoading: true,
|
||||||
isRefreshing: false,
|
|
||||||
lastUpdate: null,
|
lastUpdate: null,
|
||||||
start: () => {}, // eslint-disable-line
|
start: () => {}, // eslint-disable-line
|
||||||
stop: () => {}, // eslint-disable-line
|
stop: () => {}, // eslint-disable-line
|
||||||
@@ -186,6 +184,9 @@ function getStatus(
|
|||||||
return status
|
return status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This does not need to be exposed and works much better as variable than state variable which may trigger some unnecessary re-renders
|
||||||
|
let isRefreshing = false
|
||||||
|
|
||||||
export function Provider({ children }: Props): ReactElement {
|
export function Provider({ children }: Props): ReactElement {
|
||||||
const { beeApi, beeDebugApi } = useContext(SettingsContext)
|
const { beeApi, beeDebugApi } = useContext(SettingsContext)
|
||||||
const { provider } = useContext(TopUpContext)
|
const { provider } = useContext(TopUpContext)
|
||||||
@@ -207,7 +208,6 @@ export function Provider({ children }: Props): ReactElement {
|
|||||||
|
|
||||||
const [error, setError] = useState<Error | null>(initialValues.error)
|
const [error, setError] = useState<Error | null>(initialValues.error)
|
||||||
const [isLoading, setIsLoading] = useState<boolean>(initialValues.isLoading)
|
const [isLoading, setIsLoading] = useState<boolean>(initialValues.isLoading)
|
||||||
const [isRefreshing, setIsRefreshing] = useState<boolean>(initialValues.isRefreshing)
|
|
||||||
const [lastUpdate, setLastUpdate] = useState<number | null>(initialValues.lastUpdate)
|
const [lastUpdate, setLastUpdate] = useState<number | null>(initialValues.lastUpdate)
|
||||||
const [frequency, setFrequency] = useState<number | null>(30000)
|
const [frequency, setFrequency] = useState<number | null>(30000)
|
||||||
|
|
||||||
@@ -265,7 +265,7 @@ export function Provider({ children }: Props): ReactElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setIsRefreshing(true)
|
isRefreshing = true
|
||||||
setError(null)
|
setError(null)
|
||||||
|
|
||||||
// Wrap the chequebook balance call to return BZZ values as Token object
|
// Wrap the chequebook balance call to return BZZ values as Token object
|
||||||
@@ -374,11 +374,11 @@ export function Provider({ children }: Props): ReactElement {
|
|||||||
await Promise.allSettled(promises)
|
await Promise.allSettled(promises)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(e as Error)
|
setError(e as Error)
|
||||||
} finally {
|
|
||||||
setIsLoading(false)
|
|
||||||
setIsRefreshing(false)
|
|
||||||
setLastUpdate(Date.now())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setIsLoading(false)
|
||||||
|
isRefreshing = false
|
||||||
|
setLastUpdate(Date.now())
|
||||||
}
|
}
|
||||||
|
|
||||||
const start = (freq = REFRESH_WHEN_OK) => {
|
const start = (freq = REFRESH_WHEN_OK) => {
|
||||||
@@ -447,7 +447,6 @@ export function Provider({ children }: Props): ReactElement {
|
|||||||
chainState,
|
chainState,
|
||||||
latestBeeRelease,
|
latestBeeRelease,
|
||||||
isLoading,
|
isLoading,
|
||||||
isRefreshing,
|
|
||||||
lastUpdate,
|
lastUpdate,
|
||||||
start,
|
start,
|
||||||
stop,
|
stop,
|
||||||
|
|||||||
Reference in New Issue
Block a user