|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import ReactPaginate from 'react-paginate'; |
| 3 | +import Link from '@docusaurus/Link'; |
| 4 | +import styles from './styles.module.css'; |
| 5 | +import axios from 'axios'; |
| 6 | +import dayjs from 'dayjs'; |
| 7 | +import Markdown from '../../libs/markdown'; |
| 8 | +import clsx from 'clsx'; |
| 9 | + |
| 10 | +type RepoKey = |
| 11 | + | 'frouriojs/frourio' |
| 12 | + | 'frouriojs/frourio-express' |
| 13 | + | 'frouriojs/create-frourio-app' |
| 14 | + | 'aspida/aspida'; |
| 15 | + |
| 16 | +export type Props = { |
| 17 | + repo: RepoKey; |
| 18 | +}; |
| 19 | + |
| 20 | +const ITEMS_PER_PAGE = 5; |
| 21 | + |
| 22 | +const PagenatedReleases: React.FC<Props> = ({ repo }) => { |
| 23 | + const [releases, setReleases] = useState<Release[]>([]); |
| 24 | + const [currentPage, setCurrentPage] = useState(0); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + const _f = async () => { |
| 28 | + const res = await axios.get<Release[]>( |
| 29 | + `https://api.github.com/repos/${repo}/releases?per_page=50` |
| 30 | + ); |
| 31 | + setReleases(res.data); |
| 32 | + }; |
| 33 | + _f(); |
| 34 | + }, [setReleases]); |
| 35 | + |
| 36 | + return ( |
| 37 | + <div id="pagenatedReleases"> |
| 38 | + {releases |
| 39 | + .slice(currentPage * ITEMS_PER_PAGE, (currentPage + 1) * ITEMS_PER_PAGE) |
| 40 | + .map((release, i) => ( |
| 41 | + <Release release={release} key={i} /> |
| 42 | + ))} |
| 43 | + <ReactPaginate |
| 44 | + forcePage={currentPage} |
| 45 | + pageCount={Math.ceil(releases.length / ITEMS_PER_PAGE)} |
| 46 | + pageRangeDisplayed={window.innerWidth >= 600 ? 4 : 2} |
| 47 | + marginPagesDisplayed={window.innerWidth >= 600 ? 2 : 1} |
| 48 | + onPageChange={({ selected }) => { |
| 49 | + setCurrentPage(selected); |
| 50 | + window.scroll({ top: 250, behavior: 'smooth' }); |
| 51 | + }} |
| 52 | + containerClassName={clsx('pagination', styles.pagenation)} |
| 53 | + pageClassName="pagination__item" |
| 54 | + activeClassName="pagination__item--active" |
| 55 | + previousClassName="pagination__item" |
| 56 | + nextClassName="pagination__item" |
| 57 | + breakClassName="pagination__item" |
| 58 | + pageLinkClassName="pagination__link" |
| 59 | + previousLinkClassName="pagination__link" |
| 60 | + nextLinkClassName="pagination__link" |
| 61 | + breakLinkClassName={styles.breakLink} |
| 62 | + disabledClassName="disabled" |
| 63 | + previousLabel="<" |
| 64 | + nextLabel=">" |
| 65 | + breakLabel="..." |
| 66 | + /> |
| 67 | + </div> |
| 68 | + ); |
| 69 | +}; |
| 70 | + |
| 71 | +export default PagenatedReleases; |
| 72 | + |
| 73 | +interface Release { |
| 74 | + url: string; |
| 75 | + html_url: string; |
| 76 | + id: string; |
| 77 | + node_id: string; |
| 78 | + tag_name: string; |
| 79 | + target_commitish: string; |
| 80 | + name: string; |
| 81 | + body: string; |
| 82 | + draft: boolean; |
| 83 | + prerelease: boolean; |
| 84 | + created_at: string; |
| 85 | + published_at: string; |
| 86 | +} |
| 87 | + |
| 88 | +const Release: React.FC<{ release: Release }> = ({ release }) => { |
| 89 | + return ( |
| 90 | + <article className={styles.release}> |
| 91 | + <div className={styles.leftBox}> |
| 92 | + <h2 className={styles.version}> |
| 93 | + <Link href={release.html_url}>{release.name}</Link> |
| 94 | + </h2> |
| 95 | + <span className={styles.date}>{dayjs(release.created_at).format('YYYY.MM.DD')}</span> |
| 96 | + </div> |
| 97 | + <div |
| 98 | + dangerouslySetInnerHTML={{ |
| 99 | + __html: Markdown.render(release.body), |
| 100 | + }} |
| 101 | + /> |
| 102 | + </article> |
| 103 | + ); |
| 104 | +}; |
0 commit comments