forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverlay.tsx
More file actions
173 lines (157 loc) · 3.71 KB
/
overlay.tsx
File metadata and controls
173 lines (157 loc) · 3.71 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
import type { ComponentChildren } from "preact";
// Just to get some syntax highlighting
const css = (arr: TemplateStringsArray, ...exts: never[]) => {
if (exts.length) throw new Error("Not allowed");
return arr[0];
};
export const errorCss = css`
:root {
--bg: #fff;
--bg-code-frame: rgb(255, 0, 32, 0.1);
--bg-active-line: #fbcecc;
--text: #222;
--text2: #444;
--title: #e84644;
--code: #333;
font-family: sans-serif;
line-height: 1.4;
color: var(--text);
background: var(--bg);
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
@media (prefers-color-scheme: dark) {
:root {
--bg-code-frame: rgba(251, 93, 113, 0.2);
--bg-active-line: #4f1919;
--bg: #353535;
--text: #f7f7f7;
--text2: #ddd;
--code: #fdd1d1;
}
}
.inner {
max-width: 48rem;
padding: 4rem 1rem;
margin: 0 auto;
}
.title {
color: var(--title);
font-weight: normal;
font-size: 1.5rem;
margin-bottom: 1rem;
}
.code-frame {
overflow: auto;
padding: 0.5rem;
margin-bottom: 0.5rem;
background: var(--bg-code-frame);
color: var(--code);
}
.line {
padding: 0.25rem 0.5rem;
}
.active-line {
display: inline-block;
width: 100%;
background: var(--bg-active-line);
}
.stack {
overflow-x: auto;
}
.close-btn {
position: absolute;
top: 1rem;
right: 1rem;
color: var(--title);
display: block;
width: 3rem;
height: 3rem;
background: none;
border: none;
transform: translate3d(0, 0, 0);
}
.close-btn:active {
transform: translate3d(0, 2px, 0);
}
.close-btn:hover {
cursor: pointer;
filter: drop-shadow(0 0 0.75rem crimson);
}
`;
function CodeFrame(props: { codeFrame: string }) {
const lines: ComponentChildren[] = [];
props.codeFrame.trimEnd().split("\n").forEach(
(line, i, arr) => {
const vnode = (
<span
class={"line" + (line.startsWith(">") ? " active-line" : "")}
>
{line}
</span>
);
lines.push(vnode);
if (i < arr.length - 1) lines.push("\n");
},
);
return (
<pre class="code-frame">
<code>{lines}</code>
</pre>
);
}
const DEFAULT_MESSAGE = "Internal Server Error";
export function ErrorOverlay(props: { url: URL }) {
const url = props.url;
const title = url.searchParams.get("message") || DEFAULT_MESSAGE;
const stack = url.searchParams.get("stack");
const codeFrame = url.searchParams.get("code-frame");
if (title === DEFAULT_MESSAGE && !stack && !codeFrame) {
return null;
}
return (
<>
<div class="frsh-error-page">
<style dangerouslySetInnerHTML={{ __html: errorCss }} />
<div class="inner">
<div class="header">
<button
type="button"
class="close-btn"
aria-label="close"
id="close-btn"
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9.75 9.75l4.5 4.5m0-4.5l-4.5 4.5M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
</button>
</div>
<div>
<h1 class="title">{title}</h1>
{codeFrame ? <CodeFrame codeFrame={codeFrame} /> : null}
{stack ? <pre class="stack">{stack}</pre> : null}
</div>
</div>
</div>
<script
dangerouslySetInnerHTML={{
__html:
`document.querySelector("#close-btn").addEventListener("click", () => parent.postMessage("close-error-overlay"));`,
}}
/>
</>
);
}