2022-03-30 23:01:24 -04:00
|
|
|
import { Badge, Tooltip } from "@geist-ui/core";
|
|
|
|
import { timeUntil } from "@lib/time-ago";
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
|
|
|
|
|
|
const ExpirationBadge = ({
|
|
|
|
postExpirationDate,
|
2022-03-31 02:30:01 -04:00
|
|
|
// onExpires
|
2022-03-30 23:01:24 -04:00
|
|
|
}: {
|
|
|
|
postExpirationDate: Date | string | null
|
|
|
|
onExpires?: () => void
|
|
|
|
}) => {
|
|
|
|
const expirationDate = useMemo(() => postExpirationDate ? new Date(postExpirationDate) : null, [postExpirationDate])
|
|
|
|
const [timeUntilString, setTimeUntil] = useState<string | null>(expirationDate ? timeUntil(expirationDate) : null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let interval: NodeJS.Timer | null = null;
|
|
|
|
if (expirationDate) {
|
|
|
|
interval = setInterval(() => {
|
|
|
|
if (expirationDate) {
|
|
|
|
setTimeUntil(timeUntil(expirationDate))
|
|
|
|
}
|
|
|
|
}, 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [expirationDate])
|
|
|
|
|
|
|
|
const isExpired = useMemo(() => {
|
2022-03-30 23:52:30 -04:00
|
|
|
return timeUntilString && timeUntilString === "in 0 seconds"
|
|
|
|
}, [timeUntilString])
|
2022-03-30 23:01:24 -04:00
|
|
|
|
2022-03-31 02:30:01 -04:00
|
|
|
// useEffect(() => {
|
|
|
|
// // check if expired every
|
|
|
|
// if (isExpired) {
|
|
|
|
// if (onExpires) {
|
|
|
|
// onExpires();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }, [isExpired, onExpires])
|
2022-03-30 23:01:24 -04:00
|
|
|
|
|
|
|
if (!expirationDate) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Badge type={isExpired ? "error" : "warning"}>
|
|
|
|
<Tooltip
|
2022-04-05 19:17:08 -04:00
|
|
|
hideArrow
|
2022-03-30 23:01:24 -04:00
|
|
|
text={`${expirationDate.toLocaleDateString()} ${expirationDate.toLocaleTimeString()}`}>
|
|
|
|
{isExpired ? "Expired" : `Expires ${timeUntilString}`}
|
|
|
|
</Tooltip>
|
|
|
|
</Badge>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ExpirationBadge
|