summaryrefslogtreecommitdiffhomepage
path: root/services/blog/nuldoc-src/components/Pagination.tsx
blob: ab13e361e17fb1ee1cc4f11818dedf62523156af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
type Props = {
  currentPage: number;
  totalPages: number;
  basePath: string;
};

export default function Pagination(
  { currentPage, totalPages, basePath }: Props,
) {
  if (totalPages <= 1) {
    return <div></div>;
  }

  const firstPage = 1;
  const lastPage = totalPages;
  const prevPage = currentPage > 1 ? currentPage - 1 : null;
  const nextPage = currentPage < totalPages ? currentPage + 1 : null;

  const firstHref = pageUrlAt(basePath, firstPage);
  const lastHref = pageUrlAt(basePath, lastPage);
  const prevHref = prevPage ? pageUrlAt(basePath, prevPage) : null;
  const nextHref = nextPage ? pageUrlAt(basePath, nextPage) : null;

  return (
    <nav className="pagination">
      <div className="pagination-prev">
        {prevHref
          ? (
            <a href={prevHref}>
              前へ
            </a>
          )
          : null}
      </div>
      <div
        className={"pagination-page" +
          (firstPage === currentPage ? " pagination-page-current" : "")}
      >
        {firstPage === currentPage
          ? String(firstPage)
          : <a href={firstHref}>{String(firstPage)}</a>}
      </div>
      {currentPage - firstPage > 1
        ? (
          <div className="pagination-elipsis">
            …
          </div>
        )
        : null}
      {currentPage !== firstPage && currentPage !== lastPage
        ? (
          <div className="pagination-page pagination-page-current">
            {String(currentPage)}
          </div>
        )
        : null}
      {lastPage - currentPage > 1
        ? (
          <div className="pagination-elipsis">
            …
          </div>
        )
        : null}
      <div
        className={"pagination-page" +
          (lastPage === currentPage ? " pagination-page-current" : "")}
      >
        {lastPage === currentPage
          ? String(lastPage)
          : <a href={lastHref}>{String(lastPage)}</a>}
      </div>
      <div className="pagination-next">
        {nextHref
          ? (
            <a href={nextHref}>
              次へ
            </a>
          )
          : null}
      </div>
    </nav>
  );
}

function pageUrlAt(basePath: string, page: number): string {
  return page === 1 ? basePath : `${basePath}${page}/`;
}