(e: React.ChangeEvent<HTMLInputElement>)
| 51 | const [isPdfProcessing, setIsPdfProcessing] = useState(false) |
| 52 | |
| 53 | const handleFileUpload = async (e: React.ChangeEvent<HTMLInputElement>) => { |
| 54 | const file = e.target.files?.[0] |
| 55 | if (!file) return |
| 56 | |
| 57 | setUploadedFile(file) |
| 58 | |
| 59 | // Read file content |
| 60 | if (file.type === "text/plain" || file.name.endsWith(".md") || file.name.endsWith(".tex")) { |
| 61 | const text = await file.text() |
| 62 | setInputText(text) |
| 63 | } else if (file.type === "application/pdf") { |
| 64 | // Use Server Action to extract PDF content, with local fallback |
| 65 | setIsPdfProcessing(true) |
| 66 | setInputText(`[Processing PDF: ${file.name}...]`) |
| 67 | |
| 68 | try { |
| 69 | const formData = new FormData() |
| 70 | formData.append("pdf_file", file) |
| 71 | |
| 72 | // Try Server Action (remote API) first |
| 73 | console.log("[AutoFigure] Trying remote API extraction...") |
| 74 | const result = await convertPdfToMarkdown(formData) |
| 75 | |
| 76 | if (result.success && result.markdown) { |
| 77 | setInputText(result.markdown) |
| 78 | console.log("[AutoFigure] PDF extraction successful (remote API)") |
| 79 | } else { |
| 80 | // Remote API failed, try local extraction as fallback |
| 81 | console.log("[AutoFigure] Remote API failed, trying local extraction...") |
| 82 | setInputText(`[Remote API failed, using local extraction...]`) |
| 83 | |
| 84 | try { |
| 85 | const localText = await extractPdfText(file) |
| 86 | if (localText && localText.trim()) { |
| 87 | setInputText(localText) |
| 88 | console.log("[AutoFigure] PDF extraction successful (local fallback)") |
| 89 | } else { |
| 90 | setInputText(`[PDF extraction failed: No text extracted]`) |
| 91 | console.error("[AutoFigure] Local extraction returned empty text") |
| 92 | } |
| 93 | } catch (localError) { |
| 94 | setInputText(`[PDF extraction failed: ${result.error || "Unknown error"}]`) |
| 95 | console.error("[AutoFigure] Both remote and local extraction failed:", localError) |
| 96 | } |
| 97 | } |
| 98 | } catch (error) { |
| 99 | // Server Action completely failed, try local extraction |
| 100 | console.log("[AutoFigure] Server Action failed, trying local extraction...") |
| 101 | try { |
| 102 | const localText = await extractPdfText(file) |
| 103 | if (localText && localText.trim()) { |
| 104 | setInputText(localText) |
| 105 | console.log("[AutoFigure] PDF extraction successful (local fallback after error)") |
| 106 | } else { |
| 107 | setInputText(`[PDF extraction failed: ${error}]`) |
| 108 | } |
| 109 | } catch (localError) { |
| 110 | setInputText(`[PDF extraction failed: ${error}]`) |
nothing calls this directly
no test coverage detected