File size: 1,798 Bytes
7df1b93
ce7e38e
762231c
d6ea0f8
 
ce7e38e
d6ea0f8
ce7e38e
 
 
d6ea0f8
 
ce7e38e
d6ea0f8
ce7e38e
5dabbcf
 
d6ea0f8
5dabbcf
ce7e38e
d6ea0f8
ce7e38e
 
 
5f97286
 
 
 
 
 
d6ea0f8
 
 
 
 
 
ce7e38e
d6ea0f8
 
 
 
e6559cd
ce7e38e
d6ea0f8
 
 
 
e6559cd
d6ea0f8
 
 
 
 
 
ce7e38e
 
 
8c4e60e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useEffect, useState, useRef } from "react";

export default function FileUpload({ onCodeParsed, reset }) {
  const [preview, setPreview] = useState(null);
  const inputRef = useRef(null); // โœ… create the ref

  const handleFileChange = (e) => {
    const file = e.target.files[0];
    if (!file) return;

    setPreview({ name: file.name, size: file.size });

    const reader = new FileReader();

    reader.onload = () => {
      const code = reader.result;
      if (typeof onCodeParsed === "function") {
        onCodeParsed(code, file.name);
      }
    };

    reader.readAsText(file);
  };

 // โœ… Reset file input and preview when `reset` changes
  useEffect(() => {
    setPreview(null);
    if (inputRef.current) inputRef.current.value = "";
  }, [reset]);

  const triggerUpload = () => {
    if (inputRef.current) {
      inputRef.current.click(); // โœ… safe
    }
  };

  return (
    <div style={wrapperStyle}>
      <button onClick={triggerUpload} style={labelStyle}>
        ๐Ÿ“ Upload Code
      </button>
      <input
        ref={inputRef}
        type="file"
        accept=".js,.ts,.py,.java,.cpp,.cs,.txt"
        onChange={handleFileChange}
        hidden
      />

      {preview && (
        <div style={previewStyle}>
          <strong>{preview.name}</strong> โ€” {(preview.size / 1024).toFixed(1)} KB
        </div>
      )}
    </div>
  );
}

const wrapperStyle = {
  display: "flex",
  flexDirection: "column",
  alignItems: "flex-start",
};

const labelStyle = {
  display: "inline-block",
  padding: "8px 16px",
  backgroundColor: "#1e1e2f",
  color: "#fff",
  borderRadius: "8px",
  cursor: "pointer",
  border: "1px solid #444",
  marginBottom: "6px",
  fontSize: "0.95rem",
};

const previewStyle = {
  color: "#ccc",
  fontSize: "0.85rem",
};