-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathpagination.js
More file actions
110 lines (97 loc) · 3.51 KB
/
pagination.js
File metadata and controls
110 lines (97 loc) · 3.51 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React, { useEffect, useState } from "react";
import { graphql, useStaticQuery } from "gatsby";
import Button from "../../reusecore/Button";
import PaginationWrapper from "./pagination.style";
const STABLE_ROUTES = [
// Getting Started
{ link: "/projects/sistent/getting-started/about", text: "About" },
{ link: "/projects/sistent/getting-started/installation", text: "Installation" },
{ link: "/projects/sistent/getting-started/usage", text: "Usage" },
{ link: "/projects/sistent/getting-started/tokens", text: "Tokens" },
// Identity
{ link: "/projects/sistent/identity/color", text: "Colors" },
{ link: "/projects/sistent/identity/color/guidance", text: "Colors" },
{ link: "/projects/sistent/identity/color/code", text: "Colors" },
{ link: "/projects/sistent/identity/spacing", text: "Spacing" },
{ link: "/projects/sistent/identity/spacing/guidance", text: "Spacing" },
{ link: "/projects/sistent/identity/spacing/code", text: "Spacing" },
{ link: "/projects/sistent/identity/typography", text: "Typography" },
{ link: "/projects/sistent/identity/typography/guidance", text: "Typography" },
{ link: "/projects/sistent/identity/typography/code", text: "Typography" },
];
const PAGE_TYPE_ORDER = {
overview: 1,
guidance: 2,
code: 3,
};
const SistentPagination = () => {
const [currentPage, setCurrentPage] = useState(0);
const data = useStaticQuery(graphql`
query SistentPaginationNav {
allMdx(
filter: {
fields: { collection: { eq: "sistent" } }
}
) {
nodes {
frontmatter {
name
component
published
}
fields {
slug
pageType
}
}
}
}
`);
// Compile set of published components based on overview pages
const publishedComponents = new Set();
data.allMdx.nodes.forEach((node) => {
if (node.fields.pageType === "overview" && node.frontmatter.published === true) {
publishedComponents.add(node.frontmatter.component);
}
});
// Map, filter out drafts, and group by tab order: overview -> guidance -> code
const dynamicRoutes = data.allMdx.nodes
.map((node) => ({
componentSlug: node.frontmatter.component,
name: node.frontmatter.name || node.frontmatter.component,
link: node.fields.slug,
pageType: node.fields.pageType,
}))
.filter((node) => publishedComponents.has(node.componentSlug))
.sort((a, b) => {
if (a.componentSlug !== b.componentSlug) {
return (a.componentSlug || "").localeCompare(b.componentSlug || "");
}
return (
(PAGE_TYPE_ORDER[a.pageType] || 99) - (PAGE_TYPE_ORDER[b.pageType] || 99)
);
});
const fullContentArray = [...STABLE_ROUTES, ...dynamicRoutes];
useEffect(() => {
const path = window.location.pathname;
// Handle trajectory slashes
const cleanPath = path.endsWith("/") && path.length > 1 ? path.slice(0, -1) : path;
const index = fullContentArray.findIndex((x) => x.link === cleanPath);
setCurrentPage(index);
}, [fullContentArray]);
return (
<PaginationWrapper>
{currentPage > 0 ? (
<Button $secondary $url={fullContentArray[currentPage - 1]?.link}>
← Previous
</Button>
) : <div />}
{currentPage !== -1 && currentPage < fullContentArray.length - 1 ? (
<Button $primary $url={fullContentArray[currentPage + 1]?.link}>
Next →
</Button>
) : <div />}
</PaginationWrapper>
);
};
export default SistentPagination;