11"use client" ;
22
3- import { useCallback , useEffect , useState } from "react" ;
3+ import { useCallback , useEffect , useState , useRef } from "react" ;
44import toast from "react-hot-toast" ;
55import { Button } from "@/components/ui/button" ;
66import axios , { AxiosError } from "axios" ;
@@ -13,7 +13,7 @@ import {
1313 TouchSensor ,
1414 useSensor ,
1515 useSensors ,
16- DragEndEvent ,
16+ type DragEndEvent ,
1717} from "@dnd-kit/core" ;
1818import {
1919 arrayMove ,
@@ -24,17 +24,12 @@ import {
2424import { CSS } from "@dnd-kit/utilities" ;
2525import Dropzone from "react-dropzone" ;
2626import { Upload , XIcon } from "lucide-react" ;
27- import { getDocument , GlobalWorkerOptions } from "pdfjs-dist" ;
28- import { type ApiResponse } from "@/interface"
27+ import { GlobalWorkerOptions } from "pdfjs-dist" ;
28+ import type { ApiResponse } from "@/interface"
2929
3030GlobalWorkerOptions . workerSrc =
3131 "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.8.69/pdf.worker.min.mjs" ;
3232
33- interface APIResponse {
34- status : string ;
35- message ?: string ;
36- }
37-
3833interface uploadResponse {
3934 file_url : string ;
4035 thumbnail_url : string ;
@@ -49,6 +44,12 @@ export default function Page() {
4944 const [ isGlobalDragging , setIsGlobalDragging ] = useState ( false ) ;
5045 const [ zoomIndex , setZoomIndex ] = useState < number | null > ( null ) ;
5146
47+ const previewsRef = useRef ( previews ) ;
48+
49+ useEffect ( ( ) => {
50+ previewsRef . current = previews ;
51+ } , [ previews ] ) ;
52+
5253 useEffect ( ( ) => {
5354 const onDragEnter = ( ) => setIsGlobalDragging ( true ) ;
5455 const onDragLeave = ( ) => setIsGlobalDragging ( false ) ;
@@ -68,7 +69,7 @@ export default function Page() {
6869 // Cleanup previews on unmount
6970 useEffect ( ( ) => {
7071 return ( ) => {
71- previews . forEach ( ( item ) => {
72+ previewsRef . current . forEach ( ( item ) => {
7273 try {
7374 URL . revokeObjectURL ( item . preview ) ;
7475 } catch { }
@@ -281,10 +282,10 @@ export default function Page() {
281282 await axios . post < ApiResponse < uploadResponse > > ( "/api/upload" , formData ) ;
282283 return { message : "Papers uploaded successfully!" } ;
283284 } catch ( error ) {
284- if ( error instanceof AxiosError && error . response ?. data ) {
285- const errorData = error . response . data ;
285+ if ( error instanceof AxiosError ) {
286+ const errorData = error ? .response ? .data as ApiResponse < unknown > ;
286287 const errorMessage =
287- errorData . message ?? "Failed to upload papers" ;
288+ errorData ? .message ?? "Failed to upload papers" ;
288289 throw new Error ( errorMessage ) ;
289290 }
290291 throw new Error ( "Failed to upload papers" ) ;
0 commit comments