-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Expand file tree
/
Copy pathrequest-snippets.jsx
More file actions
173 lines (154 loc) · 5.55 KB
/
Copy pathrequest-snippets.jsx
File metadata and controls
173 lines (154 loc) · 5.55 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React, { useRef, useEffect, useState } from "react"
import classNames from "classnames"
import PropTypes from "prop-types"
import { CopyToClipboard } from "react-copy-to-clipboard"
const COPY_REQUEST_SNIPPET_LABEL = "Copy request snippet to clipboard"
const style = {
cursor: "pointer",
lineHeight: 1,
display: "inline-flex",
backgroundColor: "rgb(250, 250, 250)",
paddingBottom: "0",
paddingTop: "0",
border: "1px solid rgb(51, 51, 51)",
borderRadius: "4px 4px 0 0",
boxShadow: "none",
borderBottom: "none"
}
const activeStyle = {
cursor: "pointer",
lineHeight: 1,
display: "inline-flex",
backgroundColor: "rgb(51, 51, 51)",
boxShadow: "none",
border: "1px solid rgb(51, 51, 51)",
paddingBottom: "0",
paddingTop: "0",
borderRadius: "4px 4px 0 0",
marginTop: "-5px",
marginRight: "-5px",
marginLeft: "-5px",
zIndex: "9999",
borderBottom: "none"
}
const RequestSnippets = ({ request, requestSnippetsSelectors, getComponent }) => {
const rootRef = useRef(null)
const ArrowIcon = getComponent("ArrowUpIcon")
const ArrowDownIcon = getComponent("ArrowDownIcon")
const SyntaxHighlighter = getComponent("SyntaxHighlighter", true)
const [activeLanguage, setActiveLanguage] = useState(requestSnippetsSelectors.getSnippetGenerators()?.keySeq().first())
const [isExpanded, setIsExpanded] = useState(requestSnippetsSelectors?.getDefaultExpanded())
const snippetGenerators = requestSnippetsSelectors.getSnippetGenerators()
const activeGenerator = snippetGenerators.get(activeLanguage)
const snippet = activeGenerator.get("fn")(request)
const handleGenChange = (key) => {
const needsChange = activeLanguage !== key
if (needsChange) {
setActiveLanguage(key)
}
}
const handleSetIsExpanded = () => {
setIsExpanded(!isExpanded)
}
const handleGetBtnStyle = (key) => {
if (key === activeLanguage) {
return activeStyle
}
return style
}
const handlePreventYScrollingBeyondElement = (e) => {
const { target, deltaY } = e
const { scrollHeight: contentHeight, offsetHeight: visibleHeight, scrollTop } = target
const scrollOffset = visibleHeight + scrollTop
const isElementScrollable = contentHeight > visibleHeight
const isScrollingPastTop = scrollTop === 0 && deltaY < 0
const isScrollingPastBottom = scrollOffset >= contentHeight && deltaY > 0
if (isElementScrollable && (isScrollingPastTop || isScrollingPastBottom)) {
e.preventDefault()
}
}
useEffect(() => {
const doIt = () => {
}
doIt()
}, [])
useEffect(() => {
const childNodes = Array
.from(rootRef.current.childNodes)
.filter(node => !!node.nodeType && node.classList?.contains("curl-command"))
// eslint-disable-next-line no-use-before-define
childNodes.forEach(node => node.addEventListener("mousewheel", handlePreventYScrollingBeyondElement, { passive: false }))
return () => {
// eslint-disable-next-line no-use-before-define
childNodes.forEach(node => node.removeEventListener("mousewheel", handlePreventYScrollingBeyondElement))
}
}, [request])
return (
<div className="request-snippets" ref={rootRef}>
<div style={{ width: "100%", display: "flex", justifyContent: "flex-start", alignItems: "center", marginBottom: "15px" }}>
<h4
onClick={() => handleSetIsExpanded()}
style={{ cursor: "pointer" }}
>Snippets</h4>
<button
onClick={() => handleSetIsExpanded()}
style={{ border: "none", background: "none" }}
title={isExpanded ? "Collapse operation" : "Expand operation"}
>
{isExpanded ? <ArrowDownIcon className="arrow" width="10" height="10" /> : <ArrowIcon className="arrow" width="10" height="10" />}
</button>
</div>
{
isExpanded && <div className="curl-command">
<div style={{ paddingLeft: "15px", paddingRight: "10px", width: "100%", display: "flex" }}>
{
snippetGenerators.entrySeq().map(([key, gen]) => {
return (
<div
className={classNames("btn", {"active": key === activeLanguage })}
style={handleGetBtnStyle(key)}
key={key}
onClick={() => handleGenChange(key)}
>
<h4 style={key === activeLanguage ? { color: "white", } : {}}>{gen.get("title")}</h4>
</div>
)
})
}
</div>
<div
className="copy-to-clipboard"
title={COPY_REQUEST_SNIPPET_LABEL}
aria-label={COPY_REQUEST_SNIPPET_LABEL}
>
<CopyToClipboard text={snippet}>
<button
aria-label={COPY_REQUEST_SNIPPET_LABEL}
title={COPY_REQUEST_SNIPPET_LABEL}
type="button"
/>
</CopyToClipboard>
</div>
<div>
<SyntaxHighlighter
language={activeGenerator.get("syntax")}
className="curl microlight"
renderPlainText={({ children, PlainTextViewer }) => (
<PlainTextViewer className="curl">{children}</PlainTextViewer>
)}
>
{snippet}
</SyntaxHighlighter>
</div>
</div>
}
</div>
)
}
RequestSnippets.propTypes = {
request: PropTypes.object.isRequired,
requestSnippetsSelectors: PropTypes.object.isRequired,
getComponent: PropTypes.func.isRequired,
requestSnippetsActions: PropTypes.object,
}
export default RequestSnippets