Skip to content

Commit

Permalink
refactor: types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Dec 7, 2023
1 parent a3a648e commit db5d692
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 533 deletions.
4 changes: 2 additions & 2 deletions apps/hostd/contexts/config/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('data transforms', () => {
ipv4: false,
ipv6: false,
options: {
ID: 'ID',
id: 'ID',
secret: 'secret',
zoneID: 'zone',
},
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('data transforms', () => {
ipv4: false,
ipv6: false,
options: {
ID: 'ID',
id: 'ID',
secret: 'secret',
zoneID: 'zone',
},
Expand Down
4 changes: 2 additions & 2 deletions apps/hostd/contexts/config/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function transformUp(
// DNS AWS
if (values.dnsProvider === 'route53') {
dnsOptions = {
ID: values.dnsAwsId,
id: values.dnsAwsId,
secret: values.dnsAwsSecret,
zoneID: values.dnsAwsZoneId,
} as DNSAWSOptions
Expand Down Expand Up @@ -144,7 +144,7 @@ export function transformDown(s: HostSettings): SettingsData {
// DNS AWS
if (s.ddns.provider === 'route53') {
dnsOptions = {
dnsAwsId: s.ddns.options['ID'],
dnsAwsId: s.ddns.options['id'],
dnsAwsSecret: s.ddns.options['secret'],
dnsAwsZoneId: s.ddns.options['zoneID'],
}
Expand Down
41 changes: 24 additions & 17 deletions apps/hostd/contexts/transactions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,26 @@ import { useDialog } from '../dialog'
import BigNumber from 'bignumber.js'
import { useRouter } from 'next/router'
import { useSiascanUrl } from '../../hooks/useSiascanUrl'
import { Transaction } from '@siafoundation/react-core'

const defaultLimit = 50
const filters = []

export type TransactionDataPending = {
type: 'transaction'
txType: TxType
siascanUrl: string
}

export type TransactionDataConfirmed = {
export type TransactionData = {
id: string
type: 'transaction'
txType: TxType
hash: string
inflow: string
outflow: string
timestamp: number
onClick: () => void
unconfirmed: boolean
unconfirmed?: boolean
sc: BigNumber
siascanUrl: string
raw: Transaction
}

export type TransactionData = TransactionDataPending | TransactionDataConfirmed

function useTransactionsMain() {
const router = useRouter()
const limit = Number(router.query.limit || defaultLimit)
Expand Down Expand Up @@ -72,29 +69,39 @@ function useTransactionsMain() {
...(pending.data || []).map((t): TransactionData => {
const notRealTxn = t.source !== 'transaction'
return {
id: t.id,
type: 'transaction',
unconfirmed: true,
txType: getTransactionType(t.transaction, t.source),
hash: t.ID,
timestamp: new Date(t.timestamp).getTime(),
hash: t.id,
inflow: t.inflow,
outflow: t.outflow,
sc: new BigNumber(t.inflow).minus(t.outflow),
unconfirmed: true,
siascanUrl: notRealTxn ? undefined : siascanUrl,
timestamp: new Date(t.timestamp).getTime(),
onClick: () => openDialog('transactionDetails', t.id),
raw: t.transaction,
}
}),
...(transactions.data || [])
.map((t): TransactionData => {
const notRealTxn = t.source !== 'transaction'
return {
id: t.id,
type: 'transaction',
unconfirmed: false,
txType: getTransactionType(t.transaction, t.source),
hash: t.ID,
timestamp: new Date(t.timestamp).getTime(),
onClick: () => openDialog('transactionDetails', t.ID),
hash: t.id,
inflow: t.inflow,
outflow: t.outflow,
sc: new BigNumber(t.inflow).minus(t.outflow),
siascanUrl: notRealTxn ? undefined : siascanUrl,
timestamp: new Date(t.timestamp).getTime(),
onClick: () => openDialog('transactionDetails', t.id),
raw: t.transaction,
}
})
.sort((a, b) => (a['timestamp'] < b['timestamp'] ? 1 : -1)),
.sort((a, b) => (a.timestamp < b.timestamp ? 1 : -1)),
]
}, [pending, transactions, openDialog, siascanUrl])

Expand Down
3 changes: 1 addition & 2 deletions apps/hostd/contexts/volumes/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export function useDataset({

function getFields(c: VolumeMeta): VolumeData {
return {
id: String(c.ID),
ID: c.ID,
id: String(c.id),
localPath: c.localPath,
usedSectors: c.usedSectors,
usedBytes: new BigNumber(c.usedSectors).times(MiBToBytes(4)).toNumber(),
Expand Down
1 change: 0 additions & 1 deletion apps/hostd/contexts/volumes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import BigNumber from 'bignumber.js'

export type VolumeData = {
id: string
ID: number
localPath: string
usedSectors: number
totalSectors: number
Expand Down
33 changes: 5 additions & 28 deletions apps/hostd/dialogs/HostdTransactionDetailsDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
import { useMemo } from 'react'
import {
getTransactionType,
TransactionDetailsDialog,
} from '@siafoundation/design-system'
import { useWalletTransactions } from '@siafoundation/react-hostd'
import { TransactionDetailsDialog } from '@siafoundation/design-system'
import { useDialog } from '../contexts/dialog'
import { useTransactions } from '../contexts/transactions'

export function HostdTransactionDetailsDialog() {
const { id, dialog, openDialog, closeDialog } = useDialog()
const { dataset } = useTransactions()

// TODO: add transaction endpoint
const transactions = useWalletTransactions({
params: {},
config: {
swr: {
revalidateOnFocus: false,
refreshInterval: 60_000,
},
},
disabled: dialog !== 'transactionDetails',
})
const transaction = useMemo(() => {
const txn = transactions.data?.find((t) => t.ID === id)
if (!txn) {
return null
}
return {
txType: getTransactionType(txn.transaction, txn.source),
inflow: txn.inflow,
outflow: txn.outflow,
timestamp: txn.timestamp,
raw: txn.transaction,
}
}, [transactions, id])
return dataset?.find((t) => t.id === id)
}, [dataset, id])

return (
<TransactionDetailsDialog
Expand Down
2 changes: 1 addition & 1 deletion apps/hostd/dialogs/VolumeDeleteDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function VolumeDeleteDialog({ trigger, open, onOpenChange }: Props) {
async (values: typeof defaultValues) => {
const response = await volumeDelete.delete({
params: {
id: volume.data?.ID,
id: volume.data?.id,
force: values.force,
},
})
Expand Down
4 changes: 2 additions & 2 deletions apps/renterd/contexts/transactions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ function useTransactionsMain() {
return {
type: 'transaction',
txType: getTransactionType(t),
// hash: t.ID,
// hash: t.id,
// timestamp: new Date(t.Timestamp).getTime(),
// onClick: () => openDialog('transactionDetails', t.ID),
// onClick: () => openDialog('transactionDetails', t.id),
// sc: totals.sc,
unconfirmed: true,
siascanUrl,
Expand Down
68 changes: 38 additions & 30 deletions libs/design-system/src/app/TransactionDetailsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type Props = {
id: string
transaction?: {
txType: TxType
inflow: string
outflow: string
timestamp: string | number
raw: Transaction
inflow?: string
outflow?: string
timestamp?: string | number
raw?: Transaction
}
trigger?: React.ReactNode
open: boolean
Expand Down Expand Up @@ -49,36 +49,44 @@ export function TransactionDetailsDialog({
{transaction ? (
<div className="flex flex-col gap-4 pb-10 w-full overflow-hidden">
<div className="flex flex-wrap gap-4">
<div className="flex items-baseline gap-2">
<Text>Inflow</Text>
<ValueSc value={new BigNumber(transaction?.inflow || 0)} />
</div>
<div className="flex items-baseline gap-2">
<Text>Outflow</Text>
<ValueSc
value={new BigNumber(transaction?.outflow || 0).negated()}
/>
</div>
<div className="flex items-baseline gap-2">
<Text>Miner fee</Text>
<ValueSc
value={
new BigNumber(
transaction?.raw?.minerFees?.reduce(
(acc, val) => acc.plus(val),
new BigNumber(0)
) || 0
)
}
/>
</div>
{transaction?.inflow !== undefined && (
<div className="flex items-baseline gap-2">
<Text>Inflow</Text>
<ValueSc value={new BigNumber(transaction?.inflow || 0)} />
</div>
)}
{transaction?.outflow !== undefined && (
<div className="flex items-baseline gap-2">
<Text>Outflow</Text>
<ValueSc
value={new BigNumber(transaction?.outflow || 0).negated()}
/>
</div>
)}
{transaction?.raw?.minerFees !== undefined && (
<div className="flex items-baseline gap-2">
<Text>Miner fee</Text>
<ValueSc
value={
new BigNumber(
transaction?.raw.minerFees?.reduce(
(acc, val) => acc.plus(val),
new BigNumber(0)
) || 0
)
}
/>
</div>
)}
<div className="flex-1" />
<div className="flex items-baseline gap-2">
<Text>Timestamp</Text>
<Text>
{humanDate(transaction?.timestamp || 0, {
timeStyle: 'short',
})}
{transaction?.timestamp
? humanDate(transaction?.timestamp || 0, {
timeStyle: 'short',
})
: 'Unconfirmed'}
</Text>
</div>
</div>
Expand Down
Loading

0 comments on commit db5d692

Please sign in to comment.