import { plural, 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,
readingTime,
results,
thematics,
topics,
website,
wordsCount,
} = meta;
const { asPath, locale } = useRouter();
const isThematic = () => asPath.includes('/thematique/');
const isArticle = () => asPath.includes('/article/');
const dateOptions: Intl.DateTimeFormatOptions = {
day: 'numeric',
month: 'long',
year: 'numeric',
};
const getTopics = () => {
return (
topics &&
topics.map((topic) => {
return (
{topic.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 getReadingTime = () => {
if (!readingTime) return;
if (readingTime < 0) return t`less than 1 minute`;
return plural(readingTime, {
zero: '# minutes',
one: '# minute',
other: '# minutes',
});
};
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)}
)}
{readingTime !== undefined && wordsCount !== undefined && (
- {t`Reading time:`}
-
{getReadingTime()}
)}
{results && (
- {t`Total: `}
-
{plural(results, {
zero: '# articles',
one: '# article',
other: '# articles',
})}
)}
{!isThematic() && thematics && thematics.length > 0 && (
-
{thematics.length > 1 ? t`Thematics:` : t`Thematic:`}
{getThematics()}
)}
{isThematic() && topics && topics.length > 0 && (
-
{topics.length > 1 ? t`Topics:` : t`Topic:`}
{getTopics()}
)}
{website && (
)}
{commentCount !== undefined && (
)}
);
};
export default PostMeta;