-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOpenRPCDocMethod.tsx
More file actions
154 lines (141 loc) · 5.07 KB
/
OpenRPCDocMethod.tsx
File metadata and controls
154 lines (141 loc) · 5.07 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React from "react";
const DocSidebar = require("@theme/DocSidebar").default;
import {
Sidebar,
} from "@docusaurus/plugin-content-docs/src/sidebars/types";
import Layout from '@theme/Layout';
import {ExamplePairingObject, MethodObject, ContentDescriptorObject} from '@open-rpc/meta-schema';
import { InteractiveMethod, Method} from '@metamask/open-rpc-docs-react';
import { join } from 'path';
import "./OpenRPCDocMethod.css";
const CodeBlock = require('@theme/CodeBlock').default;
const getExamplesFromMethod = (method?: MethodObject): ExamplePairingObject[] => {
if (!method) { return []; }
if (!method.params) { return []; }
const examples: ExamplePairingObject[] = [];
(method.params as ContentDescriptorObject[]).forEach((param, index: number) => {
if (param.schema && param.schema.examples && param.schema.examples.length > 0) {
param.schema.examples.forEach((ex: any, i: number) => {
const example = examples[i];
if (example === undefined) {
examples.push({
name: "generated-example",
params: [
{
name: param.name,
value: ex,
},
],
result: {
name: "example-result",
value: null,
},
});
} else {
example.params.push({
name: param.name,
value: ex,
});
}
});
}
});
const methodResult = method.result as ContentDescriptorObject;
if (methodResult && methodResult.schema && methodResult.schema.examples && methodResult.schema.examples.length > 0) {
methodResult.schema.examples.forEach((ex: any, i: number) => {
const example = examples[i];
if (example === undefined) {
examples.push({
name: "generated-example",
params: [],
result: {
name: methodResult.name,
value: ex,
},
});
} else {
example.result = {
name: methodResult.name,
value: ex,
};
}
});
}
return examples;
};
export default function OpenRPCDocMethod(props: any) {
const {versionMetadata} = props;
let sidebar: Sidebar = [
{
type: 'link' as const,
label: props.propsFile.openrpcDocument.info.title || 'JSON-RPC',
href: props.propsFile.path,
},
].concat(
props.propsFile.openrpcDocument.methods.map((method: MethodObject) => {
return {
type: 'link',
label: method.name,
href: join(props.propsFile.path, method.name.toLowerCase()),
};
}),
);
if (versionMetadata) {
sidebar = Object.values(versionMetadata.docsSidebars)[0] as Sidebar;
}
const method = props.propsFile.openrpcDocument.methods.find((m: MethodObject) => {
const parts = props.route.path.split("/");
let name = parts[parts.length - 1];
// deal with trailingSlash: true
if (name === "") {
name = parts[parts.length - 2];
}
return m.name.toLowerCase() === name.toLowerCase();
})
method.examples = method.examples || getExamplesFromMethod(method);
const [selectedExamplePairing, setSelectedExamplePairing] = React.useState<ExamplePairingObject | undefined>(method.examples[0]);
// requestTemplate
return (
<Layout>
<div style={{ display: "flex", width: "100%", flex: "1 0", }} className="docusaurus-openrpc">
<aside
style={{
display: "block",
width: "var(--doc-sidebar-width)",
marginTop: "calc(-1 * var(--ifm-navbar-height))",
borderRight: "1px solid var(--ifm-toc-border-color)",
willChange: "width",
transition: "width var(--ifm-transition-fast) ease",
clipPath: "inset(0)",
}}
className="theme-doc-sidebar-container">
<div style={{
top: 0,
position: "sticky",
height: "100%",
maxHeight: "100vh",
}}>
<DocSidebar path={props.route.path} sidebar={sidebar}/>
</div>
</aside>
<main className="docMainContainer" style={{width: "100%"}}>
<div className="container padding-top--md padding-bottom--lg">
<div className="row">
<div className="col col--7">
{!method &&
<div>Index</div>
}
<article>
{method && <Method method={method} components={{CodeBlock}} onExamplePairingChange={(examplePairing: ExamplePairingObject | undefined) => setSelectedExamplePairing(examplePairing)}/>}
</article>
</div>
<div id="interactive-box" className="col col--5 interactive-right-sidebar table-of-contents__left-border thin-scrollbar">
{method && <InteractiveMethod openrpcDocument={props.propsFile.openrpcDocument} method={method} components={{CodeBlock}} selectedExamplePairing={selectedExamplePairing as ExamplePairingObject} requestTemplate={props.propsFile.requestTemplate}/>}
</div>
</div>
</div>
</main>
</div>
</Layout>
);
}