Skip to content

Commit 98f4c3e

Browse files
NisanthanNanthakumarAJ
authored andcommitted
feat: DevelopmentAPI Sidebar (#2211)
Objective: We want to customize the sidebar of the OpenAPI docs and show something similar to the Stripe docs. If a user wants to exit the OpenAPI Reference then they can use the breadcrumbs navigation.
1 parent 7d74441 commit 98f4c3e

File tree

6 files changed

+76
-5
lines changed

6 files changed

+76
-5
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import React from "react";
2+
import { graphql, useStaticQuery } from "gatsby";
3+
import { useLocation } from "@reach/router";
4+
5+
import SidebarLink from "./sidebarLink";
6+
import DynamicNav, { toTree } from "./dynamicNav";
7+
8+
const query = graphql`
9+
query DevelopmentApiNavQuery {
10+
allSitePage(
11+
filter: { path: { regex: "//development-api//" } }
12+
sort: { fields: path }
13+
) {
14+
nodes {
15+
path
16+
context {
17+
title
18+
}
19+
}
20+
}
21+
}
22+
`;
23+
24+
export default () => {
25+
const data = useStaticQuery(query);
26+
const tree = toTree(data.allSitePage.nodes.filter(n => !!n.context));
27+
const endpoints = tree[0].children.filter(curr => curr.children.length > 1);
28+
const location = useLocation();
29+
30+
const isActive = path => location && location.pathname.startsWith(path);
31+
32+
return (
33+
<ul className="list-unstyled" data-sidebar-tree>
34+
<DynamicNav
35+
root="development-api"
36+
title="API Reference"
37+
tree={tree}
38+
exclude={endpoints.map(elem => elem.node.path)}
39+
/>
40+
<li className="mb-3" data-sidebar-branch>
41+
<div
42+
className="sidebar-title d-flex align-items-center mb-0"
43+
data-sidebar-link
44+
>
45+
<h6>Endpoints</h6>
46+
</div>
47+
<ul className="list-unstyled" data-sidebar-tree>
48+
{endpoints.map(({ node: { path, context: { title } }, children }) => (
49+
<React.Fragment key={path}>
50+
<SidebarLink to={path}>{title}</SidebarLink>
51+
{isActive(path) && (
52+
<div style={{ paddingLeft: "0.5rem" }}>
53+
{children
54+
.filter(({ node }) => !!node)
55+
.map(({ node: { path, context: { title } } }) => (
56+
<SidebarLink key={path} to={path}>
57+
{title}
58+
</SidebarLink>
59+
))}
60+
</div>
61+
)}
62+
</React.Fragment>
63+
))}
64+
</ul>
65+
</li>
66+
</ul>
67+
);
68+
};

src/components/dynamicNav.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Entity<T> = {
2222
node: Node | null;
2323
};
2424

25-
interface EntityTree extends Entity<EntityTree> {}
25+
export interface EntityTree extends Entity<EntityTree> {}
2626

2727
export const toTree = (nodeList: Node[]): EntityTree[] => {
2828
const result = [];

src/gatsby/createPages/createDevelopmentApiReference.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default async ({ actions, graphql, reporter }) => {
44
const data = await getDataOrPanic(
55
`
66
query {
7-
allFile(filter: {absolutePath: {}, relativePath: {in: ["permissions.mdx", "auth.mdx", "index.mdx", "requests.mdx"]}, dir: {regex: "/api/"}}) {
7+
allFile(filter: {absolutePath: {}, relativePath: {in: ["permissions.mdx", "auth.mdx", "index.mdx", "requests.mdx", "pagination.mdx"]}, dir: {regex: "/api/"}}) {
88
nodes {
99
id
1010
childMarkdownRemark {

src/templates/developmentAPI.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Prism from "prismjs";
44

55
import BasePage from "~src/components/basePage";
66
import SmartLink from "~src/components/smartLink";
7+
import DevelopmentApiSidebar from "~src/components/developmentApiSidebar";
78

89
import "prismjs/components/prism-json";
910

@@ -71,7 +72,7 @@ export default props => {
7172
}, []);
7273

7374
return (
74-
<BasePage {...props}>
75+
<BasePage sidebar={<DevelopmentApiSidebar />} {...props}>
7576
<div className="row">
7677
<div className="col-6">
7778
{data.summary && <p>{data.summary}</p>}

src/templates/developmentApiDoc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { graphql } from "gatsby";
33

44
import BasePage from "~src/components/basePage";
55
import SmartLink from "~src/components/smartLink";
6+
import DevelopmentApiSidebar from "~src/components/developmentApiSidebar";
67

78
export default props => {
89
const {
910
data: { allOpenApi },
1011
} = props;
1112

1213
return (
13-
<BasePage {...props}>
14+
<BasePage sidebar={<DevelopmentApiSidebar />} {...props}>
1415
<ul>
1516
{allOpenApi.edges.map(({ node: { path } }) => (
1617
<li

src/templates/developmentApiReference.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { graphql } from "gatsby";
33

44
import BasePage from "~src/components/basePage";
55
import SmartLink from "~src/components/smartLink";
6+
import DevelopmentApiSidebar from "~src/components/developmentApiSidebar";
67

78
export default props => {
89
const {
@@ -19,7 +20,7 @@ export default props => {
1920
}, {});
2021

2122
return (
22-
<BasePage {...props}>
23+
<BasePage sidebar={<DevelopmentApiSidebar />} {...props}>
2324
<h2>Endpoints</h2>
2425
<p>A full list of the currently supported API endpoints:</p>
2526
<ul>

0 commit comments

Comments
 (0)