From 071c26ef5136231d142f97784d352b5effdc050c Mon Sep 17 00:00:00 2001 From: maciejhirsz Date: Sun, 15 Jul 2018 10:48:16 +0200 Subject: [PATCH] Minor tweaks to time displays --- packages/frontend/src/Connection.ts | 2 ++ packages/frontend/src/components/Chain.tsx | 4 ++-- packages/frontend/src/components/Node.tsx | 6 +++--- packages/frontend/src/utils.ts | 20 ++++++++++++++++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/frontend/src/Connection.ts b/packages/frontend/src/Connection.ts index 09c25c2..7b5ef37 100644 --- a/packages/frontend/src/Connection.ts +++ b/packages/frontend/src/Connection.ts @@ -114,6 +114,8 @@ export class Connection { case Actions.BestBlock: { const [best, blockTimestamp, blockAverage] = message.payload; + nodes.forEach((node) => node.blockDetails[4] = null); + this.state = this.update({ best, blockTimestamp, blockAverage }); continue messages; diff --git a/packages/frontend/src/components/Chain.tsx b/packages/frontend/src/components/Chain.tsx index 2c9b9a3..8d065ce 100644 --- a/packages/frontend/src/components/Chain.tsx +++ b/packages/frontend/src/components/Chain.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; import { State as AppState } from '../state'; -import { formatNumber } from '../utils'; +import { formatNumber, secondsWithPrecision } from '../utils'; import { Tile, Icon, Node, Ago } from './'; import nodeIcon from '../icons/server.svg'; @@ -54,7 +54,7 @@ export class Chain extends React.Component {
#{formatNumber(best)} - { blockAverage == null ? '-' : (blockAverage / 1000).toFixed(3) + 's' } + { blockAverage == null ? '-' : secondsWithPrecision(blockAverage / 1000) }
diff --git a/packages/frontend/src/components/Node.tsx b/packages/frontend/src/components/Node.tsx index 459f213..861e31b 100644 --- a/packages/frontend/src/components/Node.tsx +++ b/packages/frontend/src/components/Node.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { formatNumber, trimHash } from '../utils'; +import { formatNumber, trimHash, milliOrSecond, secondsWithPrecision } from '../utils'; import { Ago } from './Ago'; import { Types, Maybe } from '@dotstats/common'; @@ -26,8 +26,8 @@ export function Node(props: Node.Props) { {txcount} #{formatNumber(height)} {trimHash(hash, 16)} - {(blockTime / 1000).toFixed(3)}s - {propagationTime === null ? '∞' : `${propagationTime}ms`} + {secondsWithPrecision(blockTime/1000)} + {propagationTime === null ? '∞' : milliOrSecond(propagationTime as number)} ); diff --git a/packages/frontend/src/utils.ts b/packages/frontend/src/utils.ts index 788824e..7a459b4 100644 --- a/packages/frontend/src/utils.ts +++ b/packages/frontend/src/utils.ts @@ -23,3 +23,23 @@ export function trimHash(hash: string, length: number): string { return hash.substr(0, side) + '..' + hash.substr(-side, side); } + +export function milliOrSecond(num: number): string { + if (num < 10000) { + return `${num}ms`; + } + + return `${(num / 1000) | 0}s`; +} + +export function secondsWithPrecision(num: number): string { + const intString = (num | 0).toString() + const intDigits = intString.length; + + switch (intDigits) { + case 1: return num.toFixed(3) + 's'; + case 2: return num.toFixed(2) + 's'; + case 3: return num.toFixed(1) + 's'; + default: return intString + 's'; + } +}