forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storybook.shared.tsx
281 lines (254 loc) · 7.91 KB
/
storybook.shared.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import * as React from 'react'
import * as Kb from '../common-adapters'
import * as Styles from '../styles'
import * as Container from '../util/container'
import {Provider} from 'react-redux'
import {createStore, applyMiddleware} from 'redux'
import {GatewayProvider, GatewayDest} from '@chardskarth/react-gateway'
import {action} from '@storybook/addon-actions'
import Box from '../common-adapters/box'
import Text from '../common-adapters/text'
import ClickableBox from '../common-adapters/clickable-box'
import RandExp from 'randexp'
import * as PP from './prop-providers'
export type SelectorMap = {[K in string]: (arg0: any) => any | Object}
const unexpected = (name: string) => () => {
throw new Error(`unexpected ${name}`)
}
// On mobile the GatewayDest wrapper needs to fill the entire screen, so we set fillAbsolute
// However on desktop, if the wrapper takes the full screen it will cover the other components
const styleDestBox = Styles.platformStyles({
isElectron: {
position: 'absolute',
},
isMobile: {
...Styles.globalStyles.fillAbsolute,
},
})
// we set pointerEvents to 'box-none' so that the wrapping box will not catch
// touch events and they will be passed down to the child (popup)
class DestBox extends React.Component {
render() {
return <Kb.Box pointerEvents="box-none" style={styleDestBox} {...this.props} />
}
}
/**
* Creates a provider using a faux store of closures that compute derived viewProps
* @param {SelectorMap} map an object of the form {DisplayName: Function(ownProps)} with
* each closure returning the derived viewProps for the connected component
* @returns {React.Node} a <Provider /> that creates a store from the supplied map of closures.
* The Provider will ignore all dispatched actions. It also wraps the component
* tree in an <ErrorBoundary /> that adds auxiliary info in case of an error.
*/
// Redux doesn't allow swapping the store given a single provider so we use a new key to force a new provider to
// work around this issue
// TODO remove this and move to use MockStore instead
let uniqueProviderKey = 1
const createPropProvider = (...maps: SelectorMap[]) => {
const merged: SelectorMap = maps.reduce((obj, merged) => ({...obj, ...merged}), {})
/*
* GatewayDest and GatewayProvider need to be wrapped by the Provider here in
* order for storybook to correctly mock connected components inside of
* popups.
* React.Fragment is used to render StorybookErrorBoundary and GatewayDest as
* children to GatewayProvider which only takes one child
*/
return (story: () => React.ReactNode) => (
<Provider
key={`provider:${uniqueProviderKey++}`}
store={
// @ts-ignore
createStore(state => state, merged)
}
// @ts-ignore
merged={merged}
>
<GatewayProvider>
<>
<StorybookErrorBoundary children={story()} />
<GatewayDest component={DestBox} name="popup-root" />
</>
</GatewayProvider>
</Provider>
)
}
// Plumb dispatches through storybook actions panel
const actionLog = () => (next: any) => (a: any) => {
action('ReduxDispatch')(a)
return next(a)
}
// Includes common old-style propProvider temporarily
export const MockStore = ({store, children}: any): any => (
<Provider
key={`storyprovider:${uniqueProviderKey++}`}
store={createStore(state => state, {...store, ...PP.Common()}, applyMiddleware(actionLog))}
// @ts-ignore
merged={store}
>
<GatewayProvider>
<>
<StorybookErrorBoundary children={children} />
<GatewayDest component={DestBox} name="popup-root" />
</>
</GatewayProvider>
</Provider>
)
type updateFn = (draftState: Container.TypedState) => void
export const updateStoreDecorator = (store: Container.TypedState, update: updateFn) => (story: any) => (
<MockStore store={Container.produce(store, update)}>{story()}</MockStore>
)
export const createNavigator = (params: any) => ({
navigation: {
getParam: (key: any) => params[key],
},
})
class StorybookErrorBoundary extends React.Component<
any,
{
hasError: boolean
error: Error | null
info: {
componentStack: string
} | null
}
> {
constructor(props: any) {
super(props)
this.state = {error: null, hasError: false, info: null}
// Disallow catching errors when snapshot testing
if (!__STORYSHOT__) {
this.componentDidCatch = (
error: Error,
info: {
componentStack: string
}
) => {
this.setState({error, hasError: true, info})
}
} else {
this.componentDidCatch = undefined
}
}
render() {
if (this.state.hasError) {
return (
<Kb.Box
style={{
...Styles.globalStyles.flexBoxColumn,
borderColor: Styles.globalColors.red_75,
borderStyle: 'solid',
borderWidth: 2,
padding: 10,
}}
>
<Kb.Text type="Terminal" style={{color: Styles.globalColors.black, marginBottom: 8}}>
🛑 An error occurred in a connected child component. Did you supply all props the child expects?
</Kb.Text>
<Kb.Box
style={{
...Styles.globalStyles.flexBoxColumn,
backgroundColor: Styles.globalColors.blueDarker2,
borderRadius: Styles.borderRadius,
padding: 10,
whiteSpace: 'pre-line',
}}
>
<Kb.Text type="Terminal" negative={true} selectable={true}>
{this.state.error && this.state.error.toString()}
{this.state.info && this.state.info.componentStack}
</Kb.Text>
</Kb.Box>
</Kb.Box>
)
}
return this.props.children
}
}
/**
* Utilities for writing stories
*/
class Rnd {
_seed = 0
constructor(seed: number | string) {
if (typeof seed === 'string') {
this._seed = seed.split('').reduce((acc, _, i) => seed.charCodeAt(i) + acc, 0)
} else {
this._seed = seed
}
}
next = () => {
this._seed = (this._seed * 16807) % 2147483647
return this._seed
}
// Inclusive
randInt = (low: number, high: number) => (this.next() % (high + 1 - low)) + low
generateString = (regex: RegExp): string => {
const r = new RandExp(regex)
r.randInt = this.randInt
return r.gen()
}
}
const scrollViewDecorator = (story: any) => (
<Kb.ScrollView style={{height: '100%', width: '100%'}}>{story()}</Kb.ScrollView>
)
class PerfBox extends React.Component<
{
copiesToRender: number
children: React.ReactNode
},
{
key: number
}
> {
state = {key: 1}
_text = null
_startTime = 0
_endTime = 0
_incrementKey = () => {
this.setState(old => ({key: old.key + 1}))
}
_updateTime = () => {
this._endTime = this._getTime()
const diff = this._endTime - this._startTime
console.log('PerfTiming: ', diff)
}
_getTime = () => {
const perf: any = window ? window.performance : undefined
if (typeof perf !== 'undefined') {
return perf.now()
} else {
return Date.now()
}
}
render() {
this._startTime = this._getTime()
setTimeout(this._updateTime, 0)
return (
<Box key={this.state.key}>
<ClickableBox onClick={this._incrementKey}>
<Text type="Body">Refresh: #{this.state.key}</Text>
</ClickableBox>
{new Array(this.props.copiesToRender).fill(0).map((_, idx) => (
<Box key={idx}>{this.props.children}</Box>
))}
</Box>
)
}
}
const perfDecorator = (copiesToRender: number = 100) => (story: any) => (
<PerfBox copiesToRender={copiesToRender}>{story()} </PerfBox>
)
// Used to pass extra props to a component in a story without flow typing
const propOverridesForStory = (p: any): {} => ({
storyProps: p,
})
export {
unexpected,
createPropProvider,
propOverridesForStory,
StorybookErrorBoundary,
Rnd,
scrollViewDecorator,
action,
perfDecorator,
}