-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclient.jsx
71 lines (62 loc) · 1.51 KB
/
client.jsx
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
import React, { useState } from "react";
import ReactDOM from "react-dom";
// I haven't used any of this bs in a year, hopefully it still the same
/// Look mum! I am so cool, I put strings into a different file
function css(strings) {
let style = document.getElementById("css-in-js-style-thing");
if (!style) {
style = document.createElement("style");
style.id = "css-in-js-style-thing";
document.head.appendChild(style);
}
const randomClassId = Math.random().toString(36).slice(2);
style.textContent += `
.${randomClassId} {
${strings[0]}
}`;
return randomClassId;
}
async function runC(code) {
const res = await fetch("/rpc/rce", {
method: "POST",
body: JSON.stringify({ code: code }),
headers: {
"Content-Type": "application/json",
},
});
return await res.json();
}
const appStyles = css`
margin: 3rem auto;
max-width: 600px;
padding: 0 1rem;
`;
const App = () => (
<div className={appStyles}>
<h1>React use C demo</h1>
<Demo />
</div>
);
const Demo = () => {
const [output, setOutput] = useState(null);
const cHelloWorld = async () => {
"use c";
#include <stdio.h>
int main() {
printf("Hello from C!\n");
return 0;
}
};
const onClick = async () => {
const out = await cHelloWorld();
setOutput(out.stdout);
};
return (
<div>
<button onClick={onClick}>Run C code!</button>
<p>Output from C:</p>
<pre>{ output }</pre>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("app"));