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

31 lines
848 B
TypeScript
Raw Normal View History

'use client'
2022-12-04 04:31:51 -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-12-04 04:31:51 -05:00
const formattedTime = `${createdDate.toLocaleDateString()} ${createdDate.toLocaleTimeString()}`
2022-04-09 20:48:19 -04:00
return (
2022-12-04 04:31:51 -05:00
// TODO: investigate tooltip not showing
<Tooltip content={formattedTime}>
<Badge type="secondary">
{" "}
<>{time}</>
</Badge>
2022-12-04 04:31:51 -05:00
</Tooltip>
2022-04-09 20:48:19 -04:00
)
}
export default CreatedAgoBadge