Skip to content

Commit

Permalink
Remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed Nov 15, 2024
1 parent 31b4e43 commit 42f06c8
Showing 1 changed file with 0 additions and 37 deletions.
37 changes: 0 additions & 37 deletions dapp/src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import {
ONE_DAY_IN_SECONDS,
ONE_HOUR_IN_SECONDS,
ONE_MINUTE_IN_SECONDS,
ONE_MONTH_IN_SECONDS,
ONE_SEC_IN_MILLISECONDS,
ONE_WEEK_IN_SECONDS,
ONE_YEAR_IN_SECONDS,
} from "#/constants"
import { TimeUnits } from "#/types"
import { DateTime } from "luxon"
Expand All @@ -32,40 +29,6 @@ export const unixTimestampToTimeUnits = (targetUnix: number): TimeUnits => {
}
}

// unit, max diff, divisor
const unitsToDivisor: [Intl.RelativeTimeFormatUnit, number, number][] = [
["second", ONE_MINUTE_IN_SECONDS, 1],
["minute", ONE_HOUR_IN_SECONDS, ONE_MINUTE_IN_SECONDS],
["hour", ONE_DAY_IN_SECONDS, ONE_HOUR_IN_SECONDS],
["day", ONE_WEEK_IN_SECONDS, ONE_DAY_IN_SECONDS],
["week", ONE_MONTH_IN_SECONDS, ONE_WEEK_IN_SECONDS],
["month", ONE_YEAR_IN_SECONDS, ONE_MONTH_IN_SECONDS],
["year", Infinity, ONE_YEAR_IN_SECONDS],
]
const rtf = new Intl.RelativeTimeFormat(DATE_FORMAT_LOCALE_TAG)

/**
* The problem of displaying relative time has already been solved in Threshold Network
* Let's use this logic to be able to display relative time such as: 2 mins ago, 3 hours ago...
*
* Source:
* https://github.com/threshold-network/token-dashboard/blob/main/src/utils/date.ts#L48-L61
*/
export const getRelativeTime = (dateOrUnixTimestamp: Date | number): string => {
const time =
typeof dateOrUnixTimestamp === "number"
? dateOrUnixTimestamp
: dateToUnixTimestamp(dateOrUnixTimestamp)

const diff = Math.round(time - dateToUnixTimestamp())

const [unit, , divisor] =
unitsToDivisor.find(([, maxDiff]) => maxDiff > Math.abs(diff)) ??
unitsToDivisor[0]

return rtf.format(Math.floor(diff / divisor), unit)
}

export const timestampToRelativeTime = (timestamp: number) =>
DateTime.fromMillis(timestamp).toRelative()

Expand Down

0 comments on commit 42f06c8

Please sign in to comment.