-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApiReference.tsx
133 lines (123 loc) · 3.79 KB
/
ApiReference.tsx
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import Link from "next/link";
import { ApiReferenceProvider } from "../../components/ApiReference/ApiReferenceContext";
import { ApiReferenceSection } from "../../components/ApiReference";
import MinimalHeader from "../../components/Header/MinimalHeader";
import Sidebar from "../../components/Sidebar";
import { Page } from "../../layouts/Page";
import { useEffect, useLayoutEffect } from "react";
import { useRouter } from "next/router";
import { StainlessConfig } from "../../lib/openApiSpec";
import { OpenAPIV3 } from "@scalar/openapi-types";
import { getSidebarContent } from "./helpers";
import { SidebarSection } from "../../data/types";
type Props = {
name: string;
openApiSpec: OpenAPIV3.Document;
stainlessSpec: StainlessConfig;
preContent?: React.ReactNode;
preSidebarContent?: SidebarSection[];
resourceOrder: string[];
};
function ApiReference({
name,
openApiSpec,
stainlessSpec,
preContent,
preSidebarContent,
resourceOrder = [],
}: Props) {
const router = useRouter();
const basePath = router.pathname.split("/")[1];
useEffect(() => {
const path = router.asPath;
const resourcePath = path.replace(`/${basePath}`, "");
const element = document.querySelector(
`[data-resource-path="${resourcePath}"]`,
);
if (element) {
setTimeout(() => {
element.scrollIntoView();
}, 200);
}
}, [router.asPath, basePath]);
useLayoutEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const resourcePath =
entry.target.getAttribute("data-resource-path");
if (resourcePath) {
window.history.replaceState(
null,
"",
`/${basePath}${resourcePath}`,
);
}
}
});
},
{
threshold: 0.1,
rootMargin: "-64px 0px -80% 0px",
},
);
// Observe all elements with data-resource-path
document.querySelectorAll("[data-resource-path]").forEach((element) => {
observer.observe(element);
});
// Cleanup observer on unmount
return () => observer.disconnect();
}, [basePath]);
return (
<ApiReferenceProvider
openApiSpec={openApiSpec}
stainlessConfig={stainlessSpec}
>
<div className="wrapper">
<Page
header={<MinimalHeader pageType="API" />}
sidebar={
<Sidebar
content={getSidebarContent(
openApiSpec,
stainlessSpec,
resourceOrder,
basePath,
preSidebarContent,
)}
>
<Link
href="/"
passHref
className="text-sm block font-medium text-gray-500 hover:text-gray-900 dark:text-gray-300 dark:hover:text-gray-100"
>
← Back to docs
</Link>
</Sidebar>
}
metaProps={{
title: `Knock ${name} Reference | Knock`,
description: `Complete reference documentation for the Knock ${name}.`,
}}
>
<div className="w-full max-w-5xl lg:flex mx-auto relative">
<div className="w-full flex-auto">
<div className="docs-content api-docs-content">
{preContent}
{resourceOrder.map((resourceName) => (
<ApiReferenceSection
key={resourceName}
resourceName={resourceName}
resource={stainlessSpec.resources[resourceName]}
/>
))}
</div>
</div>
</div>
</Page>
</div>
</ApiReferenceProvider>
);
}
export default ApiReference;