aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/layout/summary.tsx
blob: fa3dfe5986652422c91a3377683a08d702f5edcc (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
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 { useReadingTime } from '../../../utils/hooks';
import {
  ButtonLink,
  Heading,
  type HeadingLevel,
  Icon,
  Link,
  Figure,
} from '../../atoms';
import { Meta, type MetaData } 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 { author, commentsCount, cover, dates, thematics, topics, wordsCount } =
    meta;
  const readingTime = useReadingTime(wordsCount, true);

  const getMeta = (): MetaData => {
    return {
      author: author?.name,
      publication: { date: dates.publication },
      update:
        dates.update && dates.publication !== dates.update
          ? { date: dates.update }
          : undefined,
      readingTime,
      thematics: thematics?.map((thematic) => (
        <Link key={thematic.id} href={thematic.url}>
          {thematic.name}
        </Link>
      )),
      topics: topics?.map((topic) => (
        <Link key={topic.id} href={topic.url}>
          {topic.name}
        </Link>
      )),
      comments: {
        about: title,
        count: commentsCount ?? 0,
        target: `${url}#comments`,
      },
    };
  };

  return (
    <article className={styles.wrapper}>
      {cover ? (
        <Figure>
          <NextImage {...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}>
        <Meta className={styles.meta} data={getMeta()} spacing="xs" />
      </footer>
    </article>
  );
};