export const PERSONAL_LINKS = { GITHUB: 'https://github.com/ArmandPhilippot', GITLAB: 'https://gitlab.com/ArmandPhilippot', LINKEDIN: 'https://www.linkedin.com/in/armandphilippot', SHAARLI: 'https://shaarli.armandphilippot.com/', } as const; /** * App routes. * * All static routes should be configured here to avoid 404 if a route changes. */ export const ROUTES = { ARTICLE: '/article', BLOG: '/blog', CONTACT: '/contact', CV: '/cv', HOME: '/', LEGAL_NOTICE: '/mentions-legales', NOT_FOUND: '/404', PROJECTS: '/projets', RSS: '/feed', SEARCH: '/recherche', THEMATICS: { INDEX: '/thematique', FREE: '/thematique/libre', LINUX: '/thematique/linux', WEB_DEV: '/thematique/developpement-web', }, TOPICS: '/sujet', } as const; // cSpell:ignore legales thematique developpement export const STORAGE_KEY = { ACKEE: 'ackee-tracking', MOTION: 'reduced-motion', PRISM: 'prismjs-color-scheme', THEME: 'theme', } as const; export const PRISM_THEME_ATTRIBUTE = 'data-prismjs-color-scheme-current'; export const VALID_THEMES = ['dark', 'light', 'system'] as const; selected='selected'>main The frontend of my personal website.Armand Philippot
aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/layout/comment.tsx
blob: db7cb3ae9fb6abc348269849d2c7bdeaa51cd999 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* eslint-disable max-statements */
import NextImage from 'next/image';
import Script from 'next/script';
import { type FC, useCallback, useState } from 'react';
import { useIntl } from 'react-intl';
import type { Comment as CommentSchema, WithContext } from 'schema-dts';
import type { SingleComment } from '../../../types';
import { useSettings } from '../../../utils/hooks';
import { Button, Link, Time } from '../../atoms';
import {
  Card,
  CardActions,
  CardBody,
  CardCover,
  CardFooter,
  CardHeader,
  CardMeta,
  CardTitle,
} from '../../molecules';
import { CommentForm, type CommentFormProps } from '../forms';
import styles from './comment.module.scss';

export type UserCommentProps = Pick<
  SingleComment,
  'approved' | 'content' | 'id' | 'meta' | 'parentId'
> &
  Pick<CommentFormProps, 'Notice' | 'saveComment'> & {
    /**
     * Enable or disable the reply button. Default: true.
     */
    canReply?: boolean;
  };

/**
 * UserComment component
 *
 * Render a single comment.
 */
export const UserComment: FC<UserCommentProps> = ({
  approved,
  canReply = true,
  content,
  id,
  meta,
  Notice,
  parentId,
  saveComment,
  ...props
}) => {
  const intl = useIntl();
  const { website } = useSettings();
  const [isReplying, setIsReplying] = useState<boolean>(false);

  const handleReply = useCallback(
    () => setIsReplying((prevState) => !prevState),
    []
  );

  if (!approved) {
    return (
      <div className={styles.wrapper}>
        {intl.formatMessage({
          defaultMessage: 'This comment is awaiting moderation...',
          description: 'Comment: awaiting moderation',
          id: '6a1Uo6',
        })}
      </div>
    );
  }

  const { author, date } = meta;

  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 formTitle = intl.formatMessage({
    defaultMessage: 'Leave a reply',
    description: 'Comment: comment form title',
    id: '2fD5CI',
  });

  const commentSchema: WithContext<CommentSchema> = {
    '@context': 'https://schema.org',
    '@id': `${website.url}/#comment-${id}`,
    '@type': 'Comment',
    parentItem: parentId
      ? { '@id': `${website.url}/#comment-${parentId}` }
      : undefined,
    about: { '@type': 'Article', '@id': `${website.url}/#article` },
    author: {
      '@type': 'Person',
      name: author.name,
      image: author.avatar?.src,
      url: author.website,
    },
    creator: {
      '@type': 'Person',
      name: author.name,
      image: author.avatar?.src,
      url: author.website,
    },
    dateCreated: date,
    datePublished: date,
    text: content,
  };

  return (
    <>
      <Script
        dangerouslySetInnerHTML={{ __html: JSON.stringify(commentSchema) }}
        id="schema-comments"
        type="application/ld+json"
      />
      <Card
        cover={
          author.avatar ? (
            <CardCover className={styles.avatar}>
              <NextImage
                {...props}
                alt={author.avatar.alt}
                fill
                src={author.avatar.src}
                style={{ objectFit: 'cover' }}
              />
            </CardCover>
          ) : undefined
        }
        id={`comment-${id}`}
        variant={2}
      >
        <CardHeader>
          <CardTitle className={styles.author} isFake>
            {author.website ? (
              <Link href={author.website}>{author.name}</Link>
            ) : (
              author.name
            )}
          </CardTitle>
          <CardMeta
            hasInlinedItems
            items={[
              {
                id: 'publication-date',
                label: intl.formatMessage({
                  defaultMessage: 'Published on:',
                  description: 'Comment: publication date label',
                  id: 'soj7do',
                }),
                value: (
                  <Link href={`#comment-${id}`}>
                    <Time date={date} showTime />
                  </Link>
                ),
              },
            ]}
          />
        </CardHeader>
        <CardBody
          className={styles.body}
          dangerouslySetInnerHTML={{ __html: content }}
        />
        {canReply ? (
          <CardFooter>
            <CardActions>
              <Button kind="tertiary" onClick={handleReply}>
                {buttonLabel}
              </Button>
            </CardActions>
          </CardFooter>
        ) : null}
      </Card>
      {isReplying ? (
        <Card className={styles.form} variant={2}>
          <CardBody>
            <CommentForm
              Notice={Notice}
              parentId={id}
              saveComment={saveComment}
              title={formTitle}
            />
          </CardBody>
        </Card>
      ) : null}
    </>
  );
};