1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2026-06-05 20:15:31 -04:00
Files
jarv.is/components/comment-count.tsx
T
2026-02-19 14:12:42 -05:00

40 lines
980 B
TypeScript

"use client";
import { useEffect, useState } from "react";
import { getCommentCount } from "@/lib/server/comments";
const CommentCount = ({ slug }: { slug: string }) => {
const [count, setCount] = useState<number | null>(null);
const [error, setError] = useState(false);
useEffect(() => {
getCommentCount(slug)
.then((result: number) => {
setCount(result);
})
.catch((err: unknown) => {
console.error("[comment-count] error:", err);
setError(true);
});
}, [slug]);
if (error) {
return <span title="Error getting comments">?</span>;
}
if (count === null) {
return <span className="motion-safe:animate-pulse">0</span>;
}
return (
<span
title={`${Intl.NumberFormat(process.env.NEXT_PUBLIC_SITE_LOCALE).format(count)} ${count === 1 ? "comment" : "comments"}`}
>
{Intl.NumberFormat(process.env.NEXT_PUBLIC_SITE_LOCALE).format(count)}
</span>
);
};
export { CommentCount };