|
| 1 | +// Copyright (c) 2026 IOTA Stiftung |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { DisplayStats, TooltipPosition } from '@iota/apps-ui-kit'; |
| 5 | +import { formatDate, useFormatCoin } from '@iota/core'; |
| 6 | +import { type IotaObjectData } from '@iota/iota-sdk/client'; |
| 7 | +import { CoinFormat, formatDigest } from '@iota/iota-sdk/utils'; |
| 8 | +import clsx from 'clsx'; |
| 9 | +import { ObjectLink, TransactionLink } from '~/components/ui'; |
| 10 | +import { onCopySuccess } from '~/lib/utils'; |
| 11 | +import { ErrorBoundary } from '~/components'; |
| 12 | +// TODO: use '@iota/audit-trail/web' after published |
| 13 | +import { type OnChainAuditTrail } from '@iota/audit-trail'; |
| 14 | + |
| 15 | +interface AuditTrailSummaryViewProps { |
| 16 | + auditTrailObject: OnChainAuditTrail; |
| 17 | + objectData: IotaObjectData; |
| 18 | +} |
| 19 | + |
| 20 | +export function AuditTrailSummaryView({ |
| 21 | + auditTrailObject, |
| 22 | + objectData, |
| 23 | +}: AuditTrailSummaryViewProps): JSX.Element { |
| 24 | + const objectId = objectData?.objectId; |
| 25 | + const storageRebate = objectData?.storageRebate; |
| 26 | + const version = `v${auditTrailObject.version}`; |
| 27 | + const sequenceNumber = `${auditTrailObject.sequenceNumber}`; |
| 28 | + |
| 29 | + const dateFormat = (timestamp: bigint): string => |
| 30 | + // Convert seconds to milliseconds for Date constructor |
| 31 | + formatDate(new Date(Number(timestamp)), ['year', 'month', 'day', 'hour', 'minute']); |
| 32 | + const createdAt = dateFormat(auditTrailObject.createdAt); |
| 33 | + const lastTransactionBlockDigest = objectData?.previousTransaction; |
| 34 | + |
| 35 | + return ( |
| 36 | + <ErrorBoundary> |
| 37 | + <div className="flex flex-col gap-md"> |
| 38 | + <div className={clsx('address-grid-container-top', 'no-image', 'no-description')}> |
| 39 | + {objectId && ( |
| 40 | + <div> |
| 41 | + <ObjectIdCard objectId={objectId} /> |
| 42 | + </div> |
| 43 | + )} |
| 44 | + |
| 45 | + {version && ( |
| 46 | + <div> |
| 47 | + <DisplayStats |
| 48 | + label="Version" |
| 49 | + value={version} |
| 50 | + tooltipPosition={TooltipPosition.Left} |
| 51 | + tooltipText="Version of object in a progressive sequence." |
| 52 | + /> |
| 53 | + </div> |
| 54 | + )} |
| 55 | + |
| 56 | + {storageRebate && ( |
| 57 | + <div> |
| 58 | + <StorageRebateCard storageRebate={storageRebate} /> |
| 59 | + </div> |
| 60 | + )} |
| 61 | + |
| 62 | + {createdAt && ( |
| 63 | + <div> |
| 64 | + <DisplayStats |
| 65 | + label="Created at" |
| 66 | + value={createdAt} |
| 67 | + tooltipPosition={TooltipPosition.Left} |
| 68 | + tooltipText="Timestamp of the transaction that first published this audit trail onchain." |
| 69 | + /> |
| 70 | + </div> |
| 71 | + )} |
| 72 | + |
| 73 | + {sequenceNumber && ( |
| 74 | + <div> |
| 75 | + <DisplayStats |
| 76 | + label="Sequence" |
| 77 | + value={sequenceNumber} |
| 78 | + tooltipPosition={TooltipPosition.Left} |
| 79 | + tooltipText="Version of state change in a progressive sequence." |
| 80 | + /> |
| 81 | + </div> |
| 82 | + )} |
| 83 | + |
| 84 | + {lastTransactionBlockDigest && ( |
| 85 | + <div> |
| 86 | + <LastTxBlockCard digest={lastTransactionBlockDigest} /> |
| 87 | + </div> |
| 88 | + )} |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + </ErrorBoundary> |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +interface ObjectIdCardProps { |
| 96 | + objectId: string; |
| 97 | +} |
| 98 | + |
| 99 | +function ObjectIdCard({ objectId }: ObjectIdCardProps): JSX.Element { |
| 100 | + return ( |
| 101 | + <DisplayStats |
| 102 | + label="Object ID" |
| 103 | + value={ |
| 104 | + <div className="flex flex-col gap-xs"> |
| 105 | + <ObjectLink objectId={objectId} copyText={objectId} /> |
| 106 | + </div> |
| 107 | + } |
| 108 | + tooltipPosition={TooltipPosition.Left} |
| 109 | + tooltipText="The unique onchain identifier of the Move object storing this audit trail's state." |
| 110 | + /> |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +interface LastTxBlockCardProps { |
| 115 | + digest: string; |
| 116 | +} |
| 117 | + |
| 118 | +function LastTxBlockCard({ digest }: LastTxBlockCardProps): JSX.Element { |
| 119 | + return ( |
| 120 | + <DisplayStats |
| 121 | + label="Last Transaction Block Digest" |
| 122 | + value={<TransactionLink digest={digest}>{formatDigest(digest)}</TransactionLink>} |
| 123 | + copyText={digest} |
| 124 | + onCopySuccess={onCopySuccess} |
| 125 | + tooltipPosition={TooltipPosition.Left} |
| 126 | + tooltipText="Hash of the most recent transaction that modified this audit trail. Use it to inspect transaction details on the explorer." |
| 127 | + /> |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +interface StorageRebateCardProps { |
| 132 | + storageRebate: string; |
| 133 | +} |
| 134 | + |
| 135 | +function StorageRebateCard({ storageRebate }: StorageRebateCardProps): JSX.Element | null { |
| 136 | + const [storageRebateFormatted, symbol] = useFormatCoin({ |
| 137 | + balance: storageRebate, |
| 138 | + format: CoinFormat.Full, |
| 139 | + }); |
| 140 | + |
| 141 | + return ( |
| 142 | + <DisplayStats |
| 143 | + label="Storage Rebate" |
| 144 | + value={`-${storageRebateFormatted}`} |
| 145 | + supportingLabel={symbol} |
| 146 | + tooltipPosition={TooltipPosition.Left} |
| 147 | + tooltipText="IOTA tokens locked as a storage deposit for this object. Partially refundable when the object is deleted or reduced in size." |
| 148 | + /> |
| 149 | + ); |
| 150 | +} |
0 commit comments