import { Button } from '@components/Buttons'; import CommentForm from '@components/CommentForm/CommentForm'; import { config } from '@config/website'; import { t } from '@lingui/macro'; import { Comment as CommentData } from '@ts/types/comments'; import Head from 'next/head'; import Image from 'next/image'; import Link from 'next/link'; import { useRouter } from 'next/router'; import { useEffect, useRef, useState } from 'react'; import { Comment as CommentSchema, WithContext } from 'schema-dts'; import styles from './Comment.module.scss'; const Comment = ({ articleId, comment, isNested = false, }: { articleId: number; comment: CommentData; isNested?: boolean; }) => { const router = useRouter(); const [isReply, setIsReply] = useState(false); const firstFieldRef = useRef(null); useEffect(() => { firstFieldRef.current && firstFieldRef.current.focus(); }); const getCommentAuthor = () => { return comment.author.url ? ( {comment.author.name} ) : ( {comment.author.name} ); }; const getLocaleDate = () => { const commentDate = new Date(comment.date); const date = commentDate.toLocaleDateString(router.locale, { year: 'numeric', month: 'long', day: 'numeric', }); const time = commentDate .toLocaleTimeString(router.locale, { hour: 'numeric', minute: 'numeric', }) .replace(':', 'h'); return t`${date} at ${time}`; }; const getApprovedComment = () => { return ( <>
{comment.author.gravatarUrl && (
{comment.author.name}
)} {getCommentAuthor()}
{t`Published on:`}
{getLocaleDate()}
{!isNested && (
)}
{isReply && ( )} {comment.replies.length > 0 && (
    {comment.replies.map((reply) => { return ( ); })}
)} ); }; const getCommentStatus = () => { return

{t`This comment is awaiting moderation.`}

; }; const schemaJsonLd: WithContext = { '@context': 'https://schema.org', '@id': `${config.url}/#comment-${comment.commentId}`, '@type': 'Comment', parentItem: isNested ? { '@id': `${config.url}/#comment-${comment.parentDatabaseId}` } : undefined, about: { '@type': 'Article', '@id': `${config.url}/#article` }, author: { '@type': 'Person', name: comment.author.name, image: comment.author.gravatarUrl, url: comment.author.url, }, creator: { '@type': 'Person', name: comment.author.name, image: comment.author.gravatarUrl, url: comment.author.url, }, dateCreated: comment.date, datePublished: comment.date, text: comment.content, }; return ( <>
  • {comment.approved ? getApprovedComment() : getCommentStatus()}
  • ); }; export default Comment;