Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch balance query in batch #77

Merged
merged 3 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/components/currency.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,12 @@ const AssetAmount: NextPage<{
decimals: number
symbol: string
className?: string
}> = ({ quantity, decimals, symbol, className }) => (
<NumberFormat
className={className}
value={toDecimal(quantity, decimals)}
decimalSeparator='.'
isNumericString={true}
thousandSeparator={false}
allowNegative={false}
decimalScale={decimals}
suffix={` ${symbol}`}
displayType='text' />
)
}> = ({ quantity, decimals, symbol, className }) => {
const value = toDecimal(quantity, decimals).replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/, "$1")
return (
<span className={className}>{`${value} ${symbol}`}</span>
)
}

const ADAAmount: NextPage<{
lovelace: bigint
Expand Down
48 changes: 42 additions & 6 deletions src/components/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { db, Treasury } from '../db'
import { useRouter } from 'next/router'
import Image from 'next/image'
import { getTreasuriesPath } from '../route'
import { encodeCardanoData } from '../cardano/multiplatform-lib'
import { Cardano, encodeCardanoData, useCardanoMultiplatformLib } from '../cardano/multiplatform-lib'
import { getBalanceByPaymentAddresses, usePaymentAddressesQuery } from '../cardano/query-api'
import type { Value } from '../cardano/query-api'
import { ADAAmount } from './currency'

const Toggle: NextPage<{
isOn: boolean
Expand Down Expand Up @@ -143,34 +146,67 @@ const PrimaryBar: NextPage = () => {

const TreasuryListing: NextPage<{
treasury: Treasury
}> = ({ treasury }) => {
balance?: Value
}> = ({ treasury, balance }) => {
const { name, script } = treasury
const base64CBOR = encodeCardanoData(script, 'base64')
const lovelace = balance?.lovelace
return (
<NavLink
href={getTreasuriesPath(encodeURIComponent(base64CBOR))}
onPageClassName='bg-sky-700 font-semibold'
className='block w-full p-4 truncate hover:bg-sky-700'>
{name}
<div>{name}</div>
{lovelace && <div className='text-sm font-normal'><ADAAmount lovelace={lovelace} /></div>}
</NavLink>
)
}

const TreasuryList: NextPage<{
cardano: Cardano
treasuries: Treasury[]
}> = ({ cardano, treasuries }) => {
const [config, _] = useContext(ConfigContext)
const addresses = cardano && treasuries && treasuries.map((treasury) => {
const script = cardano.lib.NativeScript.from_bytes(treasury.script)
return cardano.getScriptAddress(script, config.isMainnet).to_bech32()
})
const { data } = usePaymentAddressesQuery({
variables: { addresses },
fetchPolicy: 'cache-first',
pollInterval: 10000
})
const balanceMap = new Map<string, Value>()
data?.paymentAddresses.forEach((paymentAddress) => {
const address = paymentAddress.address
const balance = getBalanceByPaymentAddresses([paymentAddress])
balanceMap.set(address, balance)
})
const balances = (addresses ?? []).map((address) => balanceMap.get(address))

return (
<nav className='block w-full'>
{treasuries.map((treasury, index) => <TreasuryListing key={index} treasury={treasury} balance={balances[index]} />)}
</nav>
)
}

const SecondaryBar: NextPage = () => {
const treasuries = useLiveQuery(async () => db.treasuries.toArray())
const cardano = useCardanoMultiplatformLib()

return (
<aside className='flex flex-col w-60 bg-sky-800 items-center text-white overflow-y-auto'>
<div className='w-full bg-sky-900 font-semibold'>
<nav className='w-full bg-sky-900 font-semibold'>
<NavLink
href='/treasuries/new'
onPageClassName='bg-sky-700'
className='flex w-full p-4 items-center space-x-1 justify-center hover:bg-sky-700'>
<PlusIcon className='w-4' />
<span>New Treasury</span>
</NavLink>
</div>
{treasuries && treasuries.map((treasury, index) => <TreasuryListing key={index} treasury={treasury} />)}
</nav>
{cardano && treasuries && <TreasuryList cardano={cardano} treasuries={treasuries} />}
</aside>
)
}
Expand Down
6 changes: 5 additions & 1 deletion src/pages/treasuries/[base64CBOR]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ const ShowBalance: NextPage<{
}> = ({ cardano, script, className }) => {
const [config, _] = useContext(ConfigContext)
const address = cardano.getScriptAddress(script, config.isMainnet).to_bech32()
const { loading, error, data } = usePaymentAddressesQuery({ variables: { addresses: [address] }, fetchPolicy: 'network-only' })
const { loading, error, data } = usePaymentAddressesQuery({
variables: { addresses: [address] },
fetchPolicy: 'cache-first',
pollInterval: 2000
})

if (loading) return null
if (error) return null
Expand Down