Spaces:
Running
Running
Commit ·
d6ea0f8
1
Parent(s): e6559cd
commit 018
Browse files- src/components/FileUpload.jsx +29 -12
src/components/FileUpload.jsx
CHANGED
|
@@ -1,34 +1,51 @@
|
|
| 1 |
-
import { useRef } from "react";
|
| 2 |
|
| 3 |
export default function FileUpload({ onCodeParsed }) {
|
| 4 |
-
const
|
|
|
|
| 5 |
|
| 6 |
-
const
|
| 7 |
const file = e.target.files[0];
|
| 8 |
if (!file) return;
|
| 9 |
|
|
|
|
|
|
|
| 10 |
const reader = new FileReader();
|
|
|
|
| 11 |
reader.onload = () => {
|
| 12 |
const code = reader.result;
|
| 13 |
if (typeof onCodeParsed === "function") {
|
| 14 |
-
onCodeParsed(code, file.name);
|
| 15 |
}
|
| 16 |
};
|
|
|
|
| 17 |
reader.readAsText(file);
|
| 18 |
};
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return (
|
| 21 |
-
<div
|
|
|
|
|
|
|
|
|
|
| 22 |
<input
|
| 23 |
-
type="file"
|
| 24 |
-
accept=".js,.ts,.py,.java,.json,.txt"
|
| 25 |
ref={inputRef}
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
/>
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
</div>
|
| 33 |
);
|
| 34 |
}
|
|
|
|
| 1 |
+
import React, { useState, useRef } from "react";
|
| 2 |
|
| 3 |
export default function FileUpload({ onCodeParsed }) {
|
| 4 |
+
const [preview, setPreview] = useState(null);
|
| 5 |
+
const inputRef = useRef(null); // ✅ create the ref
|
| 6 |
|
| 7 |
+
const handleFileChange = (e) => {
|
| 8 |
const file = e.target.files[0];
|
| 9 |
if (!file) return;
|
| 10 |
|
| 11 |
+
setPreview({ name: file.name, size: file.size });
|
| 12 |
+
|
| 13 |
const reader = new FileReader();
|
| 14 |
+
|
| 15 |
reader.onload = () => {
|
| 16 |
const code = reader.result;
|
| 17 |
if (typeof onCodeParsed === "function") {
|
| 18 |
+
onCodeParsed(code, file.name);
|
| 19 |
}
|
| 20 |
};
|
| 21 |
+
|
| 22 |
reader.readAsText(file);
|
| 23 |
};
|
| 24 |
|
| 25 |
+
const triggerUpload = () => {
|
| 26 |
+
if (inputRef.current) {
|
| 27 |
+
inputRef.current.click(); // ✅ safe
|
| 28 |
+
}
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
return (
|
| 32 |
+
<div style={wrapperStyle}>
|
| 33 |
+
<button onClick={triggerUpload} style={labelStyle}>
|
| 34 |
+
📁 Upload Code
|
| 35 |
+
</button>
|
| 36 |
<input
|
|
|
|
|
|
|
| 37 |
ref={inputRef}
|
| 38 |
+
type="file"
|
| 39 |
+
accept=".js,.ts,.py,.java,.cpp,.cs,.txt"
|
| 40 |
+
onChange={handleFileChange}
|
| 41 |
+
hidden
|
| 42 |
/>
|
| 43 |
+
|
| 44 |
+
{preview && (
|
| 45 |
+
<div style={previewStyle}>
|
| 46 |
+
<strong>{preview.name}</strong> — {(preview.size / 1024).toFixed(1)} KB
|
| 47 |
+
</div>
|
| 48 |
+
)}
|
| 49 |
</div>
|
| 50 |
);
|
| 51 |
}
|