summaryrefslogtreecommitdiffstats
path: root/src/components/organisms/layout/comment.tsx
blob: f4b822a37c14b3c89f4d6a9868539e3e999a5fd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import Button from '@components/atoms/buttons/button';
import Link from '@components/atoms/links/link';
import DescriptionList from '@components/atoms/lists/description-list';
import { getFormattedDate, getFormattedTime } from '@utils/helpers/format';
import Image from 'next/image';
import { FC, useState } from 'react';
import { useIntl } from 'react-intl';
import CommentForm, { type CommentFormProps } from '../forms/comment-form';
import styles from './comment.module.scss';

export type CommentAuthor = {
  /**
   * The author avatar.
   */
  avatar: string;
  /**
   * The author name.
   */
  name: string;
  /**
   * The author website.
   */
  url?: string;
};

export type CommentProps = {
  /**
   * The comment author data.
   */
  author: CommentAuthor;
  /**
   * Enable or disable the reply button. Default: true.
   */
  canReply?: boolean;
  /**
   * The comment body.
   */
  content: string;
  /**
   * The comment id.
   */
  id: number | string;
  /**
   * The post id.
   */
  postId: number | string;
  /**
   * The comment date.
   */
  publication: string;
  /**
   * A callback function to save comment form data.
   */
  saveComment: CommentFormProps['saveComment'];
};

/**
 * Comment component
 *
 * Render a single comment.
 */
const Comment: FC<CommentProps> = ({
  author,
  canReply = true,
  content,
  id,
  publication,
  saveComment,
  ...props
}) => {
  const intl = useIntl();
  const [isReplying, setIsReplying] = useState<boolean>(false);
  const commentDate = getFormattedDate(publication);
  const commentTime = getFormattedTime(publication);
  const commentDateTime = new Date(publication).toISOString();

  const avatarAltText = intl.formatMessage(
    {
      defaultMessage: '{author} avatar',
      description: 'Comment: avatar alternative text',
      id: 'T/hUiO',
    },
    { author: author.name }
  );
  const buttonLabel = isReplying
    ? intl.formatMessage({
        defaultMessage: 'Cancel reply',
        description: 'Comment: cancel reply button',
        id: 'LCorTC',
      })
    : intl.formatMessage({
        defaultMessage: 'Reply',
        description: 'Comment: reply button',
        id: 'hzHuCc',
      });
  const dateLabel = intl.formatMessage({
    defaultMessage: 'Published on:',
    description: 'Comment: publication date label',
    id: 'soj7do',
  });
  const dateValue = intl.formatMessage(
    {
      defaultMessage: '{date} at {time}',
      description: 'Comment: publication date and time',
      id: 'Ld6yMP',
    },
    {
      date: commentDate,
      time: commentTime,
    }
  );
  const formTitle = intl.formatMessage({
    defaultMessage: 'Leave a reply',
    description: 'Comment: comment form title',
    id: '2fD5CI',
  });

  const dateLink = (
    <Link href={`#comment-${id}`}>
      <time dateTime={commentDateTime}></time>
      {dateValue}
    </Link>
  );

  return (
    <>
      <article
        id={`comment-${id}`}
        className={`${styles.wrapper} ${styles['wrapper--comment']}`}
      >
        <header className={styles.header}>
          <div className={styles.avatar}>
            <Image
              src={author.avatar}
              alt={avatarAltText}
              layout="fill"
              objectFit="cover"
              {...props}
            />
          </div>
          {author.url ? (
            <Link href={author.url} className={styles.author}>
              {author.name}
            </Link>
          ) : (
            <span className={styles.author}>{author.name}</span>
          )}
        </header>
        <DescriptionList
          items={[{ id: 'comment-date', term: dateLabel, value: [dateLink] }]}
          layout="inline"
          className={styles.date}
          groupClassName={styles.meta}
        />
        <div className={styles.body}>{content}</div>
        <footer className={styles.footer}>
          {canReply && (
            <Button kind="tertiary" onClick={() => setIsReplying(!isReplying)}>
              {buttonLabel}
            </Button>
          )}
        </footer>
      </article>
      {isReplying && (
        <CommentForm
          saveComment={saveComment}
          title={formTitle}
          className={`${styles.wrapper} ${styles['wrapper--form']}`}
        />
      )}
    </>
  );
};

export default Comment;