6 changed files with 40 additions and 35 deletions
@ -113,9 +113,6 @@ importers: |
|||
rfc4648: |
|||
specifier: ^1.5.4 |
|||
version: 1.5.4 |
|||
timeago-react: |
|||
specifier: ^3.0.6 |
|||
version: 3.0.6([email protected]) |
|||
vite-plugin-node-polyfills: |
|||
specifier: ^0.23.0 |
|||
version: 0.23.0([email protected])([email protected](@types/[email protected])) |
|||
@ -2817,14 +2814,6 @@ packages: |
|||
[email protected]: |
|||
resolution: {integrity: sha512-iOqSav00cVxEEICeD7TjLB1sueEL+81Wpzp2bY17uZjZN0pWZPuo4suZ/61VujxmqSGFfgOcNuTZ85QJwNZQpw==} |
|||
|
|||
[email protected]: |
|||
resolution: {integrity: sha512-4ywnCX3iFjdp84WPK7gt8s4n0FxXbYM+xv8hYL73p83dpcMxzmO+0W4xJuxflnkWNvum5aEaqTe6LZ3lUIudjQ==} |
|||
peerDependencies: |
|||
react: ^0.14.0 || ^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0 |
|||
|
|||
[email protected]: |
|||
resolution: {integrity: sha512-a7wPxPdVlQL7lqvitHGGRsofhdwtkoSXPGATFuSOA2i1ZNQEPLrGnj68vOp2sOJTCFAQVXPeNMX/GctBaO9L2w==} |
|||
|
|||
[email protected]: |
|||
resolution: {integrity: sha512-9phl76Cqm6FhSX9Xe1ZUAMLtm1BLkKj2Qd5ApyWkXzsMRaA7dgr81kf4wJmQf/hAvg8EEyJxDo3du/0KlhPiKQ==} |
|||
engines: {node: '>=0.6.0'} |
|||
@ -6498,13 +6487,6 @@ snapshots: |
|||
dependencies: |
|||
readable-stream: 3.6.2 |
|||
|
|||
[email protected]([email protected]): |
|||
dependencies: |
|||
react: 19.0.0 |
|||
timeago.js: 4.0.2 |
|||
|
|||
[email protected]: {} |
|||
|
|||
[email protected]: |
|||
dependencies: |
|||
setimmediate: 1.0.5 |
|||
|
|||
@ -1,9 +0,0 @@ |
|||
import TimeAgoReact from "timeago-react"; |
|||
|
|||
export interface TimeAgoProps { |
|||
timestamp: number; |
|||
} |
|||
|
|||
export const TimeAgo = ({ timestamp }: TimeAgoProps): JSX.Element => { |
|||
return <TimeAgoReact datetime={timestamp} opts={{ minInterval: 10 }} />; |
|||
}; |
|||
@ -0,0 +1,34 @@ |
|||
import { type JSX, useEffect, useState } from "react"; |
|||
|
|||
export interface TimeAgoProps { |
|||
timestamp: number; |
|||
} |
|||
|
|||
const getTimeAgo = (timestamp: number): string => { |
|||
const now = Date.now(); |
|||
const secondsPast = Math.floor((now - timestamp) / 1000); |
|||
|
|||
if (secondsPast < 10) return "now"; |
|||
if (secondsPast < 60) return `${secondsPast} seconds ago`; |
|||
if (secondsPast < 3600) return `${Math.floor(secondsPast / 60)} minutes ago`; |
|||
if (secondsPast < 86400) return `${Math.floor(secondsPast / 3600)} hours ago`; |
|||
return `${Math.floor(secondsPast / 86400)} days ago`; |
|||
}; |
|||
|
|||
export const TimeAgo = ({ timestamp }: TimeAgoProps): JSX.Element => { |
|||
const [timeAgo, setTimeAgo] = useState(getTimeAgo(timestamp)); |
|||
|
|||
useEffect(() => { |
|||
const interval = setInterval(() => { |
|||
setTimeAgo(getTimeAgo(timestamp)); |
|||
}, 10000); |
|||
|
|||
return () => clearInterval(interval); |
|||
}, [timestamp]); |
|||
|
|||
useEffect(() => { |
|||
setTimeAgo(getTimeAgo(timestamp)); |
|||
}, [timestamp]); |
|||
|
|||
return <span>{timeAgo}</span>; |
|||
}; |
|||
Loading…
Reference in new issue