FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
chatwoot/app/javascript/shared/helpers/timeHelper.js at develop · jakharamit44/chatwoot · GitHub
jakharamit44
/
chatwoot
Public
forked from
chatwoot/chatwoot
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
chatwoot
/
app
/
javascript
/
shared
/
helpers
/
timeHelper.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
140 lines (129 loc) · 4.81 KB
Breadcrumbs
chatwoot
/
app
/
javascript
/
shared
/
helpers
/
timeHelper.js
Copy path
File metadata and controls
140 lines (129 loc) · 4.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import
{
format
,
isSameYear
,
fromUnixTime
,
formatDistanceToNow
,
differenceInDays
,
}
from
'date-fns'
;
/**
* Formats a Unix timestamp into a human-readable time format.
*
@param
{
number
} time - Unix timestamp.
*
@param
{
string
} [dateFormat='h:mm a'] - Desired format of the time.
*
@returns
{
string
} Formatted time string.
*/
export
const
messageStamp
=
(
time
,
dateFormat
=
'h:mm a'
)
=>
{
const
unixTime
=
fromUnixTime
(
time
)
;
return
format
(
unixTime
,
dateFormat
)
;
}
;
/**
* Provides a formatted timestamp, adjusting the format based on the current year.
*
@param
{
number
} time - Unix timestamp.
*
@param
{
string
} [dateFormat='MMM d, yyyy'] - Desired date format.
*
@returns
{
string
} Formatted date string.
*/
export
const
messageTimestamp
=
(
time
,
dateFormat
=
'MMM d, yyyy'
)
=>
{
const
messageTime
=
fromUnixTime
(
time
)
;
const
now
=
new
Date
(
)
;
const
messageDate
=
format
(
messageTime
,
dateFormat
)
;
if
(
!
isSameYear
(
messageTime
,
now
)
)
{
return
format
(
messageTime
,
'LLL d y, h:mm a'
)
;
}
return
messageDate
;
}
;
/**
* Converts a Unix timestamp to a relative time string (e.g., 3 hours ago).
*
@param
{
number
} time - Unix timestamp.
*
@returns
{
string
} Relative time string.
*/
export
const
dynamicTime
=
time
=>
{
const
unixTime
=
fromUnixTime
(
time
)
;
return
formatDistanceToNow
(
unixTime
,
{
addSuffix
:
true
}
)
;
}
;
/**
* Formats a Unix timestamp into a specified date format.
*
@param
{
number
} time - Unix timestamp.
*
@param
{
string
} [dateFormat='MMM d, yyyy'] - Desired date format.
*
@returns
{
string
} Formatted date string.
*/
export
const
dateFormat
=
(
time
,
df
=
'MMM d, yyyy'
)
=>
{
const
unixTime
=
fromUnixTime
(
time
)
;
return
format
(
unixTime
,
df
)
;
}
;
/**
* Converts a detailed time description into a shorter format, optionally appending 'ago'.
*
@param
{
string
} time - Detailed time description (e.g., 'a minute ago').
*
@param
{
boolean
} [withAgo=false] - Whether to append 'ago' to the result.
*
@returns
{
string
} Shortened time description.
*/
export
const
shortTimestamp
=
(
time
,
withAgo
=
false
)
=>
{
// This function takes a time string and converts it to a short time string
// with the following format: 1m, 1h, 1d, 1mo, 1y
// The function also takes an optional boolean parameter withAgo
// which will add the word "ago" to the end of the time string
const
suffix
=
withAgo
?
' ago'
:
''
;
const
timeMappings
=
{
'less than a minute ago'
:
'now'
,
'in less than a minute'
:
'now'
,
'a minute ago'
:
`1m
${
suffix
}
`
,
'an hour ago'
:
`1h
${
suffix
}
`
,
'a day ago'
:
`1d
${
suffix
}
`
,
'a month ago'
:
`1mo
${
suffix
}
`
,
'a year ago'
:
`1y
${
suffix
}
`
,
}
;
// Check if the time string is one of the specific cases
if
(
timeMappings
[
time
]
)
{
return
timeMappings
[
time
]
;
}
const
convertToShortTime
=
time
.
replace
(
/
a
b
o
u
t
|
o
v
e
r
|
a
l
m
o
s
t
|
/
g
,
''
)
.
replace
(
' minute ago'
,
`m
${
suffix
}
`
)
.
replace
(
' minutes ago'
,
`m
${
suffix
}
`
)
.
replace
(
' hour ago'
,
`h
${
suffix
}
`
)
.
replace
(
' hours ago'
,
`h
${
suffix
}
`
)
.
replace
(
' day ago'
,
`d
${
suffix
}
`
)
.
replace
(
' days ago'
,
`d
${
suffix
}
`
)
.
replace
(
' month ago'
,
`mo
${
suffix
}
`
)
.
replace
(
' months ago'
,
`mo
${
suffix
}
`
)
.
replace
(
' year ago'
,
`y
${
suffix
}
`
)
.
replace
(
' years ago'
,
`y
${
suffix
}
`
)
;
return
convertToShortTime
;
}
;
/**
* Formats a duration in seconds into mm:ss or hh:mm:ss.
*
@param
{
number|string
} durationInSeconds - Duration in seconds.
*
@returns
{
string
} Formatted duration string. Empty string for invalid input.
*/
export
const
formatDuration
=
durationInSeconds
=>
{
if
(
durationInSeconds
===
null
||
durationInSeconds
===
undefined
)
return
''
;
const
totalSeconds
=
Number
(
durationInSeconds
)
;
if
(
Number
.
isNaN
(
totalSeconds
)
||
totalSeconds
<
0
)
return
''
;
const
hours
=
Math
.
floor
(
totalSeconds
/
3600
)
;
const
minutes
=
Math
.
floor
(
(
totalSeconds
%
3600
)
/
60
)
;
const
seconds
=
totalSeconds
%
60
;
const
mm
=
minutes
.
toString
(
)
.
padStart
(
2
,
'0'
)
;
const
ss
=
seconds
.
toString
(
)
.
padStart
(
2
,
'0'
)
;
if
(
hours
>
0
)
{
return
`
${
hours
.
toString
(
)
.
padStart
(
2
,
'0'
)
}
:
${
mm
}
:
${
ss
}
`
;
}
return
`
${
mm
}
:
${
ss
}
`
;
}
;
/**
* Calculates the difference in days between now and a given timestamp.
*
@param
{
Date
} now - Current date/time.
*
@param
{
number
} timestampInSeconds - Unix timestamp in seconds.
*
@returns
{
number
} Number of days difference.
*/
export
const
getDayDifferenceFromNow
=
(
now
,
timestampInSeconds
)
=>
{
const
date
=
new
Date
(
timestampInSeconds
*
1000
)
;
return
differenceInDays
(
now
,
date
)
;
}
;
/**
* Checks if more than 24 hours have passed since a given timestamp.
* Useful for determining if retry/refresh actions should be disabled.
*
@param
{
number
} timestamp - Unix timestamp.
*
@returns
{
boolean
} True if more than 24 hours have passed.
*/
export
const
hasOneDayPassed
=
timestamp
=>
{
if
(
!
timestamp
)
return
true
;
// Defensive check
return
getDayDifferenceFromNow
(
new
Date
(
)
,
timestamp
)
>=
1
;
}
;
Back
|
FazBrowse Home
|
New Git URL