From 37bc9d25deecb04b7970881d46551d5b33fe88df Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Fri, 17 Dec 2021 19:21:11 +0100
Subject: chore: add meta to single posts

---
 src/components/PostHeader/PostHeader.tsx | 72 ++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100644 src/components/PostHeader/PostHeader.tsx

(limited to 'src/components/PostHeader/PostHeader.tsx')

diff --git a/src/components/PostHeader/PostHeader.tsx b/src/components/PostHeader/PostHeader.tsx
new file mode 100644
index 0000000..5c5aff4
--- /dev/null
+++ b/src/components/PostHeader/PostHeader.tsx
@@ -0,0 +1,72 @@
+import { t } from '@lingui/macro';
+import { ArticleAuthor, ArticleDates } 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,
+  date,
+  intro,
+  title,
+  thematics,
+}: {
+  author: ArticleAuthor;
+  date: ArticleDates;
+  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(
+          date.publication
+        )}.`}</li>
+        <li>{t`Last update on ${getLocaleDate(date.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;
-- 
cgit v1.2.3

