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, subjects, thematics, website } = meta;
const { asPath, locale } = useRouter();
const isThematic = () => asPath.includes('/thematique/');
const dateOptions: Intl.DateTimeFormatOptions = {
day: 'numeric',
month: 'long',
year: 'numeric',
};
const getSubjects = () => {
return (
subjects &&
subjects.map((subject) => {
return (
{subject.title}
);
})
);
};
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)}
)}
{!isThematic() && thematics && thematics.length > 0 && (
-
{thematics.length > 1 ? t`Thematics` : t`Thematic`}
{getThematics()}
)}
{isThematic() && subjects && subjects.length > 0 && (
-
{subjects.length > 1 ? t`Subjects` : t`Subject`}
{getSubjects()}
)}
{website && (
)}
{commentCount !== undefined && (
- {t`Comments`}
{getCommentsCount()}
)}
);
};
export default PostMeta;