summaryrefslogtreecommitdiffstats
path: root/src/components/PostHeader/PostHeader.tsx
blob: 3ee6705e94491e0c2ae2b220e2ac78550b9f9bbd (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
import { t } from '@lingui/macro';
import { Dates } from '@ts/types/app';
import { ArticleAuthor } from '@ts/types/articles';
import { ThematicPreview } from '@ts/types/taxonomies';
import Link from 'next/link';
import { useRouter } from 'next/router';
import styles from './PostHeader.module.scss';

const PostHeader = ({
  author,
  dates,
  intro,
  title,
  thematics,
}: {
  author: ArticleAuthor;
  dates: Dates;
  intro: string;
  title: string;
  thematics: ThematicPreview[];
}) => {
  const { locale } = useRouter();

  const getAuthor = () => {
    return author.firstName
      ? `${author.firstName} ${author.lastName}`
      : author.name;
  };

  const getLocaleDate = (date: string) => {
    const dateOptions: Intl.DateTimeFormatOptions = {
      day: 'numeric',
      month: 'long',
      year: 'numeric',
    };
    return new Date(date).toLocaleDateString(locale, dateOptions);
  };

  const getThematics = () => {
    return thematics.map((thematic) => {
      return (
        <dd key={thematic.id}>
          <Link href={`/thematique/${thematic.slug}`}>
            <a>{thematic.title}</a>
          </Link>
        </dd>
      );
    });
  };

  return (
    <header>
      <h1>{title}</h1>
      <ul className={styles.meta}>
        <li>{t`Written by ${getAuthor()} on ${getLocaleDate(
          dates.publication
        )}.`}</li>
        <li>{t`Last update on ${getLocaleDate(dates.update)}.`}</li>
        {thematics.length > 0 && (
          <li>
            <dl>
              <dt className={styles.label}>{t`Posted in:`}</dt>
              {getThematics()}
            </dl>
          </li>
        )}
      </ul>
      <div dangerouslySetInnerHTML={{ __html: intro }}></div>
    </header>
  );
};

export default PostHeader;