Skip to content

Commit

Permalink
Replace time with age in all tables
Browse files Browse the repository at this point in the history
  • Loading branch information
torztomasz committed Jul 27, 2023
1 parent c74112b commit de06d82
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 36 deletions.
43 changes: 43 additions & 0 deletions packages/frontend/src/view/components/TimeAgeCell.tsx
Original file line number Diff line number Diff line change
@@ -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 <div className="text-xs sm:text-sm">{age}</div>
}

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`
}
21 changes: 0 additions & 21 deletions packages/frontend/src/view/components/TimeCell.tsx

This file was deleted.

6 changes: 3 additions & 3 deletions packages/frontend/src/view/components/tables/OffersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'>
Expand Down Expand Up @@ -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' }] : []),
Expand All @@ -58,7 +58,7 @@ export function OffersTable(props: OffersTableProps) {
columns={columns}
rows={props.offers.map((offer) => {
const cells: ReactNode[] = [
<TimeCell timestamp={offer.timestamp} />,
<TimeAgeCell timestamp={offer.timestamp} />,
<Link>#{offer.id}</Link>,
<TradeColumn
offer={offer}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 TransactionsTableProps {
transactions: TransactionEntry[]
Expand All @@ -33,7 +33,7 @@ export interface TransactionEntry {
export function TransactionsTable(props: TransactionsTableProps) {
const columns: Column[] = []
if (!props.hideTime) {
columns.push({ header: 'Time (UTC)' })
columns.push({ header: 'Age' })
}
columns.push(
{ header: 'Tx Hash' },
Expand All @@ -51,7 +51,7 @@ export function TransactionsTable(props: TransactionsTableProps) {

const cells: ReactNode[] = []
if (!props.hideTime) {
cells.push(<TimeCell timestamp={transaction.timestamp} />)
cells.push(<TimeAgeCell timestamp={transaction.timestamp} />)
}
cells.push(
<Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +23,7 @@ export function HomeStateUpdatesTable(props: HomeStateUpdatesTableProps) {
return (
<Table
columns={[
{ header: 'Time (UTC)' },
{ header: 'Age' },
{ header: 'Id' },
{ header: 'Tx Hash' },
{ header: 'Updates', numeric: true },
Expand All @@ -41,7 +41,7 @@ export function HomeStateUpdatesTable(props: HomeStateUpdatesTableProps) {
return {
link: `/state-updates/${stateUpdate.id}`,
cells: [
<TimeCell timestamp={stateUpdate.timestamp} />,
<TimeAgeCell timestamp={stateUpdate.timestamp} />,
<Link>#{stateUpdate.id}</Link>,
<InlineEllipsis className="max-w-[80px] sm:max-w-[160px]">
{stateUpdate.hash.toString()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -23,15 +23,15 @@ export function TransactionHistoryTable(props: TransactionHistoryTableProps) {
<SectionHeading title="History" />
<Table
columns={[
{ header: 'Time (UTC)' },
{ header: 'Age' },
{ header: 'Status' },
{ header: 'Description' },
]}
rows={props.entries.map((entry) => {
return {
cells: [
entry.timestamp ? (
<TimeCell timestamp={entry.timestamp} />
<TimeAgeCell timestamp={entry.timestamp} />
) : (
// 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -28,7 +28,7 @@ export function UserBalanceChangesTable(props: UserBalanceChangesTableProps) {
return (
<Table
columns={[
{ header: 'Time (UTC)' },
{ header: 'Age' },
{ header: 'Update' },
{ header: 'Asset' },
{ header: 'Change', numeric: true },
Expand All @@ -39,7 +39,7 @@ export function UserBalanceChangesTable(props: UserBalanceChangesTableProps) {
return {
link: `/state-updates/${entry.stateUpdateId}`,
cells: [
<TimeCell timestamp={entry.timestamp} />,
<TimeAgeCell timestamp={entry.timestamp} />,
<Link>#{entry.stateUpdateId}</Link>,
<AssetWithLogo type="small" assetInfo={assetToInfo(entry.asset)} />,
<ChangeText className="text-sm font-medium">
Expand Down

0 comments on commit de06d82

Please sign in to comment.