diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-01-20 19:32:36 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-01-20 19:32:36 +0100 |
| commit | 50a3df40bc8d41271c4cd8d6873a6d4e1dd87b42 (patch) | |
| tree | ee338cebabbcce5c53c417172b25367c00e19e17 /src/components/ProjectSummary/ProjectSummary.tsx | |
| parent | df9e8b1985a0f1c71c0657e72fad008bf437faba (diff) | |
chore: add a project summary component
Diffstat (limited to 'src/components/ProjectSummary/ProjectSummary.tsx')
| -rw-r--r-- | src/components/ProjectSummary/ProjectSummary.tsx | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/components/ProjectSummary/ProjectSummary.tsx b/src/components/ProjectSummary/ProjectSummary.tsx new file mode 100644 index 0000000..0d00f06 --- /dev/null +++ b/src/components/ProjectSummary/ProjectSummary.tsx @@ -0,0 +1,122 @@ +import GithubIcon from '@assets/images/social-media/github.svg'; +import GitlabIcon from '@assets/images/social-media/gitlab.svg'; +import { t } from '@lingui/macro'; +import { getRepoData } from '@services/repos/github'; +import { ProjectMeta } from '@ts/types/app'; +import { RepoData } from '@ts/types/github'; +import { slugify } from '@utils/helpers/slugify'; +import Image from 'next/image'; +import { useRouter } from 'next/router'; +import { useEffect, useState } from 'react'; +import styles from './ProjectSummary.module.scss'; + +const ProjectSummary = ({ + slug, + title, + cover, + meta, +}: { + slug: string; + title: string; + cover: string; + meta: ProjectMeta; +}) => { + const { license, repos, technologies } = meta; + const [data, setData] = useState<RepoData>(); + const { locale } = useRouter(); + const githubUser = process.env.NEXT_PUBLIC_GITHUB_USER; + + useEffect(() => { + getRepoData(slug) + .then((repoData) => setData(repoData)) + .catch((e) => console.error(e)); + }, [slug]); + + const getFormattedDate = (date: string) => { + const dateOptions: Intl.DateTimeFormatOptions = { + day: 'numeric', + month: 'long', + year: 'numeric', + }; + + return new Date(date).toLocaleDateString(locale, dateOptions); + }; + + return ( + <div className={styles.wrapper}> + <div className={styles.cover}> + <Image + src={cover} + alt={t`${title} preview`} + layout="fill" + objectFit="contain" + /> + </div> + <dl className={styles.info}> + {data && ( + <div className={styles.info__item}> + <dt>{t`Created on`}</dt> + <dd>{t`${getFormattedDate(data.created_at)}`}</dd> + </div> + )} + {data && ( + <div className={styles.info__item}> + <dt>{t`Last updated on`}</dt> + <dd>{t`${getFormattedDate(data.updated_at)}`}</dd> + </div> + )} + <div className={styles.info__item}> + <dt>{t`License`}</dt> + <dd>{license}</dd> + </div> + {technologies && ( + <div className={styles.info__item}> + <dt>{t`Technologies`}</dt> + {technologies.map((techno) => ( + <dd + key={slugify(techno)} + className={`${styles.techno} ${styles['inline-data']}`} + > + {techno} + </dd> + ))} + </div> + )} + {repos && ( + <div className={styles.info__item}> + <dt>{t`Repositories`}</dt> + {repos.github && ( + <dd className={styles['inline-data']}> + <a href={repos.github} className={styles.repo}> + <GithubIcon /> + <span className="screen-reader-text">Github</span> + </a> + </dd> + )} + {repos.gitlab && ( + <dd className={styles['inline-data']}> + <a href={repos.gitlab} className={styles.repo}> + <GitlabIcon /> + <span className="screen-reader-text">Gitlab</span> + </a> + </dd> + )} + </div> + )} + {data && ( + <div> + <dt>{t`Popularity`}</dt> + <dd> + ⭐ + <a href={`https://github.com/${githubUser}/${slug}/stargazers`}> + {t`${data.stargazers_count} stars on Github`} + </a> + </dd> + </div> + )} + </dl> + </div> + ); +}; + +export default ProjectSummary; |
