/
PerAddressContent.tsx
190 lines (172 loc) · 6.05 KB
/
PerAddressContent.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import React from 'react';
import classNames from 'classnames';
import { noop } from 'lodash';
import {
AssetChangesTabs,
TransactionAssetChangeData,
} from './TenderlyAssetChanges';
import EthereumBlockie from 'ui/views/Approval/components/TxComponents/Tenderly/EthereumBlockie/EthereumBlockie';
import { formatToLocaleString, generateShortAddress } from 'ui/utils/tenderly';
import ChronologicalContent from 'ui/views/Approval/components/TxComponents/Tenderly/AssetChanges/ChronologicalContent';
import Accordion from 'ui/component/Accordion/Accordion';
import './PerAddressContent.less';
const MinAssetChangesToDisplay = 10;
interface Props {
assetChanges: TransactionAssetChangeData[];
contracts: any[];
viewAll?: boolean;
shouldShowApproximateSign: (value: number) => boolean;
activeTabValue?: AssetChangesTabs;
onViewAllToggle?: () => void;
}
function PerAddressContent({
assetChanges,
contracts,
viewAll = false,
shouldShowApproximateSign,
onViewAllToggle = noop,
activeTabValue,
}: Props) {
function ifValueNearZero(value: number) {
return Number(value.toFixed(3)) === 0;
}
function ifValuePositive(value: number) {
return value > 0;
}
function ifValueNegative(value: number) {
return value < 0;
}
const dollarValuePerAddress: Record<string, number> = {};
const assetChangesPerAddress: Record<
string,
TransactionAssetChangeData[]
> = assetChanges.reduce(
(acc: Record<string, TransactionAssetChangeData[]>, assetChange) => {
let updatedAcc = {};
if (assetChange.from) {
dollarValuePerAddress[assetChange.from] =
(dollarValuePerAddress[assetChange.from] || 0) -
Number(assetChange.dollarValue);
updatedAcc = {
[assetChange.from]: [...(acc?.[assetChange.from] || []), assetChange],
};
}
if (assetChange.to) {
dollarValuePerAddress[assetChange.to] =
(dollarValuePerAddress[assetChange.to] || 0) +
Number(assetChange.dollarValue);
updatedAcc = {
...updatedAcc,
[assetChange.to]: [...(acc?.[assetChange.to] || []), assetChange],
};
}
return {
...acc,
...updatedAcc,
};
},
{}
);
const showViewAll =
Object.keys(assetChangesPerAddress).length > MinAssetChangesToDisplay;
const showAll = !showViewAll || viewAll;
let assetChangesAddressesToDisplay = Object.keys(assetChangesPerAddress);
if (!showAll) {
assetChangesAddressesToDisplay = assetChangesAddressesToDisplay.slice(
0,
MinAssetChangesToDisplay
);
}
return (
<div className="PerAddressContent">
{assetChangesAddressesToDisplay.map((address, index) => (
<Accordion
key={`${activeTabValue}${index}`}
initiallyOpened={index === 0}
className="mt-2"
size="small"
headerClassName="PerAddressContent__Accordion__Header"
contentClassName="PerAddressContent__Accordion"
renderHeader={() => {
const sumDollarValue = dollarValuePerAddress[address] || 0;
const contract = contracts.find(
(contract) => contract.address === address
);
return (
<>
<div className="flex items-center">
<EthereumBlockie className="mr-4" hex={address} size={16} />
<div className="TextOverflowEllipsis">
{contract?.name || generateShortAddress(address, 8, 6)}
</div>
</div>
<div className="flex items-center ml-auto">
{(ifValuePositive(sumDollarValue) ||
ifValueNearZero(sumDollarValue)) && (
<div
// eslint-disable-next-line react/forbid-dom-props
id={`asset-changes-per-address-content-sum-change-${address}`}
className={classNames('mr-4 font-semibold', {
'text-green': !ifValueNearZero(sumDollarValue),
})}
>
{shouldShowApproximateSign(sumDollarValue) && (
<span>≈</span>
)}
<span>
$
{formatToLocaleString(
Math.abs(sumDollarValue).toString(),
3,
4
)}
</span>
</div>
)}
{ifValueNegative(sumDollarValue) &&
!ifValueNearZero(sumDollarValue) && (
<div
// eslint-disable-next-line react/forbid-dom-props
id={`asset-changes-per-address-content-sum-change-${address}`}
className="mr-4 font-semibold text-red"
>
{shouldShowApproximateSign(sumDollarValue) && (
<span>≈</span>
)}
<span>
-$
{formatToLocaleString(
Math.abs(sumDollarValue).toString(),
3,
4
)}
</span>
</div>
)}
</div>
</>
);
}}
>
<ChronologicalContent
className="ml-2"
assetChanges={assetChangesPerAddress[address]}
contracts={contracts}
groupAddress={address}
shouldShowApproximateSign={shouldShowApproximateSign}
// activeTabValue={activeTabValue}
/>
</Accordion>
))}
{showViewAll && (
<div className="PerAddressContent__LinkButton">
<a onClick={onViewAllToggle}>
{!viewAll && <span>Show all</span>}
{viewAll && <span>Show less</span>}
</a>
</div>
)}
</div>
);
}
export default PerAddressContent;