2022-11-30 01:10:51 -05:00
|
|
|
'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"
|
2022-11-14 02:02:31 -05:00
|
|
|
import Badge from "../badge"
|
2022-03-30 23:01:24 -04:00
|
|
|
|
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-03-30 23:01:24 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
useEffect(() => {
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
setTimeAgo(timeAgo(createdDate))
|
|
|
|
}, 1000)
|
|
|
|
return () => clearInterval(interval)
|
|
|
|
}, [createdDate])
|
2022-03-30 23:01:24 -04:00
|
|
|
|
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}>
|
2022-11-30 01:10:51 -05:00
|
|
|
<Badge type="secondary">
|
|
|
|
{" "}
|
2022-11-14 02:02:31 -05:00
|
|
|
<>{time}</>
|
2022-11-30 01:10:51 -05:00
|
|
|
</Badge>
|
2022-12-04 04:31:51 -05:00
|
|
|
</Tooltip>
|
2022-04-09 20:48:19 -04:00
|
|
|
)
|
2022-03-30 23:01:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default CreatedAgoBadge
|