/
ApiTabs.tsx
218 lines (200 loc) · 6.01 KB
/
ApiTabs.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import React, { useState } from 'react'
import styled, { keyframes } from 'styled-components'
import media from '../../../theming/mediaQueries'
import { useTheme } from '../../../theming/context'
import { CodeBlock } from '../../CodeBlock'
import { ApiPreview } from './ApiPreview'
const tabs = ['preview', 'body', 'data'] as const
type Tab = typeof tabs[number]
interface ApiTabsProps<Data, Body> {
chartClass: string
data: Data
body: Body
isLoading: boolean
responseStatus: number | null
chartUrl: string
}
export function ApiTabs<Data = any, Body = any>({
chartClass,
data,
body,
isLoading,
responseStatus,
chartUrl,
}: ApiTabsProps<Data, Body>) {
const theme = useTheme()
const [currentTab, setCurrentTab] = useState<Tab>('preview')
const [hoverTab, setHoverTab] = useState<Tab | null>(null)
return (
<Wrapper className={`chart-tabs--${currentTab}`}>
<Nav>
{tabs.map(tab => {
const isCurrent = tab === currentTab
const icon = tab === 'preview' ? chartClass : 'data'
const iconColors = isCurrent || hoverTab === tab ? 'colored' : 'neutral'
return (
<NavItem
key={tab}
className="no-select"
isCurrent={isCurrent}
onClick={() => setCurrentTab(tab)}
onMouseEnter={() => setHoverTab(tab)}
onMouseLeave={() => setHoverTab(null)}
>
<Icon className={`sprite-icons-${icon}-${theme.id}-${iconColors}`} />
{tab}
</NavItem>
)
})}
</Nav>
<Content>
{currentTab === 'preview' && (
<ApiPreview responseStatus={responseStatus} chartUrl={chartUrl} />
)}
{currentTab === 'body' && <CodeBlock>{JSON.stringify(body, null, ' ')}</CodeBlock>}
{currentTab === 'data' && <CodeBlock>{data}</CodeBlock>}
<Loader isLoading={isLoading} />
</Content>
</Wrapper>
)
}
const Wrapper = styled.div`
position: fixed;
top: ${({ theme }) => theme.dimensions.headerHeight}px;
right: 0;
width: 60%;
--innerWidth: calc(100% - ${({ theme }) => theme.dimensions.miniNavWidth}px);
width: calc(var(--innerWidth) * 0.6);
--innerHeight: calc(100% - ${({ theme }) => theme.dimensions.headerHeight}px);
height: calc(var(--innerHeight) * 0.55);
z-index: 10;
overflow: hidden;
background: ${({ theme }) => theme.colors.cardBackground};
border-bottom: 1px solid ${({ theme }) => theme.colors.border};
${media.tablet`
& {
top: ${({ theme }) => theme.dimensions.headerHeight}px;
right: 0;
width: 60%;
--innerHeight: calc(100% - ${({ theme }) => theme.dimensions.headerHeight}px);
height: calc(var(--innerHeight) * 0.55);
}
`}
${media.mobile`
& {
position: relative;
top: auto;
right: auto;
width: auto;
height: 460px;
z-index: 0;
border-top: 1px solid ${({ theme }) => theme.colors.border};
}
`}
`
const Nav = styled.nav`
height: 46px;
background: ${({ theme }) => theme.colors.background};
display: flex;
font-size: 15px;
color: #aaa;
position: relative;
`
const NavItem = styled.span<{
isCurrent: boolean
}>`
cursor: pointer;
height: 46px;
display: block;
position: relative;
padding-left: 46px;
padding-right: 14px;
padding-top: 11px;
background: ${({ isCurrent, theme }) =>
isCurrent ? theme.colors.cardBackground : 'transparent'};
&:hover {
color: ${({ theme }) => theme.colors.text};
}
`
const Icon = styled.span`
position: absolute;
top: 0;
left: 0;
display: block;
transform: scale(0.44);
transform-origin: top left;
margin: 12px 0 0 12px;
`
const Content = styled.div`
position: absolute;
top: 46px;
bottom: 0;
width: 100%;
overflow-x: hidden;
overflow-y: auto;
`
const Loader = ({ isLoading }: { isLoading: boolean }) => {
return (
<LoaderContainer isLoading={isLoading}>
<span>
<LoaderBar index={0} />
</span>
<span>
<LoaderBar index={1} />
</span>
<span>
<LoaderBar index={2} />
</span>
<span>
<LoaderBar index={3} />
</span>
<span>
<LoaderBar index={4} />
</span>
</LoaderContainer>
)
}
const LoaderContainer = styled.div<{
isLoading: boolean
}>`
position: absolute;
top: 50%;
left: 50%;
width: 100px;
height: 80px;
margin-top: -40px;
margin-left: -50px;
background: ${({ theme }) => theme.colors.cardBackground};
display: grid;
grid-template-columns: 10px 10px 10px 10px 10px;
grid-column-gap: 6px;
padding: 10px 13px;
border-radius: 2px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
pointer-events: none;
opacity: ${({ isLoading }) => (isLoading ? 1 : 0)};
transform: translate3d(0, ${({ isLoading }) => (isLoading ? 0 : '20px')}, 0);
transition: transform 200ms, opacity 200ms;
`
const loaderBarAnimation = keyframes`
0%, 100% {
transform: scale3d(1, 0, 1);
}
50% {
transform: scale3d(1, 1, 1);
}
`
const LoaderBar = styled.span<{
index: number
}>`
display: block;
background: ${({ theme }) => theme.colors.accent};
width: 100%;
height: 100%;
transform: scale3d(1, 0, 1);
transform-origin: 100% 100%;
animation-name: ${loaderBarAnimation};
animation-duration: 800ms;
animation-iteration-count: infinite;
animation-delay: ${({ index }) => (index + 1) * 60}ms;
`