Skip to content

Commit 2cdd7aa

Browse files
authored
Merge pull request #90 from abhitrueprogrammer/staging
chore: year search fix and upload pointer changed to hand file toast when file select and bugfixes
2 parents 35e5f9b + 609f490 commit 2cdd7aa

File tree

7 files changed

+144
-110
lines changed

7 files changed

+144
-110
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"clsx": "^2.1.1",
3838
"cmdk": "1.0.0",
3939
"debounce": "^2.1.1",
40+
"dropzone": "6.0.0-beta.2",
4041
"file-saver": "^2.0.5",
4142
"fuse.js": "^7.0.0",
4243
"geist": "^1.3.0",

pnpm-lock.yaml

Lines changed: 35 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/api/ai-upload/route.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export async function POST(req: Request) {
5151
}
5252
}
5353
const tags = await processAndAnalyze({ imageURL });
54+
5455
const finalTags = await setTagsFromCurrentLists(tags);
5556
console.log("Final tags:", finalTags);
5657
const subject = finalTags["course-name"];
@@ -285,6 +286,11 @@ async function setTagsFromCurrentLists(
285286
}
286287
function findMatch<T>(arr: T[], value: string | undefined): T | undefined {
287288
if (!value) return undefined; // Handle undefined case
288-
const pattern = new RegExp(`[${value}]`, "i");
289-
return arr.find((item) => pattern.test(String(item)));
289+
const pattern = new RegExp(
290+
value
291+
.split("")
292+
.map((char) => `(?=.*${char})`)
293+
.join(""),
294+
"i"
295+
); return arr.find((item) => pattern.test(String(item)));
290296
}

src/app/upload/page.tsx

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import Dropzone from "react-dropzone";
1212
import { createCanvas } from "canvas";
1313
import { getDocument, GlobalWorkerOptions } from "pdfjs-dist";
1414
import { PDFDocument } from "pdf-lib";
15-
async function pdfToImage(file: File) {
16-
GlobalWorkerOptions.workerSrc = 'https://unpkg.com/[email protected]/build/pdf.worker.min.js'
15+
async function pdfToImage(file: File) {
16+
GlobalWorkerOptions.workerSrc =
17+
"https://unpkg.com/[email protected]/build/pdf.worker.min.js";
1718

18-
const pdfDoc = await PDFDocument.load(await file.arrayBuffer());
19+
const pdfDoc = await PDFDocument.load(await file.arrayBuffer());
1920

2021
// Get the first page
2122
const page = pdfDoc.getPages()[0];
@@ -45,8 +46,7 @@ const Page = () => {
4546
const [files, setFiles] = useState<File[]>([]);
4647
const [isUploading, setIsUploading] = useState(false);
4748
const [, setResetSearch] = useState(false);
48-
49-
const handlePrint = async () => {
49+
function pdfCheckAndSelect<T extends File>(acceptedFiles: T[]) {
5050
const maxFileSize = 5 * 1024 * 1024;
5151
const allowedFileTypes = [
5252
"application/pdf",
@@ -55,70 +55,67 @@ const Page = () => {
5555
"image/gif",
5656
];
5757

58-
// if (!slot) {
59-
// toast.error("Slot is required");
60-
// return;
61-
// }
62-
// if (!subject) {
63-
// toast.error("Subject is required");
64-
// return;
65-
// }
66-
// if (!exam) {
67-
// toast.error("Exam is required");
68-
// return;
69-
// }
70-
// if (!year) {
71-
// toast.error("Year is required");
72-
// return;
73-
// }
74-
if (!campus) {
75-
setCampus("Vellore")
76-
77-
}
78-
// if (!semester) {
79-
// toast.error("Semester is required");
80-
// return;
81-
// }
82-
if (!files || files.length === 0) {
83-
toast.error("No files selected");
58+
const toastId = toast.loading("uploading your files");
59+
if (!acceptedFiles || acceptedFiles.length === 0) {
60+
toast.error("No files selected", {
61+
id: toastId,
62+
});
8463
return;
8564
}
8665

87-
if (files.length > 5) {
88-
toast.error("More than 5 files selected");
66+
if (acceptedFiles.length > 5) {
67+
toast.error("More than 5 files selected", {
68+
id: toastId,
69+
});
8970
return;
9071
}
9172

9273
// File validations
93-
const invalidFiles = files.filter(
74+
const invalidFiles = acceptedFiles.filter(
9475
(file) =>
9576
file.size > maxFileSize || !allowedFileTypes.includes(file.type),
9677
);
9778

9879
if (invalidFiles.length > 0) {
9980
toast.error(
10081
`Some files are invalid. Ensure each file is below 5MB and of an allowed type (PDF, JPEG, PNG, GIF).`,
82+
{
83+
id: toastId,
84+
},
10185
);
10286
return;
10387
}
10488

105-
const isPdf = files.length === 1 && files[0]?.type === "application/pdf";
106-
if (isPdf && files.length > 1) {
107-
toast.error("PDFs must be uploaded separately");
89+
const isPdf =
90+
acceptedFiles.length === 1 &&
91+
acceptedFiles[0]?.type === "application/pdf";
92+
if (isPdf && acceptedFiles.length > 1) {
93+
toast.error("PDFs must be uploaded separately", {
94+
id: toastId,
95+
});
10896
return;
10997
}
110-
console.log("Outside")
98+
99+
setFiles(acceptedFiles);
100+
toast.success(`${acceptedFiles.length} files selected!`, {
101+
id: toastId,
102+
});
103+
}
104+
const handlePrint = async () => {
105+
if (!campus) {
106+
setCampus("Vellore");
107+
}
108+
109+
const isPdf = files.length === 1 && files[0]?.type === "application/pdf";
111110

112111
// Prepare FormData
113112
const formData = new FormData();
114113
files.forEach((file) => {
115114
formData.append("files", file);
116115
});
117-
// formData.append("subject", subject);
118-
// formData.append("slot", slot);
119-
if(isPdf && files[0])
120-
{
121-
formData.append("image", await pdfToImage(files[0]))
116+
117+
if (isPdf && files[0]) {
118+
formData.append("image", await pdfToImage(files[0]));
122119
}
123120
// formData.append("exam", exam);
124121
formData.append("campus", campus);
@@ -161,15 +158,11 @@ const Page = () => {
161158
{/* <legend className="text-lg font-bold">Upload papers</legend> */}
162159

163160
<div className="flex w-full flex-col 2xl:gap-y-4">
164-
165161
{/* File Dropzone */}
166162
<div>
167-
<Dropzone
168-
onDrop={(acceptedFiles) => setFiles(acceptedFiles)}
169-
accept={{ "image/*": [], "application/pdf": [] }}
170-
>
163+
<Dropzone onDrop={pdfCheckAndSelect}>
171164
{({ getRootProps, getInputProps }) => (
172-
<section className="my-2 -mr-2 rounded-2xl border-2 border-dashed p-8 text-center">
165+
<section className="my-2 -mr-2 cursor-pointer rounded-2xl border-2 border-dashed p-8 text-center">
173166
<div {...getRootProps()}>
174167
<input {...getInputProps()} />
175168
<p>

0 commit comments

Comments
 (0)