CoastalCommitsPastes/client/app/components/badges/created-ago-badge/index.tsx

29 lines
791 B
TypeScript
Raw Normal View History

2022-11-12 21:39:03 -05:00
import Tooltip from "@components/tooltip"
2022-04-09 20:48:19 -04:00
import { timeAgo } from "@lib/time-ago"
import { useMemo, useState, useEffect } from "react"
import Badge from "../badge"
2022-04-09 20:48:19 -04:00
const CreatedAgoBadge = ({ createdAt }: { createdAt: string | Date }) => {
const createdDate = useMemo(() => new Date(createdAt), [createdAt])
const [time, setTimeAgo] = useState(timeAgo(createdDate))
2022-04-09 20:48:19 -04:00
useEffect(() => {
const interval = setInterval(() => {
setTimeAgo(timeAgo(createdDate))
}, 1000)
return () => clearInterval(interval)
}, [createdDate])
2022-04-09 20:48:19 -04:00
const formattedTime = `${createdDate.toLocaleDateString()} ${createdDate.toLocaleTimeString()}`
return (
<Badge type="secondary">
{" "}
2022-11-12 21:39:03 -05:00
<Tooltip content={formattedTime}>
<>{time}</>
2022-04-09 20:48:19 -04:00
</Tooltip>
</Badge>
)
}
export default CreatedAgoBadge