File size: 1,822 Bytes
872630d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import React, { useState, useEffect } from 'react';
// @ts-ignore
import * as PDFJS from 'pdfjs-dist/build/pdf';
import FileEmbedder from './FileEmbedder';

PDFJS.GlobalWorkerOptions.workerSrc = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js";

interface FileLoaderProps {
  setFileText: (text: string) => void;
}

export const FileLoader: React.FC<FileLoaderProps> = ({ setFileText }) => {
  const [selectedFile, setSelectedFile] = useState<File | null>(null);

  // Handle file processing
  useEffect(() => {
    const processPDF = async (file: File) => {
      try {
        const fileData = new Uint8Array(await file.arrayBuffer());
        const pdf = await PDFJS.getDocument({ data: fileData }).promise;
        const maxPages = pdf.numPages;
        const pageTexts = [];

        for (let pageNo = 1; pageNo <= maxPages; pageNo++) {
          const page = await pdf.getPage(pageNo);
          const tokenizedText = await page.getTextContent();
          // @ts-ignore
          const pageText = tokenizedText.items.map(token => token.str).join(' ');
          pageTexts.push(pageText);
        }

        const documentText = pageTexts.join(' ');
        setFileText(documentText);
      } catch (error) {
        console.error('PDF processing error:', error);
      }
    };

    if (selectedFile && selectedFile.type === "application/pdf") {
      processPDF(selectedFile);
    }
  }, [selectedFile, setFileText]);

  // Handle file selection
  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    if (event.target.files) {
      setSelectedFile(event.target.files[0]);
    }
  };

  return (
    <div>
      <FileEmbedder onFileSelect={handleFileChange} />
      {/* Optionally display some status or progress indicator */}
    </div>
  );
};