Skip to content

Commit 55300d8

Browse files
committed
Add PartyId component
1 parent 483ac2f commit 55300d8

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {formatPartyId} from "../src/utils";
2+
3+
describe('Stake formatting', () => {
4+
it.each([
5+
["pool1zmtm8yef33z2n7x4nn0kvv9xpzjuj7725p9y9m5t960g5qy51ua", "pool1zmtm8…y51ua"],
6+
["pool23kk0fksdayg23htnj372avmnwwql9c2zz0ah8jt63rjdzyjr95n", "pool23kk0f…jr95n"],
7+
])('formatting party id remove all but the 10 first and the last 5 characters', (partyId, expected) => {
8+
expect(formatPartyId(partyId)).toEqual(expected);
9+
});
10+
11+
it.each([
12+
["pool1zmtm8yef33zuj7725p9y9m5t960g5qy51ua", "pool1zmtm8…y51ua"],
13+
["pool1zmtm8yef33z22n7x4nn0kvv9xpzjn7x4nn0kvv9xpzjuj7725p9y9m5t960g5qy51ua", "pool1zmtm8…y51ua"],
14+
["pool23kk0fyjr95n", "pool23kk0f…jr95n"],
15+
["pool23kk0fjr95n", "pool23kk0fjr95n"],
16+
["pool23kk0jr95n", "pool23kk0jr95n"],
17+
["pool25n", "pool25n"],
18+
])('formatting party id remove all but the 10 first and the last 5 characters even for non usual length', (partyId, expected) => {
19+
expect(formatPartyId(partyId)).toEqual(expected);
20+
});
21+
});

mithril-explorer/src/components/CertificateModal/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ProtocolParameters from "../ProtocolParameters";
77
import PoolTicker from "../PoolTicker";
88
import VerifiedBadge from '../VerifiedBadge';
99
import {selectedAggregator} from "../../store/settingsSlice";
10+
import PartyId from "../PartyId";
1011

1112
export default function CertificateModal(props) {
1213
const [certificate, setCertificate] = useState({});
@@ -96,8 +97,8 @@ export default function CertificateModal(props) {
9697
<VerifiedBadge tooltip="Verified Signer"/>
9798
}
9899
</td>
99-
<td>{signer.party_id}</td>
100-
<td><PoolTicker aggregator={aggregator} partyId={signer.party_id} /></td>
100+
<td><PartyId partyId={signer.party_id}/></td>
101+
<td><PoolTicker aggregator={aggregator} partyId={signer.party_id}/></td>
101102
<td style={{textAlign: "end"}}><Stake lovelace={signer.stake}/></td>
102103
</tr>
103104
)}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import {Button, OverlayTrigger, Tooltip} from "react-bootstrap";
3+
import {formatPartyId} from "../utils";
4+
5+
export default function PartyId({partyId}) {
6+
function copyToClipboard() {
7+
if (window.isSecureContext && partyId) {
8+
navigator.clipboard.writeText(partyId).then(() => {
9+
});
10+
}
11+
}
12+
13+
return (
14+
<>
15+
<Button variant="link" onClick={copyToClipboard} size="md" className="p-0">
16+
<i className="bi bi-copy" style={{color: 'black'}}></i>
17+
</Button>
18+
<> </>
19+
<OverlayTrigger overlay={<Tooltip>{partyId}</Tooltip>}>
20+
<span>{formatPartyId(partyId)}</span>
21+
</OverlayTrigger>
22+
</>
23+
);
24+
}

mithril-explorer/src/components/PendingCertificate/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, {useEffect, useState} from 'react';
22
import {Card, CardGroup, ListGroup} from "react-bootstrap";
33
import {useSelector} from "react-redux";
4+
import PartyId from "../PartyId";
45
import PoolTicker from "../PoolTicker";
56
import RawJsonButton from "../RawJsonButton";
67
import SignedEntityType from "../SignedEntityType";
@@ -74,7 +75,7 @@ export default function PendingCertificate(props) {
7475
{pendingCertificate.signers.map(signer =>
7576
<ListGroup.Item key={signer.party_id}>
7677
<PoolTicker partyId={signer.party_id} aggregator={aggregator}/><br/>
77-
{signer.party_id}
78+
<PartyId partyId={signer.party_id}/>
7879
{signer.verification_key_signature &&
7980
<div className="float-end">
8081
<VerifiedBadge tooltip="Verified Signer"/>
@@ -98,7 +99,7 @@ export default function PendingCertificate(props) {
9899
{pendingCertificate.next_signers.map(signer =>
99100
<ListGroup.Item key={signer.party_id}>
100101
<PoolTicker partyId={signer.party_id} aggregator={aggregator}/><br/>
101-
{signer.party_id}
102+
<PartyId partyId={signer.party_id}/>
102103
{signer.verification_key_signature &&
103104
<div className="float-end">
104105
<VerifiedBadge tooltip="Verified Signer"/>

mithril-explorer/src/utils.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ const formatCurrency = (number, maximumFractionDigits = 2) => number.toLocaleStr
1313
maximumFractionDigits: maximumFractionDigits,
1414
});
1515

16+
function formatPartyId(partyId) {
17+
if ((typeof partyId === 'string' || partyId instanceof String) && partyId.length > 15) {
18+
return partyId.slice(0, 10) + "…" + partyId.slice(-5)
19+
} else {
20+
return partyId;
21+
}
22+
}
23+
1624
function formatStake(lovelace) {
1725
// Credits to Jasser Mark Arioste for the original idea:
1826
// https://reacthustle.com/blog/how-to-convert-number-to-kmb-format-in-javascript
@@ -89,5 +97,6 @@ module.exports = {
8997
setChartJsDefaults,
9098
toAda,
9199
formatCurrency,
92-
formatBytes
100+
formatBytes,
101+
formatPartyId,
93102
}

0 commit comments

Comments
 (0)