-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathRefBlocks.tsx
226 lines (202 loc) · 5.76 KB
/
RefBlocks.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
import { global } from '@storybook/global';
import type { FC } from 'react';
import React, { useState, useCallback, Fragment } from 'react';
import { Icons, WithTooltip, Spaced, Button, Link } from '@storybook/components';
import { logger } from '@storybook/client-logger';
import { styled } from '@storybook/theming';
import { Loader, Contained } from './Loader';
const { window: globalWindow, document } = global;
const TextStyle = styled.div(({ theme }) => ({
fontSize: theme.typography.size.s2 - 1,
lineHeight: '20px',
margin: 0,
}));
const Text = styled.div(({ theme }) => ({
fontSize: theme.typography.size.s2 - 1,
lineHeight: '20px',
margin: 0,
code: {
fontSize: theme.typography.size.s1,
},
ul: {
paddingLeft: 20,
marginTop: 8,
marginBottom: 8,
},
}));
const ErrorDisplay = styled.pre(
{
width: 420,
boxSizing: 'border-box',
borderRadius: 8,
overflow: 'auto',
whiteSpace: 'pre',
},
({ theme }) => ({
color: theme.color.dark,
})
);
const ErrorName = styled.strong(({ theme }) => ({
color: theme.color.orange,
}));
const ErrorImportant = styled.strong(({ theme }) => ({
color: theme.color.ancillary,
textDecoration: 'underline',
}));
const ErrorDetail = styled.em(({ theme }) => ({
color: theme.textMutedColor,
}));
const firstLineRegex = /(Error): (.*)\n/;
const linesRegex = /at (?:(.*) )?\(?(.+)\)?/;
const ErrorFormatter: FC<{ error: Error }> = ({ error }) => {
if (!error) {
return <Fragment>This error has no stack or message</Fragment>;
}
if (!error.stack) {
return <Fragment>{error.message || 'This error has no stack or message'}</Fragment>;
}
const input = error.stack.toString();
const match = input.match(firstLineRegex);
if (!match) {
return <Fragment>{input}</Fragment>;
}
const [, type, name] = match;
const rawLines = input.split(/\n/).slice(1);
const [, ...lines] = rawLines
.map((line) => {
const r = line.match(linesRegex);
return r ? { name: r[1], location: r[2].replace(document.location.origin, '') } : null;
})
.filter(Boolean);
return (
<Fragment>
<span>{type}</span>: <ErrorName>{name}</ErrorName>
<br />
{lines.map((l, i) =>
l.name ? (
// eslint-disable-next-line react/no-array-index-key
<Fragment key={i}>
{' '}at <ErrorImportant>{l.name}</ErrorImportant> (
<ErrorDetail>{l.location}</ErrorDetail>)
<br />
</Fragment>
) : (
// eslint-disable-next-line react/no-array-index-key
<Fragment key={i}>
{' '}at <ErrorDetail>{l.location}</ErrorDetail>
<br />
</Fragment>
)
)}
</Fragment>
);
};
export const AuthBlock: FC<{ loginUrl: string; id: string }> = ({ loginUrl, id }) => {
const [isAuthAttempted, setAuthAttempted] = useState(false);
const refresh = useCallback(() => {
globalWindow.document.location.reload();
}, []);
const open = useCallback((e) => {
e.preventDefault();
const childWindow = globalWindow.open(loginUrl, `storybook_auth_${id}`, 'resizable,scrollbars');
// poll for window to close
const timer = setInterval(() => {
if (!childWindow) {
logger.error('unable to access loginUrl window');
clearInterval(timer);
} else if (childWindow.closed) {
clearInterval(timer);
setAuthAttempted(true);
}
}, 1000);
}, []);
return (
<Contained>
<Spaced>
{isAuthAttempted ? (
<Fragment>
<Text>
Authentication on <strong>{loginUrl}</strong> concluded. Refresh the page to fetch
this Storybook.
</Text>
<div>
<Button small gray onClick={refresh}>
<Icons icon="sync" />
Refresh now
</Button>
</div>
</Fragment>
) : (
<Fragment>
<Text>Sign in to browse this Storybook.</Text>
<div>
<Button small gray onClick={open}>
<Icons icon="lock" />
Sign in
</Button>
</div>
</Fragment>
)}
</Spaced>
</Contained>
);
};
export const ErrorBlock: FC<{ error: Error }> = ({ error }) => (
<Contained>
<Spaced>
<TextStyle>
Oh no! Something went wrong loading this Storybook.
<br />
<WithTooltip
tooltip={
<ErrorDisplay>
<ErrorFormatter error={error} />
</ErrorDisplay>
}
>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<Link isButton>
View error <Icons icon="arrowdown" />
</Link>
</WithTooltip>{' '}
<Link withArrow href="https://storybook.js.org/docs" cancel={false} target="_blank">
View docs
</Link>
</TextStyle>
</Spaced>
</Contained>
);
const FlexSpaced = styled(Spaced)({
display: 'flex',
});
const WideSpaced = styled(Spaced)({
flex: 1,
});
export const EmptyBlock: FC<any> = ({ isMain }) => (
<Contained>
<FlexSpaced col={1}>
<WideSpaced>
<Text>
{isMain ? (
<>
Oh no! Your Storybook is empty. Possible reasons why:
<ul>
<li>
The glob specified in <code>main.js</code> isn't correct.
</li>
<li>No stories are defined in your story files.</li>
</ul>{' '}
</>
) : (
<>Yikes! Something went wrong loading these stories.</>
)}
</Text>
</WideSpaced>
</FlexSpaced>
</Contained>
);
export const LoaderBlock: FC<{ isMain: boolean }> = ({ isMain }) => (
<Contained>
<Loader size={isMain ? 17 : 5} />
</Contained>
);