Remove timeago-react

This commit is contained in:
Tilen Komel
2025-01-30 15:04:07 +01:00
parent b3cde1bcd7
commit 3f09e6de93
6 changed files with 40 additions and 35 deletions
@@ -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 }} />;
};
+34
View File
@@ -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>;
};