diff --git a/packages/frontend/src/view/components/TimeAgeCell.tsx b/packages/frontend/src/view/components/TimeAgeCell.tsx new file mode 100644 index 000000000..a16fdacb2 --- /dev/null +++ b/packages/frontend/src/view/components/TimeAgeCell.tsx @@ -0,0 +1,43 @@ +import { Timestamp } from '@explorer/types' +import React from 'react' + +interface TimeCellProps { + timestamp: Timestamp +} + +export function TimeAgeCell({ timestamp }: TimeCellProps) { + const age = getAge(timestamp) + + return
{age}
+} + +function getAge(timestamp: Timestamp) { + const now = BigInt(new Date().getTime()) + const age = now - timestamp.valueOf() + const seconds = age / 1000n + const minutes = seconds / 60n + const hours = minutes / 60n + const days = hours / 24n + + if (seconds < 10n) { + return 'Just now' + } + + if (seconds < 60n && minutes === 0n && hours === 0n && days === 0n) { + return `${seconds} ${addPlural('sec', seconds)} ago` + } + + if (minutes < 60 && hours === 0n && days === 0n) { + return `${minutes} ${addPlural('min', minutes)} ago` + } + + if (hours < 24 && days === 0n) { + return `${hours} ${addPlural('hour', hours)} ago` + } + + return `${days} ${addPlural('day', days)} ago` +} + +function addPlural(val: string, n: bigint): string { + return n === 1n ? val : `${val}s` +} diff --git a/packages/frontend/src/view/components/TimeCell.tsx b/packages/frontend/src/view/components/TimeCell.tsx deleted file mode 100644 index cf583028f..000000000 --- a/packages/frontend/src/view/components/TimeCell.tsx +++ /dev/null @@ -1,21 +0,0 @@ -import { Timestamp } from '@explorer/types' -import React from 'react' - -import { formatTimestampParts } from '../../utils/formatting/formatTimestamp' - -interface TimeCellProps { - timestamp: Timestamp -} - -export function TimeCell({ timestamp }: TimeCellProps) { - const { datePart, timePart } = formatTimestampParts(timestamp) - - return ( -
- {datePart} - - {timePart} - -
- ) -} diff --git a/packages/frontend/src/view/components/tables/OffersTable.tsx b/packages/frontend/src/view/components/tables/OffersTable.tsx index 15c129f08..7345a56bd 100644 --- a/packages/frontend/src/view/components/tables/OffersTable.tsx +++ b/packages/frontend/src/view/components/tables/OffersTable.tsx @@ -14,7 +14,7 @@ import { Link } from '../Link' import { StatusBadge, StatusType } from '../StatusBadge' import { Table } from '../table/Table' import { Column } from '../table/types' -import { TimeCell } from '../TimeCell' +import { TimeAgeCell } from '../TimeAgeCell' interface OffersTableProps { context: PageContext<'perpetual'> @@ -45,7 +45,7 @@ export interface OfferEntry { export function OffersTable(props: OffersTableProps) { const columns: Column[] = [ - { header: 'Time (UTC)' }, + { header: 'Age' }, { header: 'Id' }, { header: 'Trade', align: 'center' }, ...(props.showStatus ? [{ header: 'Status' }] : []), @@ -58,7 +58,7 @@ export function OffersTable(props: OffersTableProps) { columns={columns} rows={props.offers.map((offer) => { const cells: ReactNode[] = [ - , + , #{offer.id}, ) + cells.push() } cells.push( diff --git a/packages/frontend/src/view/pages/home/components/HomeStateUpdatesTable.tsx b/packages/frontend/src/view/pages/home/components/HomeStateUpdatesTable.tsx index 5214579e0..17d9fe3e6 100644 --- a/packages/frontend/src/view/pages/home/components/HomeStateUpdatesTable.tsx +++ b/packages/frontend/src/view/pages/home/components/HomeStateUpdatesTable.tsx @@ -5,7 +5,7 @@ import { formatInt } from '../../../../utils/formatting/formatAmount' import { InlineEllipsis } from '../../../components/InlineEllipsis' import { Link } from '../../../components/Link' import { Table } from '../../../components/table/Table' -import { TimeCell } from '../../../components/TimeCell' +import { TimeAgeCell } from '../../../components/TimeAgeCell' export interface HomeStateUpdateEntry { timestamp: Timestamp @@ -23,7 +23,7 @@ export function HomeStateUpdatesTable(props: HomeStateUpdatesTableProps) { return ( , + , #{stateUpdate.id}, {stateUpdate.hash.toString()} diff --git a/packages/frontend/src/view/pages/transaction/components/HistoryTable.tsx b/packages/frontend/src/view/pages/transaction/components/HistoryTable.tsx index 8ad3a91bf..9bd0b77da 100644 --- a/packages/frontend/src/view/pages/transaction/components/HistoryTable.tsx +++ b/packages/frontend/src/view/pages/transaction/components/HistoryTable.tsx @@ -4,7 +4,7 @@ import React from 'react' import { SectionHeading } from '../../../components/SectionHeading' import { StatusBadge, StatusType } from '../../../components/StatusBadge' import { Table } from '../../../components/table/Table' -import { TimeCell } from '../../../components/TimeCell' +import { TimeAgeCell } from '../../../components/TimeAgeCell' interface TransactionHistoryTableProps { entries: TransactionHistoryEntry[] @@ -23,7 +23,7 @@ export function TransactionHistoryTable(props: TransactionHistoryTableProps) {
+ ) : ( // This may be unknown if i.e. forced trade offer was not initiated using our explorer. // We know that the offer was created, but we don't know when. diff --git a/packages/frontend/src/view/pages/user/components/UserBalanceChangesTable.tsx b/packages/frontend/src/view/pages/user/components/UserBalanceChangesTable.tsx index 17ce16b8b..ba7296769 100644 --- a/packages/frontend/src/view/pages/user/components/UserBalanceChangesTable.tsx +++ b/packages/frontend/src/view/pages/user/components/UserBalanceChangesTable.tsx @@ -8,7 +8,7 @@ import { AssetWithLogo } from '../../../components/AssetWithLogo' import { ChangeText } from '../../../components/ChangeText' import { Link } from '../../../components/Link' import { Table } from '../../../components/table/Table' -import { TimeCell } from '../../../components/TimeCell' +import { TimeAgeCell } from '../../../components/TimeAgeCell' interface UserBalanceChangesTableProps { balanceChanges: UserBalanceChangeEntry[] @@ -28,7 +28,7 @@ export function UserBalanceChangesTable(props: UserBalanceChangesTableProps) { return (
, + , #{entry.stateUpdateId}, ,