aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/layout/summary.tsx
blob: f5c16cd8cdfb1a6ce03b746ecd7b14e5b2a1f6bf (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import NextImage, { type ImageProps as NextImageProps } from 'next/image';
import type { FC, ReactNode } from 'react';
import { useIntl } from 'react-intl';
import type { Article, Meta as MetaType } from '../../../types';
import { getFormattedDate } from '../../../utils/helpers';
import { useReadingTime } from '../../../utils/hooks';
import {
  ButtonLink,
  Heading,
  type HeadingLevel,
  Icon,
  Link,
  Figure,
} from '../../atoms';
import { MetaList, type MetaItemData } from '../../molecules';
import styles from './summary.module.scss';

export type Cover = Pick<NextImageProps, 'alt' | 'src' | 'width' | 'height'>;

export type SummaryMeta = Pick<
  MetaType<'article'>,
  | 'author'
  | 'commentsCount'
  | 'cover'
  | 'dates'
  | 'thematics'
  | 'topics'
  | 'wordsCount'
>;

export type SummaryProps = Pick<Article, 'intro' | 'title'> & {
  /**
   * The post metadata.
   */
  meta: SummaryMeta;
  /**
   * The heading level (hn).
   */
  titleLevel?: HeadingLevel;
  /**
   * The post url.
   */
  url: string;
};

/**
 * Summary component
 *
 * Render a page summary.
 */
export const Summary: FC<SummaryProps> = ({
  intro,
  meta,
  title,
  titleLevel = 2,
  url,
}) => {
  const intl = useIntl();
  const readMore = intl.formatMessage(
    {
      defaultMessage: 'Read more<a11y> about {title}</a11y>',
      description: 'Summary: read more link',
      id: 'Zpgv+f',
    },
    {
      title,
      a11y: (chunks: ReactNode) => (
        // eslint-disable-next-line react/jsx-no-literals -- SR class allowed
        <span className="screen-reader-text">{chunks}</span>
      ),
    }
  );
  const readingTime = useReadingTime(meta.wordsCount, true);

  /**
   * Retrieve a formatted date (and time).
   *
   * @param {string} date - A date string.
   * @returns {JSX.Element} The formatted date wrapped in a time element.
   */
  const getDate = (date: string): JSX.Element => {
    const isoDate = new Date(`${date}`).toISOString();

    return <time dateTime={isoDate}>{getFormattedDate(date)}</time>;
  };

  const getMetaItems = (): MetaItemData[] => {
    const summaryMeta: MetaItemData[] = [
      {
        id: 'publication-date',
        label: intl.formatMessage({
          defaultMessage: 'Published on:',
          description: 'Summary: publication date label',
          id: 'TvQ2Ee',
        }),
        value: getDate(meta.dates.publication),
      },
    ];

    if (meta.dates.update && meta.dates.update !== meta.dates.publication)
      summaryMeta.push({
        id: 'update-date',
        label: intl.formatMessage({
          defaultMessage: 'Updated on:',
          description: 'Summary: update date label',
          id: 'f0Z/Po',
        }),
        value: getDate(meta.dates.update),
      });

    summaryMeta.push({
      id: 'reading-time',
      label: intl.formatMessage({
        defaultMessage: 'Reading time:',
        description: 'Summary: reading time label',
        id: 'tyzdql',
      }),
      value: readingTime,
    });

    if (meta.author)
      summaryMeta.push({
        id: 'author',
        label: intl.formatMessage({
          defaultMessage: 'Written by:',
          description: 'Summary: author label',
          id: 'r/6HOI',
        }),
        value: meta.author.name,
      });

    if (meta.thematics)
      summaryMeta.push({
        id: 'thematics',
        label: intl.formatMessage({
          defaultMessage: 'Thematics:',
          description: 'Summary: thematics label',
          id: 'bk0WOp',
        }),
        value: meta.thematics.map((thematic) => {
          return {
            id: `thematic-${thematic.id}`,
            value: <Link href={thematic.url}>{thematic.name}</Link>,
          };
        }),
      });

    if (meta.topics)
      summaryMeta.push({
        id: 'topics',
        label: intl.formatMessage({
          defaultMessage: 'Topics:',
          description: 'Summary: topics label',
          id: 'yIZ+AC',
        }),
        value: meta.topics.map((topic) => {
          return {
            id: `topic-${topic.id}`,
            value: <Link href={topic.url}>{topic.name}</Link>,
          };
        }),
      });

    if (meta.commentsCount !== undefined) {
      const commentsCount = intl.formatMessage(
        {
          defaultMessage:
            '{commentsCount, plural, =0 {No comments} one {# comment} other {# comments}}<a11y> about {title}</a11y>',
          description: 'Summary: comments count',
          id: 'ye/vlA',
        },
        {
          a11y: (chunks: ReactNode) => (
            <span className="screen-reader-text">{chunks}</span>
          ),
          commentsCount: meta.commentsCount,
          title,
        }
      );
      summaryMeta.push({
        id: 'comments-count',
        label: intl.formatMessage({
          defaultMessage: 'Comments:',
          description: 'Summary: comments label',
          id: 'bfPp0g',
        }),
        value: (
          <Link href={`${url}#comments`}>{commentsCount as JSX.Element}</Link>
        ),
      });
    }

    return summaryMeta;
  };

  return (
    <article className={styles.wrapper}>
      {meta.cover ? (
        <Figure>
          <NextImage {...meta.cover} className={styles.cover} />
        </Figure>
      ) : null}
      <header className={styles.header}>
        <Link href={url} className={styles.link}>
          <Heading level={titleLevel} className={styles.title}>
            {title}
          </Heading>
        </Link>
      </header>
      <div className={styles.body}>
        <div
          className={styles.intro}
          // eslint-disable-next-line react/no-danger
          dangerouslySetInnerHTML={{ __html: intro }}
        />
        <ButtonLink className={styles['read-more']} to={url}>
          {readMore}
          <Icon
            aria-hidden={true}
            className={styles.icon}
            // eslint-disable-next-line react/jsx-no-literals -- Direction allowed
            orientation="right"
            // eslint-disable-next-line react/jsx-no-literals -- Shape allowed
            shape="arrow"
          />
        </ButtonLink>
      </div>
      <footer className={styles.footer}>
        <MetaList className={styles.meta} items={getMetaItems()} />
      </footer>
    </article>
  );
};