|
| 1 | +import { useState, useRef } from "react"; |
| 2 | + |
| 3 | +interface DataInputProps { |
| 4 | + format: "csv" | "jsonl"; |
| 5 | + requiredColumns: string[]; |
| 6 | + placeholder: string; |
| 7 | + onData: (data: string) => void; |
| 8 | +} |
| 9 | + |
| 10 | +function DataInput({ format, requiredColumns, placeholder, onData }: DataInputProps) { |
| 11 | + const [text, setText] = useState(""); |
| 12 | + const [validationError, setValidationError] = useState<string | null>(null); |
| 13 | + const fileInputRef = useRef<HTMLInputElement>(null); |
| 14 | + |
| 15 | + const validate = (data: string): string | null => { |
| 16 | + const trimmed = data.trim(); |
| 17 | + if (!trimmed) { |
| 18 | + return "No data provided."; |
| 19 | + } |
| 20 | + |
| 21 | + if (format === "csv") { |
| 22 | + const firstLine = trimmed.split("\n")[0]; |
| 23 | + const headers = firstLine.split(",").map((h) => h.trim()); |
| 24 | + const missing = requiredColumns.filter((col) => !headers.includes(col)); |
| 25 | + if (missing.length > 0) { |
| 26 | + return `CSV is missing required column(s): ${missing.join(", ")}. Found headers: ${headers.join(", ")}`; |
| 27 | + } |
| 28 | + } else { |
| 29 | + // jsonl — validate the first line |
| 30 | + const firstLine = trimmed.split("\n")[0]; |
| 31 | + try { |
| 32 | + const obj = JSON.parse(firstLine); |
| 33 | + const keys = Object.keys(obj); |
| 34 | + const missing = requiredColumns.filter((col) => !keys.includes(col)); |
| 35 | + if (missing.length > 0) { |
| 36 | + return `JSONL first line is missing required field(s): ${missing.join(", ")}. Found fields: ${keys.join(", ")}`; |
| 37 | + } |
| 38 | + } catch { |
| 39 | + return "First line is not valid JSON. Expected JSONL format (one JSON object per line)."; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return null; |
| 44 | + }; |
| 45 | + |
| 46 | + const handleLoad = () => { |
| 47 | + const err = validate(text); |
| 48 | + if (err) { |
| 49 | + setValidationError(err); |
| 50 | + return; |
| 51 | + } |
| 52 | + setValidationError(null); |
| 53 | + onData(text.trim()); |
| 54 | + }; |
| 55 | + |
| 56 | + const handleFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 57 | + const file = e.target.files?.[0]; |
| 58 | + if (!file) return; |
| 59 | + |
| 60 | + const reader = new FileReader(); |
| 61 | + reader.onload = (event) => { |
| 62 | + const content = event.target?.result; |
| 63 | + if (typeof content === "string") { |
| 64 | + setText(content); |
| 65 | + setValidationError(null); |
| 66 | + } |
| 67 | + }; |
| 68 | + reader.readAsText(file); |
| 69 | + |
| 70 | + // Reset file input so the same file can be re-selected |
| 71 | + if (fileInputRef.current) { |
| 72 | + fileInputRef.current.value = ""; |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="data-input"> |
| 78 | + <div style={{ display: "flex", gap: "0.5rem", marginBottom: "0.5rem" }}> |
| 79 | + <button |
| 80 | + type="button" |
| 81 | + onClick={() => fileInputRef.current?.click()} |
| 82 | + className="secondary" |
| 83 | + > |
| 84 | + Upload {format.toUpperCase()} file |
| 85 | + </button> |
| 86 | + <input |
| 87 | + ref={fileInputRef} |
| 88 | + type="file" |
| 89 | + accept={format === "csv" ? ".csv" : ".jsonl,.json"} |
| 90 | + onChange={handleFileUpload} |
| 91 | + style={{ display: "none" }} |
| 92 | + /> |
| 93 | + <span className="hint"> |
| 94 | + Required {format === "csv" ? "columns" : "fields"}:{" "} |
| 95 | + <code>{requiredColumns.join(", ")}</code> |
| 96 | + </span> |
| 97 | + </div> |
| 98 | + |
| 99 | + <textarea |
| 100 | + value={text} |
| 101 | + onChange={(e) => { |
| 102 | + setText(e.target.value); |
| 103 | + setValidationError(null); |
| 104 | + }} |
| 105 | + placeholder={placeholder} |
| 106 | + rows={8} |
| 107 | + spellCheck={false} |
| 108 | + /> |
| 109 | + |
| 110 | + {validationError && <div className="error">{validationError}</div>} |
| 111 | + |
| 112 | + <button onClick={handleLoad} disabled={!text.trim()}> |
| 113 | + Load Data |
| 114 | + </button> |
| 115 | + </div> |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +export default DataInput; |
0 commit comments