Skip to content

Commit

Permalink
replace timeAgo function with moment.js
Browse files Browse the repository at this point in the history
- already a dependency anyways
- we might want to replace that with timeago.js because the library is
  smaller
  • Loading branch information
JohannesNakayama authored and mergify[bot] committed Oct 18, 2024
1 parent e5775d9 commit 51d9592
Showing 1 changed file with 3 additions and 25 deletions.
28 changes: 3 additions & 25 deletions app/integrations/hn.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type OEmbedResponse,
errorResponse,
} from './integrations.common.ts'
import moment from 'moment'

interface HackerNewsItem {
by?: string
Expand All @@ -21,29 +22,6 @@ interface HackerNewsItem {
[key: string]: any
}

// Utility function to get relative time
function timeAgo(timestamp: number): string {
const now = Date.now()
const secondsPast = (now - timestamp * 1000) / 1000

if (secondsPast < 60) {
return `${Math.floor(secondsPast)} seconds ago`
}
if (secondsPast < 3600) {
return `${Math.floor(secondsPast / 60)} minutes ago`
}
if (secondsPast < 86400) {
return `${Math.floor(secondsPast / 3600)} hours ago`
}
if (secondsPast < 2592000) {
return `${Math.floor(secondsPast / 86400)} days ago`
}
if (secondsPast < 31104000) {
return `${Math.floor(secondsPast / 2592000)} months ago`
}
return `${Math.floor(secondsPast / 31104000)} years ago`
}

async function getCommentThread(
itemId: number,
): Promise<{ thread: HackerNewsItem[]; storyId: number; isStory: boolean }> {
Expand Down Expand Up @@ -103,7 +81,7 @@ function generateStoryHTML(story: HackerNewsItem): string {
const title = sanitizeHtml(story.title || '')
const url = story.url || ''
const author = story.by || '[deleted]'
const time = story.time ? timeAgo(story.time) : ''
const time = story.time ? moment(story.time * 1000).fromNow() : ''
const score = story.score || 0
const descendants = story.descendants || 0
const storyId = story.id || ''
Expand Down Expand Up @@ -162,7 +140,7 @@ function generateThreadHTML(
},
})

const relativeTime = comment.time ? timeAgo(comment.time) : ''
const relativeTime = comment.time ? moment(comment.time * 1000).fromNow : ''

const indent = index * 20 // Adjust indentation per level

Expand Down

0 comments on commit 51d9592

Please sign in to comment.