Skip to content

Commit

Permalink
[web] Edit tx display time
Browse files Browse the repository at this point in the history
  • Loading branch information
dnjooiopa committed Apr 27, 2024
1 parent f3c4eef commit 2b6d21c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
9 changes: 2 additions & 7 deletions web/src/components/Transactions.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
'use client'

import { FC, useEffect, useMemo, useState } from 'react'
import { format } from 'date-fns'

import useIsMounted from '@/hooks/useIsMounted'
import Axios from '@/libs/axios'
import { Transaction } from '@/types'
import { TransactionType } from '@/enums'

const formatDateTime = (ts: number) => {
const f = 'yyyy-MM-dd HH:mm:ss'
return format(new Date(ts), f)
}
import { shortenTime } from '@/utils/date'

const TxItem = ({ amountSat, description, createdAt, type }: Transaction) => {
const amountColor = type === TransactionType.PAYMENT_SENT ? 'text-gray-100' : 'text-green-500'
Expand All @@ -25,7 +20,7 @@ const TxItem = ({ amountSat, description, createdAt, type }: Transaction) => {
<span className="text-lg">{displayDescription}</span>
</div>
<div>
<span className="text-sm">{formatDateTime(createdAt)}</span>
<span className="text-sm">{shortenTime(new Date(createdAt))}</span>
</div>
</div>
<div>
Expand Down
53 changes: 53 additions & 0 deletions web/src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const Minute = 60
const Hour = 3600
const Day = 86400
const Month = 30 * 86400
const Year = 365 * 86400

export const shortenTime = (d: Date) => {
const now = new Date()

let duration = (now.getTime() - d.getTime()) / 1000

if (duration < Minute) {
return 'a few seconds ago'
}

if (duration < Hour) {
duration = Number.parseInt(String(duration / Minute))
if (duration === 1) {
return 'a minute ago'
}
return duration + ' minutes ago'
}

if (duration < Day) {
duration = Number.parseInt(String(duration / Hour))
if (duration === 1) {
return 'an hour ago'
}
return duration + ' hours ago'
}

if (duration < Month) {
duration = Number.parseInt(String(duration / Day))
if (duration === 1) {
return 'a day ago'
}
return duration + ' days ago'
}

if (duration < Year) {
duration = Number.parseInt(String(duration / Month))
if (duration === 1) {
return 'a month ago'
}
return duration + ' months ago'
}

duration = Number.parseInt(String(duration / Year))
if (duration === 1) {
return 'a year ago'
}
return duration + ' years ago'
}

0 comments on commit 2b6d21c

Please sign in to comment.