-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
232 lines (217 loc) · 8.81 KB
/
Copy pathindex.js
File metadata and controls
232 lines (217 loc) · 8.81 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
import TextBox from "../components/TextBox";
import ResultBox from "../components/ResultBox";
import LoadLFortran from "../components/LoadLFortran";
import preinstalled_programs from "../utils/preinstalled_programs";
import { useIsMobile } from "../components/useIsMobile";
import { useState, useEffect, useRef } from "react";
import { Col, Row, Spin, notification } from "antd"; // Combined into one line
import { LoadingOutlined, ShareAltOutlined } from "@ant-design/icons"; // Added Share icon
import AnsiUp from "ansi_up";
// ONLY add this for Issue #23
import { decodeSnippet } from "../utils/snippet";
var ansi_up = new AnsiUp();
const antIcon = (
<LoadingOutlined
style={{
fontSize: 24,
}}
spin
/>
);
const openNotification = (msg, placement) => {
notification.info({
message: msg,
placement,
});
};
var lfortran_funcs = {
emit_ast_from_source: null,
emit_asr_from_source: null,
emit_wat_from_source: null,
emit_wasm_from_source: null,
emit_cpp_from_source: null,
emit_py_from_source: null,
compile_code: null,
execute_code: null,
};
export default function Home() {
const [moduleReady, setModuleReady] = useState(false);
const [sourceCode, setSourceCode] = useState("");
const [sourceUrl, setSourceUrl] = useState("");
const [exampleName, setExampleName] = useState("main");
const [activeTab, setActiveTab] = useState("STDOUT");
const [output, setOutput] = useState("");
const [dataFetch, setDataFetch] = useState(false);
const isMobile = useIsMobile();
const initialized = useRef(false);
const myHeight = ((!isMobile) ? "calc(100vh - 170px)" : "calc(50vh - 85px)");
// Consolidated Effect: Handles mounting logic exactly once
useEffect(() => {
if (!initialized.current) {
initialized.current = true;
setSourceCode(""); // Clear initial state
fetchData(); // Trigger the fetch
}
}, []);
// Handles the automatic run/tab change once data is ready
useEffect(() => {
if(moduleReady && dataFetch) {
handleUserTabChange("STDOUT");
}
}, [moduleReady, dataFetch]);
async function fetchData() {
const urlParams = new URLSearchParams(window.location.search);
let downloadUrl = "";
// Case 1: Shared Snippet (Issue #23)
if (urlParams.get("snippet")) {
downloadUrl = decodeSnippet(urlParams.get("snippet"));
}
// Case 2: Direct URL Parameter (Direct Raw Link)
else if (urlParams.get("url")) {
downloadUrl = urlParams.get("url");
}
// Case 4: Direct Code
else if (urlParams.get("code")) {
setSourceCode(decodeURIComponent(urlParams.get("code")));
setSourceUrl("");
setDataFetch(true);
return;
}
// Execution: Fetch only if a downloadUrl was successfully determined
if (downloadUrl) {
fetch(downloadUrl, { cache: "no-store" })
.then((response) => {
if (!response.ok) throw new Error("Fetch failed");
return response.text();
})
.then((data) => {
setSourceCode(data);
setSourceUrl(downloadUrl); // Enable Share button
setDataFetch(true);
openNotification("Source Code loaded successfully.", "bottomRight");
})
.catch((error) => {
console.error("Fetch error:", error);
// Only one notification will now appear due to the Ref guard
openNotification("Error: Please provide a direct download link.", "bottomRight");
setSourceCode(preinstalled_programs.basic.mandelbrot);
setSourceUrl("");
setDataFetch(true);
});
}
else {
// Default behavior if no valid download parameters are present
setSourceCode(preinstalled_programs.basic.mandelbrot);
setSourceUrl("");
setDataFetch(true);
// Only notify for invalid/unsupported parameters if the URL isn't empty
const hasParams = urlParams.keys().next().done === false;
if (hasParams && !urlParams.get("code") && !urlParams.get("gist") && !urlParams.get("url") && !urlParams.get("snippet")) {
openNotification("The URL contains an invalid parameter.", "bottomRight");
}
}
}
async function handleUserTabChange(key) {
if (key == "STDOUT") {
if(sourceCode.trim() === ""){
setOutput("No Source Code to compile");
setActiveTab(key);
return;
}
const start_compile = performance.now();
const wasm_bytes_response = lfortran_funcs.compile_code(sourceCode);
const end_compile = performance.now();
const duration_compile = end_compile - start_compile;
sessionStorage.setItem("duration_compile", duration_compile);
if (wasm_bytes_response) {
const [exit_code, ...compile_result] = wasm_bytes_response.split(",");
if (exit_code !== "0") {
// print compile-time error found by lfortran to output
setOutput(ansi_up.ansi_to_html(compile_result) + `\nCompilation Time: ${duration_compile} ms`);
}
else {
var stdout = [];
const exec_res = await lfortran_funcs.execute_code(
new Uint8Array(compile_result),
(text) => stdout.push(text)
);
setOutput(stdout.join(""));
}
}
} else if (key == "AST") {
const res = lfortran_funcs.emit_ast_from_source(sourceCode);
if (res) {
setOutput(ansi_up.ansi_to_html(res));
}
} else if (key == "ASR") {
const res = lfortran_funcs.emit_asr_from_source(sourceCode);
if (res) {
setOutput(ansi_up.ansi_to_html(res));
}
} else if (key == "WAT") {
const res = lfortran_funcs.emit_wat_from_source(sourceCode);
if (res) {
setOutput(ansi_up.ansi_to_html(res));
}
} else if (key == "CPP") {
const res = lfortran_funcs.emit_cpp_from_source(sourceCode);
if (res) {
setOutput(ansi_up.ansi_to_html(res));
}
} else if (key == "PY") {
setOutput("Support for PY is not yet enabled");
} else {
setOutput("Unknown key: " + key);
}
setActiveTab(key);
}
return (
<>
<LoadLFortran
moduleReady={moduleReady}
setModuleReady={setModuleReady}
lfortran_funcs={lfortran_funcs}
openNotification={openNotification}
myPrint={setOutput}
></LoadLFortran>
<Row gutter={[16, 16]}>
<Col xs={{ span: 24 }} sm={{ span: 24 }} md={{ span: 12 }}>
<TextBox
disabled={!moduleReady}
sourceCode={sourceCode}
setSourceCode={setSourceCode}
sourceUrl={sourceUrl} // New prop for sharing
setSourceUrl={setSourceUrl} // Allow clearing if code changes manually
exampleName={exampleName}
setExampleName={setExampleName}
activeTab={activeTab}
handleUserTabChange={handleUserTabChange}
myHeight={myHeight}
></TextBox>
</Col>
<Col xs={{ span: 24 }} sm={{ span: 24 }} md={{ span: 12 }}>
{moduleReady ? (
<ResultBox
activeTab={activeTab}
output={output}
handleUserTabChange={handleUserTabChange}
myHeight={myHeight}
openNotification={openNotification}
></ResultBox>
) : (
<div style={{height: myHeight}}>
<Spin
style={{
position: "relative",
top: "50%",
left: "50%",
}}
indicator={antIcon}
/>
</div>
)}
</Col>
</Row>
</>
);
}