Skip to content

Commit

Permalink
fix(@cockpit/explorers): consistently display "Mined on" timestamps
Browse files Browse the repository at this point in the history
Adjust the API endpoints to augment transaction objects with a timestamp
property from their corresponding blocks. This removes the necessity to copy
the timestamp property from a block to its transactions in the client.

Introduce a `formatTimestampForDisplay` util function in Cockpit for
consistently transforming timestamps into relative or absolute dates, depending
on whether a date is sometime during the last day.
  • Loading branch information
michaelsbradleyjr committed Jul 3, 2019
1 parent 7d27125 commit 52d54f0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
11 changes: 10 additions & 1 deletion packages/embark-blockchain-connector/src/index.js
Expand Up @@ -610,6 +610,7 @@ class BlockchainConnector {
.then(receipts => {
block.transactions.forEach((tx, index) => {
tx['receipt'] = receipts[index];
tx['timestamp'] = block.timestamp;
});
blocks.push(block);
eachCb();
Expand Down Expand Up @@ -655,7 +656,15 @@ class BlockchainConnector {
}

getBlock(blockNumber, cb) {
this.web3.eth.getBlock(blockNumber, true, cb);
this.web3.eth.getBlock(blockNumber, true, (err, block) => {
if (err) return cb(err);
if (block.transactions && block.transactions.length) {
block.transactions.forEach(tx => {
tx.timestamp = block.timestamp;
});
}
cb(null, block);
});
}

getTransactionByHash(hash, cb) {
Expand Down
3 changes: 2 additions & 1 deletion packages/embark-ui/src/components/Blocks.js
Expand Up @@ -3,6 +3,7 @@ import {Link} from "react-router-dom";
import {Row, Col, Card, CardHeader, CardBody} from 'reactstrap';
import PropTypes from 'prop-types';
import Pagination from './Pagination';
import {formatTimestampForDisplay} from '../utils/presentation';

import CardTitleIdenticon from './CardTitleIdenticon';

Expand All @@ -25,7 +26,7 @@ const Blocks = ({blocks, changePage, currentPage, numberOfPages}) => (
<Row>
<Col>
<strong>Mined on:</strong>
<div>{new Date(block.timestamp * 1000).toLocaleString()}</div>
<div>{formatTimestampForDisplay(block.timestamp)}</div>
</Col>
<Col>
<strong>Gas Used</strong>
Expand Down
10 changes: 2 additions & 8 deletions packages/embark-ui/src/components/Transactions.js
Expand Up @@ -2,8 +2,7 @@ import React from 'react';
import {Link} from "react-router-dom";
import {Row, Col, Card, CardHeader, CardBody} from 'reactstrap';
import PropTypes from 'prop-types';
import isToday from 'date-fns/is_today';
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
import {formatTimestampForDisplay} from '../utils/presentation';

import DebugButton from './DebugButton';
import CardTitleIdenticon from './CardTitleIdenticon';
Expand Down Expand Up @@ -50,12 +49,7 @@ const Transactions = ({transactions, contracts, changePage, currentPage, numberO
</Col>
<Col md={6}>
<strong>Mined on:</strong>
<div>
{isToday(new Date(transaction.timestamp * 1000)) ?
distanceInWordsToNow(new Date(transaction.timestamp * 1000), {addSuffix: true}) :
new Date(transaction.timestamp * 1000).toLocaleString()
}
</div>
<div>{formatTimestampForDisplay(transaction.timestamp)}</div>
</Col>
</Row>
</div>
Expand Down
1 change: 0 additions & 1 deletion packages/embark-ui/src/containers/TransactionsContainer.js
Expand Up @@ -97,7 +97,6 @@ class TransactionsContainer extends Component {
const txsLength = txs.length;
block.transactions.forEach((tx, idx) => {
txs[txsLength + idx + offset] = tx;
txs[txsLength + idx + offset].timestamp = block.timestamp;
});
return txs;
}, []);
Expand Down
10 changes: 10 additions & 0 deletions packages/embark-ui/src/utils/presentation.js
@@ -1,3 +1,6 @@
import isToday from 'date-fns/is_today';
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';

export function formatContractForDisplay(contract) {
let address = (contract.address || contract.deployedAddress);
let state = 'Deployed';
Expand All @@ -17,3 +20,10 @@ export function formatContractForDisplay(contract) {
}
return {address, state, stateColor};
}

export function formatTimestampForDisplay(timestamp) {
if (isToday(new Date(timestamp * 1000))) {
return distanceInWordsToNow(new Date(timestamp * 1000), {addSuffix: true});
}
return new Date(timestamp * 1000).toLocaleString();
}

0 comments on commit 52d54f0

Please sign in to comment.