| 21 | * Handles text extraction, character limit validation, and cleanup. |
| 22 | */ |
| 23 | export function useFileProcessor() { |
| 24 | const [files, setFiles] = useState<File[]>([]) |
| 25 | const [pdfData, setPdfData] = useState<Map<File, FileData>>(new Map()) |
| 26 | |
| 27 | const handleFileChange = async (newFiles: File[]) => { |
| 28 | setFiles(newFiles) |
| 29 | |
| 30 | // Extract text immediately for new PDF/text files |
| 31 | for (const file of newFiles) { |
| 32 | const needsExtraction = |
| 33 | (isPdfFile(file) || isTextFile(file)) && !pdfData.has(file) |
| 34 | if (needsExtraction) { |
| 35 | // Mark as extracting |
| 36 | setPdfData((prev) => { |
| 37 | const next = new Map(prev) |
| 38 | next.set(file, { |
| 39 | text: "", |
| 40 | charCount: 0, |
| 41 | isExtracting: true, |
| 42 | }) |
| 43 | return next |
| 44 | }) |
| 45 | |
| 46 | // Extract text asynchronously |
| 47 | try { |
| 48 | let text: string |
| 49 | if (isPdfFile(file)) { |
| 50 | text = await extractPdfText(file) |
| 51 | } else { |
| 52 | text = await extractTextFileContent(file) |
| 53 | } |
| 54 | |
| 55 | // Check character limit |
| 56 | if (text.length > MAX_EXTRACTED_CHARS) { |
| 57 | const limitK = MAX_EXTRACTED_CHARS / 1000 |
| 58 | toast.error( |
| 59 | `${file.name}: Content exceeds ${limitK}k character limit (${(text.length / 1000).toFixed(1)}k chars)`, |
| 60 | ) |
| 61 | setPdfData((prev) => { |
| 62 | const next = new Map(prev) |
| 63 | next.delete(file) |
| 64 | return next |
| 65 | }) |
| 66 | // Remove the file from the list |
| 67 | setFiles((prev) => prev.filter((f) => f !== file)) |
| 68 | continue |
| 69 | } |
| 70 | |
| 71 | setPdfData((prev) => { |
| 72 | const next = new Map(prev) |
| 73 | next.set(file, { |
| 74 | text, |
| 75 | charCount: text.length, |
| 76 | isExtracting: false, |
| 77 | }) |
| 78 | return next |
| 79 | }) |
| 80 | } catch (error) { |