From 80891c54a0895d5981e32851b6fc4998ee7e1d0d Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Thu, 12 Dec 2024 17:16:30 +1100 Subject: [PATCH] fix: inception unable to handle months and years --- package-lock.json | 4 ++-- package.json | 2 +- src/utils/formatTime.ts | 25 +++++++++++++++++++------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4ee4675f..21ac8bdd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "simple-staking", - "version": "0.3.20", + "version": "0.3.21", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "simple-staking", - "version": "0.3.20", + "version": "0.3.21", "dependencies": { "@babylonlabs-io/btc-staking-ts": "0.3.0", "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3", diff --git a/package.json b/package.json index 5f699adf..30e50b8a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simple-staking", - "version": "0.3.20", + "version": "0.3.21", "private": true, "scripts": { "dev": "next dev", diff --git a/src/utils/formatTime.ts b/src/utils/formatTime.ts index 357ad954..d546e7ac 100644 --- a/src/utils/formatTime.ts +++ b/src/utils/formatTime.ts @@ -10,17 +10,30 @@ interface Duration { seconds?: number; } -export const durationTillNow = (time: string, currentTime: number) => { - if (!time || time.startsWith("000")) return "Ongoing"; +export const durationTillNow = ( + startTimestamp: string, + currentTime: number, +) => { + if (!startTimestamp || startTimestamp.startsWith("000")) return "Ongoing"; const duration = intervalToDuration({ end: currentTime, - start: new Date(time), + start: new Date(startTimestamp), }); - let format: (keyof Duration)[] = ["days", "hours", "minutes"]; - if (!duration.days && !duration.hours && !duration.minutes) { - format.push("seconds"); + let format: (keyof Duration)[] = []; + + // If there are months or years, only show months and days + if (duration.months || duration.years) { + format = ["years", "months", "days"]; + } + // If only days or less, show more detailed time + else { + format = ["days", "hours", "minutes"]; + // Add seconds only if less than a minute + if (!duration.days && !duration.hours && !duration.minutes) { + format.push("seconds"); + } } const formattedTime = formatDuration(duration, {