From abcc9ca2061c1fb2fa8e396b77ed6787a7b92662 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 15 Dec 2020 12:17:18 +0100 Subject: [PATCH] Elapsed formatting (#4216) --- packages/react-query/src/Elapsed.tsx | 34 ++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/packages/react-query/src/Elapsed.tsx b/packages/react-query/src/Elapsed.tsx index 18496f4515..c631ad9b48 100644 --- a/packages/react-query/src/Elapsed.tsx +++ b/packages/react-query/src/Elapsed.tsx @@ -3,6 +3,7 @@ import BN from 'bn.js'; import React, { useEffect, useState } from 'react'; +import styled from 'styled-components'; import { bnToBn } from '@polkadot/util'; @@ -30,25 +31,39 @@ function tick (): void { setTimeout(tick, TICK_TIMEOUT); } -function getDisplayValue (now = 0, value: BN | Date | number = 0): string { +function formatValue (value: number, type = 's', withDecimal = false): React.ReactNode { + const [pre, post] = value.toFixed(1).split('.'); + const before = pre.split('').map((d, index) => ( +
{d}
+ )); + + return withDecimal + ? <>{before}.
{post}
{type} + : <>{before} s; +} + +function getDisplayValue (now = 0, value: BN | Date | number = 0): React.ReactNode { const tsValue = ( value && (value as Date).getTime ? (value as Date).getTime() : bnToBn(value as number).toNumber() ) || 0; - let display = '0.0 s'; + let display = formatValue(0, 's', true); if (now && tsValue) { const elapsed = Math.max(Math.abs(now - tsValue), 0) / 1000; if (elapsed < 15) { - display = `${elapsed.toFixed(1)} s`; + display = formatValue(elapsed, 's', true); } else if (elapsed < 60) { - display = `${elapsed | 0} s`; + display = formatValue(elapsed); } else if (elapsed < 3600) { - display = `${elapsed / 60 | 0} min`; + display = formatValue(elapsed, 'min'); } else { - display = `${elapsed / 3600 | 0} hr`; + display = formatValue(elapsed / 3600, 'hr'); } } @@ -77,4 +92,9 @@ function Elapsed ({ children, className = '', value }: Props): React.ReactElemen ); } -export default React.memo(Elapsed); +export default React.memo(styled(Elapsed)` + .digit { + display: inline-block; + width: 1ch; + } +`);