Skip to content

Commit 4a60c5e

Browse files
implemented Relative time(posted ago) in comments metadata using moment.js
1 parent 7ac53eb commit 4a60c5e

File tree

3 files changed

+7
-24
lines changed

3 files changed

+7
-24
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"i18next": "^19.4.5",
1515
"lodash": "^4.17.21",
1616
"markdown-it": "^12.0.6",
17+
"moment": "^2.29.1",
1718
"node-sass": "^4.13.1",
1819
"react": "^16.13.1",
1920
"react-dom": "^16.13.1",
@@ -24,8 +25,8 @@
2425
"react-image-gallery": "^1.2.5",
2526
"react-loading-skeleton": "^2.2.0",
2627
"react-markdown": "^5.0.2",
27-
"react-mentions": "^4.3.0",
2828
"react-markdown-editor-lite": "^1.2.4",
29+
"react-mentions": "^4.3.0",
2930
"react-promise-tracker": "^2.1.0",
3031
"react-router-dom": "^5.2.0",
3132
"react-scripts": "3.4.1",

src/components/Comments.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState, useEffect, useContext, useCallback } from 'react'
22
import { Link } from '@reach/router'
33
import Button from './Button'
44
import Gallery from './ImageGallery'
5-
5+
import moment from 'moment'
66
import CommentForm from './CommentForm'
77
import Context from '../modules/Context'
88
import userStory from '../services/user_story'
@@ -31,23 +31,6 @@ const toggleViewReplies = (viewRepliesToggled, setViewRepliesToggled, key) => {
3131
)
3232
}
3333

34-
const intervals = [
35-
{ label: 'year', seconds: 31536000 },
36-
{ label: 'month', seconds: 2592000 },
37-
{ label: 'day', seconds: 86400 },
38-
{ label: 'hour', seconds: 3600 },
39-
{ label: 'minute', seconds: 60 },
40-
{ label: 'second', seconds: 1 }
41-
]
42-
43-
function timeSince(creationDate) {
44-
const date = new Date(creationDate)
45-
const seconds = Math.floor((Date.now() - date.getTime()) / 1000)
46-
const interval = intervals.find((i) => i.seconds < seconds)
47-
const count = Math.floor(seconds / interval.seconds)
48-
return `${count} ${interval.label}${count !== 1 ? 's' : ''} ago`
49-
}
50-
5134
const Comments = (props) => {
5235
const { storyId } = props
5336

@@ -158,7 +141,7 @@ const Comments = (props) => {
158141
{data.user.username}
159142
</Link>
160143
<div className='metadata'>
161-
<div>{timeSince(data.createdAt)}</div>
144+
<div>{moment(data.createdAt).fromNow()}</div>
162145
</div>
163146
<div dangerouslySetInnerHTML={{ __html: data.Comments }} />
164147
<div>

0 commit comments

Comments
 (0)