-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.tsx
224 lines (182 loc) · 7.12 KB
/
ui.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
import React from 'npm:react@18.3.1';
import { render, Text, Box } from 'npm:ink@4.4.1';
import { format } from "jsr:@wilcosp/ms-relative@0.0.5";
import { checkDownTime } from "./main.ts";
// import Link from 'npm:ink-link@3.0.0';
import BigText from 'npm:ink-big-text@2.0.0';
import Gradient from 'npm:ink-gradient@3.0.0';
import * as Marked from 'npm:marked@4.2.12';
import * as Renderer from 'npm:marked-terminal@5.1.1';
import chalk from 'npm:chalk@5.2.0';
const { useState, useEffect } = React;
const startTime = Date.now();
function App({ urls, timeout, sleep, maxTime }: { urls: string[], timeout: number, sleep: number, maxTime: number }) {
useEffect(() => {
const INTERVAL = totalTime < 60_000 ? 1000 : 60_000;
const interval = setInterval(() => {
setTotalTime(Date.now() - startTime);
}, INTERVAL);
return () => clearInterval(interval);
}, []);
const [totalTime, setTotalTime] = useState(0);
const infoColor = "grey";
const flexDirection = "column";
return (
<Box gap={1} flexDirection="column">
<Title >Downtime</Title>
<Box flexDirection="row" gap={1} paddingTop={1}>
<Text color={infoColor} dimColor>From:</Text><Text color={infoColor}>{new Date(startTime).toLocaleString()}</Text>
<Text color={infoColor} dimColor>Total:</Text><Text color={infoColor}>{ms(totalTime)}</Text>
<Text color={infoColor} dimColor>--sleep</Text><Text color={infoColor}>{ms(sleep)}</Text>
<Text color={infoColor} dimColor>--timeout</Text><Text color={infoColor}>{ms(timeout)}</Text>
<Text color={infoColor} dimColor>--maxTime</Text><Text color={infoColor}>{ms(maxTime || Infinity)}</Text>
<Text dimColor>ctrl + c to close</Text>
</Box>
<Box gap={0} flexDirection={flexDirection} wrap >
{urls.map((url) => (
<UrlMonitor key={url} url={url} timeout={timeout} sleep={sleep} />
))}
</Box>
</Box>
)
}
function Title({ children }: { children?: string }) {
return (
<Box>
<Gradient name="passion">
<BigText text={children} font="simple" space={false} letterSpacing={0} />
</Gradient>
</Box>
);
}
function UrlMonitor({ url, timeout, sleep }: { url: string, key?: string, timeout: number, sleep: number }) {
try {
new URL(url);
} catch (error) {
return (
<Box padding={1}>
<Text color="red">{error.message}</Text>
</Box>
)
}
const [downTimeElapsed, setDownTimeElapsed] = useState(0);
const [upTimeElapsed, setUpTimeElapsed] = useState(0);
const [status, setStatus] = useState(' ');
const [errorMessages, setErrorMessages] = useState('');
useEffect(() => {
const controller = new AbortController();
const { signal } = controller;
const generator = checkDownTime(url, { signal, timeout, sleep });
(async () => {
for await (const data of generator) {
setDownTimeElapsed(data.totalDownTimeElapsed);
setUpTimeElapsed(data.totalUpTimeElapsed);
setStatus(data.lastResponse?.status || 0);
setErrorMessages(data.lastError?.message);
}
})();
return () => {
controller.abort();
};
}, [url]);
return (
<Box flexDirection="column" >
<Box flexDirection="row" gap={1} justifyContent="space-between" flexWrap="wrap">
<Box gap={1} flexShrink={0}>
{
status ?
<Text bold color={statusColor(status)}>► {status}</Text> :
<Text color="red">{'■ '}</Text>
}
<Text color="cyan">{url}</Text>
{/* <Link url={url} fallback={false}>
</Link> */}
</Box>
<Box gap={1} flexGrow={1} flexShrink={0} justifyContent="flex-end">
<Text color='red' inverse> {msPad(downTimeElapsed)} {"🔻"}</Text>
{/* <Text color="gray" dimColor></Text> */}
<Text color='green'>{msPad(upTimeElapsed)} {"🟢 "}</Text>
{/* <Text color="gray" dimColor></Text> */}
</Box>
</Box>
{
errorMessages
? <Box paddingTop={1} paddingLeft={3} paddingBottom={1}><Text color="red"
inverse>{errorMessages}</Text></Box>
: null
}
</Box>
);
}
function statusColor(status: number) {
if (status >= 200 && status < 300) return "#00ff00";
if (status >= 300 && status < 400) return "#ffff00";
if (status >= 400 && status < 500) return "#ff7f00";
return "#ff0000";
}
function ms(time: number): string {
if (time < 0) {
return '0 s';
}
if (time === Infinity) {
return '∞';
}
const out = format(time, { style: "short", max: 2, locale: "es-ES" });
return out || '0 s';
}
function msPad(time: number, pad: number = 11): string {
return ms(time).padStart(pad, ' ');
}
function Markdown({ children }: { children: string }) {
const marked = Marked.marked;
const TerminalRenderer = Renderer.default;
const defaultOptions = {
// Colors
code: chalk.magenta,
codespan: chalk.magentaBright,
blockquote: chalk.dim.italic,
html: chalk.gray,
heading: chalk.green.bold,
firstHeading: chalk.magenta.underline.bold,
hr: chalk.reset,
listitem: chalk.reset,
table: chalk.reset,
paragraph: chalk.reset,
strong: chalk.bold,
em: chalk.italic,
del: chalk.dim.gray.strikethrough,
link: chalk.blue,
href: chalk.blue.underline,
// Formats the bullet points and numbers for lists
// list: function (_body, _ordered) {},
// Reflow and print-out width
width: 80, // only applicable when reflow is true
reflowText: false,
// Should it prefix headers?
showSectionPrefix: true,
// Whether or not to undo marked escaping
// of entities (" -> " etc)
unescape: true,
// Whether or not to show emojis
emoji: true,
// Options passed to cli-table3
tableOptions: {},
// The size of tabs in number of spaces or as tab characters
tab: 3 // examples: 4, 2, \t, \t\t
// image: function (_href, _title, _text) {} // function for overriding the default image handling.
};
marked.setOptions({
renderer: new TerminalRenderer(defaultOptions)
});
return (
<Text>
{marked(children)}
</Text>
);
}
export function renderUI(urls: string[], sleep: number, timeout: number, maxTime: number) {
return render(<App urls={urls} timeout={timeout} sleep={sleep} maxTime={maxTime} />);
}
export function renderMarkdown(markdown: string) {
return render(<Markdown children={markdown}></Markdown>);
}