diff options
| author | Armand Philippot <git@armandphilippot.com> | 2021-12-17 19:21:11 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2021-12-17 19:21:11 +0100 |
| commit | 37bc9d25deecb04b7970881d46551d5b33fe88df (patch) | |
| tree | 44e03f3ab883164c3916a606521b07461c05e40d /src/components/PostHeader | |
| parent | cf195f8c6b4195423dd257a99afb904673d87d25 (diff) | |
chore: add meta to single posts
Diffstat (limited to 'src/components/PostHeader')
| -rw-r--r-- | src/components/PostHeader/PostHeader.module.scss | 7 | ||||
| -rw-r--r-- | src/components/PostHeader/PostHeader.tsx | 72 |
2 files changed, 79 insertions, 0 deletions
diff --git a/src/components/PostHeader/PostHeader.module.scss b/src/components/PostHeader/PostHeader.module.scss new file mode 100644 index 0000000..f013651 --- /dev/null +++ b/src/components/PostHeader/PostHeader.module.scss @@ -0,0 +1,7 @@ +.meta { + font-size: var(--font-size-sm); +} + +.label { + font-weight: inherit; +} 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; |
