@@ -2,6 +2,7 @@ import React from "react";
2
2
import CopyIcon from "./icons/CopyIcon" ;
3
3
import { mapValues } from "../utils/ObjectHelpers" ;
4
4
import { useCopyToClipboard } from "../utils/useCopyToClipboard" ;
5
+ import "@formatjs/intl-durationformat/polyfill" ;
5
6
6
7
// SponsoredFeed interface has the same structure as defined in deployment yaml/json files
7
8
interface SponsoredFeed {
@@ -27,11 +28,29 @@ interface UpdateParamsProps {
27
28
*/
28
29
// Convert time_difference (seconds) to human readable format
29
30
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
+
30
37
if ( seconds >= 3600 ) {
31
- return { value : seconds / 3600 , unit : "hour" } ;
38
+ durationObj = { hours : Math . floor ( seconds / 3600 ) } ;
32
39
} 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
+ } ;
34
52
} else {
53
+ // fallback in case formatting fails
35
54
return { value : seconds , unit : "second" } ;
36
55
}
37
56
} ;
0 commit comments