Skip to content

Commit 00fb7c1

Browse files
committed
use Intl namespace
1 parent 49728da commit 00fb7c1

File tree

3 files changed

+154
-5
lines changed

3 files changed

+154
-5
lines changed

components/SponsoredFeedsTableWithData.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import CopyIcon from "./icons/CopyIcon";
33
import { mapValues } from "../utils/ObjectHelpers";
44
import { useCopyToClipboard } from "../utils/useCopyToClipboard";
5+
import "@formatjs/intl-durationformat/polyfill";
56

67
// SponsoredFeed interface has the same structure as defined in deployment yaml/json files
78
interface SponsoredFeed {
@@ -27,11 +28,29 @@ interface UpdateParamsProps {
2728
*/
2829
// Convert time_difference (seconds) to human readable format
2930
const formatTimeUnit = (seconds: number): { value: number; unit: string } => {
31+
const duration = new (Intl as any).DurationFormat("en", {
32+
style: "long",
33+
numeric: "auto",
34+
});
35+
let durationObj: { hours?: number; minutes?: number; seconds?: number };
36+
3037
if (seconds >= 3600) {
31-
return { value: seconds / 3600, unit: "hour" };
38+
durationObj = { hours: Math.floor(seconds / 3600) };
3239
} else if (seconds >= 60) {
33-
return { value: seconds / 60, unit: "minute" };
40+
durationObj = { minutes: Math.floor(seconds / 60) };
41+
} else {
42+
durationObj = { seconds };
43+
}
44+
45+
const formatted = duration.format(durationObj);
46+
const match = formatted.match(/^([0-9]+)\s*(.*)$/);
47+
if (match) {
48+
return {
49+
value: parseInt(match[1], 10),
50+
unit: match[2].replace(/[0-9]/g, ""), // Remove any numbers from the unit
51+
};
3452
} else {
53+
// fallback in case formatting fails
3554
return { value: seconds, unit: "second" };
3655
}
3756
};

package-lock.json

Lines changed: 132 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@codesandbox/sandpack-react": "^2.6.1",
1717
"@cookbookdev/docsbot": "^4.21.5",
1818
"@cosmjs/cosmwasm-stargate": "^0.31.0",
19+
"@formatjs/intl-durationformat": "^0.7.4",
1920
"@headlessui/react": "^1.7.14",
2021
"@metamask/detect-provider": "^2.0.0",
2122
"connectkit": "^1.4.0",

0 commit comments

Comments
 (0)