import { _ } from '@evershop/evershop/lib/locale/translate/_';
import axios from 'axios';
import React from 'react';
export interface CommentData {
uuid: string;
name: string;
comment: string;
createdAt?: string | null;
likeCount: number;
liked: boolean;
replies?: CommentData[];
}
/** Plain text (sanitized server-side) rendered as text nodes; preserve breaks. */
function CommentText({ text }: { text: string }) {
const lines = text.split('\n');
return (
<>
{lines.map((line, i) => (
{line}
{i < lines.length - 1 &&
}
))}
>
);
}
function CommentItem({
comment,
depth,
onReply
}: {
comment: CommentData;
depth: number;
onReply: (c: CommentData) => void;
}) {
const [likeCount, setLikeCount] = React.useState(comment.likeCount);
const [liked, setLiked] = React.useState(comment.liked);
const [busy, setBusy] = React.useState(false);
const like = async () => {
if (busy) return;
setBusy(true);
try {
const res = await axios.post(`/api/blog/comments/${comment.uuid}/like`);
const data = res.data?.data;
if (data) {
setLikeCount(data.likeCount);
setLiked(data.liked);
}
} finally {
setBusy(false);
}
};
return (
0 ? 'ml-6 mt-4 border-l border-border pl-4' : 'mt-6'}>
{comment.name}
{comment.createdAt && (
{new Date(comment.createdAt).toLocaleDateString()}
)}
{depth < 2 && (
)}
{comment.replies?.map((reply) => (
))}
);
}
function countComments(list: CommentData[]): number {
return list.reduce(
(n, c) => n + 1 + countComments(c.replies || []),
0
);
}
export function CommentSection({
postUuid,
comments = [],
commentPolicy
}: {
postUuid: string;
comments?: CommentData[];
commentPolicy?: string;
}) {
const [replyTo, setReplyTo] = React.useState(null);
const [form, setForm] = React.useState({
name: '',
email: '',
comment: '',
website: ''
});
const [message, setMessage] = React.useState<{
type: 'success' | 'error';
text: string;
} | null>(null);
const [submitting, setSubmitting] = React.useState(false);
const total = countComments(comments);
const closed = commentPolicy === 'closed';
// When comments are closed, hide the whole section (heading, list, and form)
// — no storefront note.
if (closed) {
return null;
}
const submit = async (e: React.FormEvent) => {
e.preventDefault();
setSubmitting(true);
setMessage(null);
try {
const res = await axios.post('/api/blog/comments', {
post_uuid: postUuid,
parent_uuid: replyTo?.uuid || null,
name: form.name,
email: form.email,
comment: form.comment,
website: form.website
});
const data = res.data?.data;
if (data?.status === 'approved') {
window.location.reload();
return;
}
setMessage({
type: 'success',
text: _('Thanks! Your comment is awaiting moderation.')
});
setForm({ name: '', email: '', comment: '', website: '' });
setReplyTo(null);
} catch (err: any) {
setMessage({
type: 'error',
text:
err?.response?.data?.error?.message ||
_('Something went wrong. Please try again.')
});
} finally {
setSubmitting(false);
}
};
return (
{_('Comments')} ({total})
{comments.length === 0 && (
{_('No comments yet. Be the first!')}
)}
{comments.map((c) => (
))}
);
}