-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathDevOverlayDialog.tsx
More file actions
316 lines (288 loc) · 10.2 KB
/
DevOverlayDialog.tsx
File metadata and controls
316 lines (288 loc) · 10.2 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// @refresh skip
import ErrorStackParser from "error-stack-parser";
import * as htmlToImage from "html-to-image";
import type { JSX } from "solid-js";
import { createMemo, createSignal, ErrorBoundary, For, Show, Suspense } from "solid-js";
import { Portal } from "solid-js/web";
// @ts-ignore - terracotta module resolution issue with NodeNext
import { Dialog, DialogOverlay, DialogPanel, Select, SelectOption } from "terracotta";
import info from "../../../package.json" with { type: "json" };
import { CodeView } from "./CodeView.tsx";
import { createStackFrame, type StackFrameSource } from "./createStackFrame.ts";
import download from "./download.ts";
import {
ArrowLeftIcon,
ArrowRightIcon,
CameraIcon,
DiscordIcon,
GithubIcon,
RefreshIcon,
SolidStartIcon,
ViewCompiledIcon,
ViewOriginalIcon,
} from "./icons.tsx";
import "./styles.css";
export function classNames(...classes: (string | boolean | undefined)[]): string {
return classes.filter(Boolean).join(" ");
}
interface ErrorInfoProps {
error: unknown;
}
function ErrorInfo(props: ErrorInfoProps): JSX.Element {
const error = createMemo(() => {
const e = props.error;
if (e instanceof Error) {
return { name: e.name, message: e.message };
}
if (e instanceof ErrorEvent) {
return { message: e.message };
}
return { message: (e as Error).toString() };
});
return (
<span data-start-dev-overlay-error-info>
<span data-start-dev-overlay-error-info-name>{error().name}</span>
<span data-start-dev-overlay-error-info-message>{error().message}</span>
</span>
);
}
interface StackFramesContentProps {
error: Error;
isCompiled: boolean;
}
function getFileName(source: string): string {
try {
const path = source.startsWith("/") ? new URL(source, "file://") : new URL(source);
const paths = path.pathname.split("/");
return paths[paths.length - 1]!;
} catch (error) {
return getFileName(`/${source}`);
}
}
function getFilePath(source: StackFrameSource) {
const line = source.line ? `:${source.line}` : "";
const column = source.column ? `:${source.column}` : "";
return `${getFileName(source.source)}${line}${column}`;
}
function CodeFallback(): JSX.Element {
return (
<div data-start-dev-overlay-stack-frames-code-fallback>
<span>Source not available.</span>
</div>
);
}
function StackFramesContent(props: StackFramesContentProps) {
const stackframes = ErrorStackParser.parse(props.error);
const [selectedFrame, setSelectedFrame] = createSignal(stackframes[0]!);
return (
<div data-start-dev-overlay-stack-frames-content>
<div data-start-dev-overlay-stack-frames-code>
<ErrorBoundary fallback={null}>
{(() => {
const data = createStackFrame(selectedFrame(), () => props.isCompiled);
return (
<Suspense fallback={<CodeFallback />}>
<Show when={data()} keyed fallback={<CodeFallback />}>
{source => (
<>
<span data-start-dev-overlay-stack-frames-code-source>{source.source}</span>
<div data-start-dev-overlay-stack-frames-code-container>
<CodeView
fileName={source.source}
line={source.line}
content={source.content}
/>
</div>
</>
)}
</Show>
</Suspense>
);
})()}
</ErrorBoundary>
</div>
<Select<ErrorStackParser.StackFrame>
data-start-dev-overlay-stack-frames
value={selectedFrame()}
onChange={setSelectedFrame}
>
<For each={stackframes}>
{current => (
<ErrorBoundary
fallback={
<div data-start-dev-overlay-stack-frame>
<span data-start-dev-overlay-stack-frame-function>
{current.functionName ?? "<anonymous>"}
</span>
<span data-start-dev-overlay-stack-frame-file>
{getFilePath({
source: current.getFileName()!,
content: "",
line: current.getLineNumber()!,
column: current.getColumnNumber()!,
name: current.getFunctionName(),
})}
</span>
</div>
}
>
{(() => {
const data = createStackFrame(current, () => props.isCompiled);
return (
<Suspense>
<Show when={data()} keyed>
{source => (
<SelectOption data-start-dev-overlay-stack-frame value={current}>
<span data-start-dev-overlay-stack-frame-function>
{source.name ?? "<anonymous>"}
</span>
<span data-start-dev-overlay-stack-frame-file>{getFilePath(source)}</span>
</SelectOption>
)}
</Show>
</Suspense>
);
})()}
</ErrorBoundary>
)}
</For>
</Select>
</div>
);
}
interface StackFramesProps {
error: unknown;
isCompiled: boolean;
}
function StackFrames(props: StackFramesProps) {
return (
<Show when={props.error instanceof Error && props.error} keyed>
{current => <StackFramesContent error={current} isCompiled={props.isCompiled} />}
</Show>
);
}
interface DevOverlayDialogProps {
errors: any[];
resetError: () => void;
}
const ISSUE_THREAD = "https://github.com/solidjs/solid-start/issues/new";
const DISCORD_INVITE = "https://discord.com/invite/solidjs";
export default function DevOverlayDialog(props: DevOverlayDialogProps): JSX.Element {
const [currentPage, setCurrentPage] = createSignal(1);
const [isCompiled, setIsCompiled] = createSignal(false);
const length = createMemo(() => props.errors.length);
const truncated = createMemo(() => {
return Math.min(currentPage(), length());
});
function goPrev() {
setCurrentPage(c => {
if (c > 1) {
return c - 1;
}
return length();
});
}
function goNext() {
setCurrentPage(c => {
if (c < length()) {
return c + 1;
}
return 1;
});
}
function toggleIsCompiled() {
setIsCompiled(c => !c);
}
const [panel, setPanel] = createSignal<HTMLElement>();
function downloadScreenshot() {
const current = panel();
if (current) {
htmlToImage
.toPng(current, {
style: {
transform: "scale(0.75)",
},
})
.then(url => {
download(url, "start-screenshot.png");
});
}
}
function redirectToGithub() {
const url = new URL(ISSUE_THREAD);
url.searchParams.append("labels", "bug");
url.searchParams.append("labels", "needs+triage");
url.searchParams.append("template", "bug.yml");
url.searchParams.append("title", `[Bug?]:` + props.errors[truncated() - 1].toString());
window.open(url, "_blank")!.focus();
}
function redirectToDiscord() {
window.open(DISCORD_INVITE, "_blank")!.focus();
}
return (
<Portal>
<Dialog data-start-dev-overlay isOpen>
<div>
<DialogOverlay data-start-dev-overlay-background />
<DialogPanel ref={setPanel} data-start-dev-overlay-panel-container>
<div data-start-dev-overlay-panel>
<div data-start-dev-overlay-navbar>
<div data-start-dev-overlay-navbar-left>
<div data-start-dev-overlay-version>
<div>
<SolidStartIcon title="Solid Start Version" />
</div>
<span>{info.version as string}</span>
</div>
<Show when={props.errors.length > 1}>
<div data-start-dev-overlay-pagination>
<button data-start-dev-overlay-button onClick={goPrev} type="button">
<ArrowLeftIcon title="Go Previous" />
</button>
<div data-start-dev-overlay-page-counter>
{`${truncated()} of ${props.errors.length}`}
</div>
<button data-start-dev-overlay-button onClick={goNext} type="button">
<ArrowRightIcon title="Go Next" />
</button>
</div>
</Show>
</div>
<div data-start-dev-overlay-controls>
<button data-start-dev-overlay-button onClick={redirectToGithub} type="button">
<GithubIcon title="Create an issue thread on Github" />
</button>
<button data-start-dev-overlay-button onClick={redirectToDiscord} type="button">
<DiscordIcon title="Join our Discord Channel" />
</button>
<button data-start-dev-overlay-button onClick={downloadScreenshot} type="button">
<CameraIcon title="Capture Error Overlay" />
</button>
<button data-start-dev-overlay-button onClick={toggleIsCompiled} type="button">
<Show
when={isCompiled()}
fallback={<ViewOriginalIcon title="View Original Source" />}
>
<ViewCompiledIcon title="View Compiled Source" />
</Show>
</button>
<button data-start-dev-overlay-button onClick={props.resetError} type="button">
<RefreshIcon title="Reset Error" />
</button>
</div>
</div>
<Show when={props.errors[truncated() - 1]} keyed>
{current => (
<div data-start-dev-overlay-content>
<ErrorInfo error={current} />
<StackFrames error={current} isCompiled={isCompiled()} />
</div>
)}
</Show>
</div>
</DialogPanel>
</div>
</Dialog>
</Portal>
);
}