import { t } from '@lingui/macro'; import { ArticleMeta } from '@ts/types/articles'; import Link from 'next/link'; import { useRouter } from 'next/router'; import styles from './PostMeta.module.scss'; type PostMetaMode = 'list' | 'single'; const PostMeta = ({ meta, mode = 'list', }: { meta: ArticleMeta; mode?: PostMetaMode; }) => { const { author, commentCount, dates, thematics, website } = meta; const { locale } = useRouter(); const dateOptions: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'long', year: 'numeric', }; const getThematics = () => { return ( thematics && thematics.map((thematic) => { return (
{thematic.title}
); }) ); }; const getCommentsCount = () => { switch (commentCount) { case 0: return t`No comments`; case 1: return t`1 comment`; default: return t`${commentCount} comments`; } }; const wrapperClass = styles[`wrapper--${mode}`]; return (
{author && (
{t`Written by`}
{author.name}
)} {dates && (
{t`Published on`}
{new Date(dates.publication).toLocaleDateString( locale, dateOptions )}
)} {dates && dates.publication !== dates.update && (
{t`Updated on`}
{new Date(dates.update).toLocaleDateString(locale, dateOptions)}
)} {thematics && thematics.length > 0 && (
{thematics.length > 1 ? t`Thematics` : t`Thematic`}
{getThematics()}
)} {website && (
{t`Website`}
{website}
)} {commentCount !== undefined && (
{t`Comments`}
{getCommentsCount()}
)}
); }; export default PostMeta;