Skip to content

Commit

Permalink
fix: Fixed price cache initialising with 0 (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
daviroo authored Jul 20, 2022
1 parent 4e3697b commit f20637b
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions app/hooks/user-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,19 @@ const MY_UPDATES_SUBSCRIPTION = gql`
const usePriceCache = (): [number, (newPrice: number) => void] => {
const client = useApolloClient()
const { initialBtcPrice } = useMainQuery()
const [cachedPrice, setCachedPrice] = React.useState(0)
const [cachedPrice, setCachedPrice] = React.useState(() => {
const lastPriceData = client.readQuery({ query: PRICE_CACHE })
if (lastPriceData) {
return lastPriceData.price
} else if (initialBtcPrice) {
client.writeQuery({
query: PRICE_CACHE,
data: { price: initialBtcPrice.formattedAmount },
})
return initialBtcPrice.formattedAmount
}
return 0
})

const updatePriceCache = React.useCallback(
(newPrice) => {
Expand Down Expand Up @@ -136,7 +148,6 @@ export const useMySubscription = (): UseMyUpdates => {
usdWalletBalance: usdWalletBalanceFromMainQuery,
} = useMainQuery()
const [cachedPrice, updatePriceCach] = usePriceCache()

const intraLedgerUpdate = React.useRef<UseMyUpdates["intraLedgerUpdate"]>(null)
const lnUpdate = React.useRef<UseMyUpdates["lnUpdate"]>(null)
const onChainUpdate = React.useRef<UseMyUpdates["onChainUpdate"]>(null)
Expand Down

0 comments on commit f20637b

Please sign in to comment.