blowfish/src/renderer/store/AppProvider.jsx

111 lines
2.7 KiB
React
Raw Normal View History

2020-02-25 04:10:06 +01:00
import React, { useContext, useState, useEffect } from 'react'
2019-05-05 13:34:21 +02:00
import PropTypes from 'prop-types'
import ms from 'ms'
2020-10-10 15:37:05 +02:00
import electron from 'electron'
2020-02-25 04:10:06 +01:00
import { AppContext, PriceContext } from './createContext'
2020-02-25 16:46:04 +01:00
import { refreshInterval, conversions } from '../../config'
import { getAccounts, getBalance } from './helpers'
2019-05-05 13:34:21 +02:00
2020-10-10 15:37:05 +02:00
const ipcRenderer = electron.ipcRenderer || false
2020-02-25 04:10:06 +01:00
export default function AppProvider({ children }) {
const { prices } = useContext(PriceContext)
const [isLoading, setIsLoading] = useState(true)
const [accounts, setAccounts] = useState([])
const [needsConfig, setNeedsConfig] = useState(false)
const [currency, setCurrency] = useState('ocean')
const [accentColor, setAccentColor] = useState('#f6388a')
2020-02-25 15:16:44 +01:00
const [error, setError] = useState()
2019-05-05 13:34:21 +02:00
2020-10-10 15:37:05 +02:00
function toggleCurrencies(currency) {
setCurrency(currency)
const pricesNew = Array.from(prices)
ipcRenderer && ipcRenderer.send('currency-updated', pricesNew, currency)
}
// listener for accent color & touchbar
2020-02-25 04:10:06 +01:00
useEffect(() => {
2020-10-10 15:37:05 +02:00
if (!ipcRenderer) return
ipcRenderer.on('accent-color', (evt, accentColor) => {
setAccentColor(accentColor)
})
ipcRenderer.on('setCurrency', (evt, currency) => toggleCurrencies(currency))
return () => {
ipcRenderer.removeAllListeners('accent-color')
ipcRenderer.removeAllListeners('setCurrency')
2020-02-25 15:16:44 +01:00
}
2020-02-25 04:10:06 +01:00
}, [])
2020-02-25 04:10:06 +01:00
useEffect(() => {
2020-03-22 23:51:54 +01:00
if (!prices) return
2020-02-25 04:10:06 +01:00
async function init() {
2020-02-25 15:16:44 +01:00
try {
await setBalances()
2020-03-22 23:51:54 +01:00
console.log('Updated balance')
2020-02-25 15:16:44 +01:00
setIsLoading(false)
} catch (error) {
console.error(error.message)
setError(error.message)
}
2020-02-25 04:10:06 +01:00
}
2019-05-06 18:16:30 +02:00
2020-02-25 04:10:06 +01:00
init()
setInterval(init, ms(refreshInterval))
2019-05-07 00:25:31 +02:00
2020-02-25 04:10:06 +01:00
return () => {
clearInterval(init)
}
}, [prices])
2019-05-05 13:34:21 +02:00
2020-02-25 04:10:06 +01:00
async function setBalances() {
let newAccounts = []
2020-02-25 16:46:04 +01:00
const { needsConfig, accountsPref } = await getAccounts()
setNeedsConfig(needsConfig)
2019-05-08 01:02:02 +02:00
for (const account of accountsPref) {
2020-02-25 04:10:06 +01:00
const oceanBalance = await getBalance(account)
2019-05-05 13:34:21 +02:00
2019-05-25 02:33:54 +02:00
const conversionsBalance = Object.assign(
2020-03-22 00:49:10 +01:00
...conversions.map((key) => ({
2020-02-25 04:10:06 +01:00
[key]: oceanBalance * prices.get(key) || 0
2019-05-06 18:16:30 +02:00
}))
)
2019-05-05 13:34:21 +02:00
const newAccount = {
address: account,
balance: {
ocean: oceanBalance,
2019-05-25 02:33:54 +02:00
...conversionsBalance
2019-05-05 13:34:21 +02:00
}
}
newAccounts.push(newAccount)
}
2020-02-25 04:10:06 +01:00
if (newAccounts !== accounts) {
setAccounts(newAccounts)
}
2019-05-05 13:34:21 +02:00
}
2020-02-25 04:10:06 +01:00
const context = {
isLoading,
accounts,
currency,
needsConfig,
accentColor,
2020-02-25 15:16:44 +01:00
error,
toggleCurrencies,
setBalances
2019-05-05 13:34:21 +02:00
}
2020-02-25 04:10:06 +01:00
return <AppContext.Provider value={context}>{children}</AppContext.Provider>
}
AppProvider.propTypes = {
children: PropTypes.any.isRequired
2019-05-05 13:34:21 +02:00
}