aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/comment/approved-comment/approved-comment.tsx
blob: 233146d34c58a42c1a3e6dfb36f4237d444063cc (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 NextImage from 'next/image';
import { type ForwardRefRenderFunction, forwardRef, useCallback } from 'react';
import { useIntl } from 'react-intl';
import { Button, Link, Time } from '../../../atoms';
import {
  Card,
  CardBody,
  CardCover,
  CardHeader,
  CardMeta,
  type CardProps,
  CardTitle,
  CardFooter,
  CardActions,
  MetaItem,
} from '../../../molecules';
import styles from './approved-comment.module.scss';

export type CommentAuthorAvatar = {
  /**
   * The alternative text for the avatar.
   */
  alt: string;
  /**
   * The avatar url.
   */
  src: string;
};

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

export type CommentReplyHandler = (id: number) => void | Promise<void>;

export type ApprovedCommentProps = Omit<
  CardProps<undefined>,
  | 'children'
  | 'content'
  | 'cover'
  | 'id'
  | 'isCentered'
  | 'linkTo'
  | 'meta'
  | 'variant'
> & {
  /**
   * The author data.
   */
  author: CommentAuthor;
  /**
   * The comment.
   */
  content: string;
  /**
   * The comment id.
   */
  id: number;
  /**
   * A callback function to handle reply.
   */
  onReply?: CommentReplyHandler;
  /**
   * The publication date of the comment.
   */
  publicationDate: string;
  /**
   * Add a reply button to the comment by providing a label.
   */
  replyBtn?: string;
};

const ApprovedCommentWithRef: ForwardRefRenderFunction<
  HTMLDivElement,
  ApprovedCommentProps
> = (
  {
    author,
    className = '',
    content,
    id,
    onReply,
    publicationDate,
    replyBtn,
    ...props
  },
  ref
) => {
  const intl = useIntl();
  const commentClass = `${className}`;
  const commentId = `comment-${id}`;
  const commentLink = `#${commentId}`;
  const publicationDateLabel = intl.formatMessage({
    defaultMessage: 'Published on:',
    description: 'ApprovedComment: publication date label',
    id: 'NzeU3V',
  });

  const handleReply = useCallback(() => {
    if (onReply) onReply(id);
  }, [id, onReply]);

  return (
    <Card
      {...props}
      className={commentClass}
      cover={
        author.avatar ? (
          <CardCover hasBorders>
            <NextImage
              alt={author.avatar.alt}
              height={96}
              src={author.avatar.src}
              width={96}
            />
          </CardCover>
        ) : undefined
      }
      id={commentId}
      ref={ref}
      variant={2}
    >
      <CardHeader>
        <CardTitle className={styles.author} isFake>
          {author.website ? (
            <Link href={author.website}>{author.name}</Link>
          ) : (
            author.name
          )}
        </CardTitle>
        <CardMeta>
          <MetaItem
            isInline
            label={publicationDateLabel}
            value={
              <Link href={commentLink}>
                <Time date={publicationDate} showTime />
              </Link>
            }
          />
        </CardMeta>
      </CardHeader>
      <CardBody
        className={styles.body}
        dangerouslySetInnerHTML={{ __html: content }}
      />
      {replyBtn ? (
        <CardFooter>
          <CardActions>
            <Button
              // eslint-disable-next-line react/jsx-no-literals
              kind="tertiary"
              onClick={handleReply}
            >
              {replyBtn}
            </Button>
          </CardActions>
        </CardFooter>
      ) : null}
    </Card>
  );
};

export const ApprovedComment = forwardRef(ApprovedCommentWithRef);