/
ServerLayover.tsx
174 lines (158 loc) Β· 4.69 KB
/
ServerLayover.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
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
174
import * as React from 'react'
import * as GraphiQL from 'graphiql'
import * as CopyToClipboard from 'react-copy-to-clipboard'
import * as Relay from 'react-relay'
import BrowserView from '../BrowserView/BrowserView'
import { Icon } from 'graphcool-styles'
import * as ReactGA from 'react-ga'
import { events } from '../../utils/events'
require('graphiql/graphiql.css')
interface Props {
projectId: string
close: () => void
viewer: any
}
interface State {
showData: boolean
}
class ServerLayover extends React.Component<Props, State> {
state = {
showData: true,
}
render() {
const graphQLFetcher = (graphQLParams) => {
return fetch(`https://api.graph.cool/simple/v1/${this.props.projectId}`, {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(graphQLParams),
})
.then(response => {
if (response.ok && !graphQLParams.query.includes('IntrospectionQuery')) {
ReactGA.event(events.OverlayGraphiQLRanQuery)
}
return response.json()
})
}
return (
<div
className='fixed bottom-0 w-100 left-0 bg-gray-2'
style={{
height: this.state.showData ? 350 : 550,
transition: 'height 0.5s ease',
zIndex: 10,
}}
>
<div className='flex justify-between bg-accent' style={{ height: 70 }}>
<div className='flex pt2'>
<div
className={`
h-100 f4 flex items-center ph3 mh2 pointer
${this.state.showData ? 'bg-gray-2 accent' : 'white'}
`}
onClick={this.showData}
>
Data Browser
</div>
<div
className={`
h-100 f4 flex items-center ph3 mh2 pointer
${!this.state.showData ? 'bg-gray-2 accent' : 'white'}
`}
onClick={this.showGraphiQL}
>
GraphiQL
</div>
</div>
<div className='flex items-center p3'>
<div className='o-30' style={{marginRight: 12}}>API Endpoint</div>
<div className='flex items-center'>
<CopyToClipboard
text={`https://api.graph.cool/simple/v1/${this.props.projectId}`}
onCopy={() => ReactGA.event(events.OverlayCopiedEndpoint)}
>
<Icon
src={require('../../assets/icons/copy.svg')}
className='dim'
style={{
padding: '6px',
background: 'rgba(0,0,0,0.1)',
cursor: 'pointer',
}}
/>
</CopyToClipboard>
<div
className='o-50'
style={{background: 'rgba(0,0,0,0.05)', padding: '6px 12px'}}
>
{`https://api.graph.cool/simple/v1/${this.props.projectId}`}
</div>
</div>
<div
className='f2 mh3 pointer'
onClick={this.props.close}
>
<Icon
src={require('../../assets/icons/close.svg')}
color='#fff'
width={19}
height={19}
/>
</div>
</div>
</div>
{this.state.showData &&
<BrowserView viewer={this.props.viewer}/>
}
{!this.state.showData &&
<div style={{height: 480}}>
<GraphiQL
fetcher={graphQLFetcher}
variables={''}
/>
</div>
}
</div>
)
}
private showData = () => {
ReactGA.event(events.OverlayShowDatabrowser)
this.setState({showData: true} as State)
}
private showGraphiQL = () => {
ReactGA.event(events.OverlayShowGraphiQL)
this.setState({showData: false} as State)
}
}
const LayoverContainer = Relay.createContainer(ServerLayover, {
fragments: {
viewer: () => Relay.QL`
fragment on Viewer {
id
${BrowserView.getFragment('viewer')}
}
`,
},
})
interface RendererProps {
projectId: string
close: () => void
}
export default class LayoverRenderer extends React.Component<RendererProps, {}> {
constructor(props) {
super(props)
Relay.injectNetworkLayer(new Relay.DefaultNetworkLayer(`https://api.graph.cool/relay/v1/${this.props.projectId}`))
}
render() {
return (
<Relay.Renderer
Container={LayoverContainer}
environment={Relay.Store}
queryConfig={{
name: '',
queries: {viewer: () => Relay.QL`query { viewer }`},
params: {projectId: this.props.projectId, close: this.props.close},
}}
/>
)
}
}