1- import { useState , useEffect , useCallback } from "react" ;
2- import { toast , Toaster } from "sonner" ;
3- import Dropzone from "react-dropzone" ;
4- import * as AlertDialog from "@radix-ui/react-alert-dialog" ;
1+ import { useState , useEffect , useCallback } from "react"
2+ import { toast , Toaster } from "sonner"
3+ import Dropzone from "react-dropzone"
4+ import * as AlertDialog from "@radix-ui/react-alert-dialog"
55// import "./App.css";
66
77interface Image {
8- src : string ;
9- alt : string ;
8+ src : string
9+ alt : string
1010}
1111
1212interface SearchResult {
13- src : string ;
14- score : number ;
13+ src : string
14+ score : number
1515}
1616
1717function App ( ) {
18- const [ images , setImages ] = useState < Image [ ] > ( [ ] ) ;
19- const [ searchResults , setSearchResults ] = useState < SearchResult [ ] > ( [ ] ) ;
20- const [ selectedImage , setSelectedImage ] = useState < string | null > ( null ) ;
18+ const [ images , setImages ] = useState < Image [ ] > ( [ ] )
19+ const [ searchResults , setSearchResults ] = useState < SearchResult [ ] > ( [ ] )
20+ const [ selectedImage , setSelectedImage ] = useState < string | null > ( null )
2121
22- const [ page , setPage ] = useState ( 1 ) ;
23- const [ indexing , setIndexing ] = useState ( false ) ;
24- const [ indexSuccess , setIndexSuccess ] = useState ( false ) ;
25- const pageSize = 3 ;
22+ const [ page , setPage ] = useState ( 1 )
23+ const [ indexing , setIndexing ] = useState ( false )
24+ const [ indexSuccess , setIndexSuccess ] = useState ( false )
25+ const pageSize = 3
2626
2727 const fetchImages = useCallback ( async ( ) => {
28- const response = await fetch (
29- `/getImages?page=${ page } &pageSize=${ pageSize } `
30- ) ;
31- const data : Image [ ] = await response . json ( ) ;
32- console . log ( data ) ;
33- setImages ( data ) ;
34- } , [ page , pageSize ] ) ;
28+ const response = await fetch ( `/getImages?page=${ page } &pageSize=${ pageSize } ` )
29+ const data : Image [ ] = await response . json ( )
30+ console . log ( data )
31+ setImages ( data )
32+ } , [ page , pageSize ] )
3533
3634 useEffect ( ( ) => {
37- void fetchImages ( ) ;
38- } , [ page , pageSize , fetchImages ] ) ;
35+ void fetchImages ( )
36+ } , [ page , pageSize , fetchImages ] )
3937
4038 const handleImageDrop = async ( acceptedFiles : File [ ] ) => {
41- const formData = new FormData ( ) ;
39+ const formData = new FormData ( )
4240 acceptedFiles . forEach ( ( file ) => {
43- formData . append ( "images" , file ) ;
44- } ) ;
41+ formData . append ( "images" , file )
42+ } )
4543
4644 const response = await fetch ( `/uploadImages?pageSize=${ pageSize } ` , {
4745 method : "POST" ,
4846 body : formData ,
49- } ) ;
47+ } )
5048
5149 if ( response . status === 200 ) {
52- const { pageOfFirstImage } = await response . json ( ) ;
53- await fetchImages ( ) ;
54- setPage ( pageOfFirstImage ) ;
55- const imageOrImages = acceptedFiles . length > 1 ? "Images" : "Image" ;
56- toast . success ( `${ imageOrImages } uploaded successfully` ) ;
50+ const { pageOfFirstImage} = await response . json ( )
51+ await fetchImages ( )
52+ setPage ( pageOfFirstImage )
53+ const imageOrImages = acceptedFiles . length > 1 ? "Images" : "Image"
54+ toast . success ( `${ imageOrImages } uploaded successfully` )
5755 }
58- } ;
56+ }
5957
6058 const handleIndexClick = async ( ) => {
61- setIndexing ( true ) ;
62- const response = await fetch ( "/indexImages" ) ;
63- setIndexing ( false ) ;
59+ setIndexing ( true )
60+ const response = await fetch ( "/indexImages" )
61+ setIndexing ( false )
6462 if ( response . status === 200 ) {
65- setIndexSuccess ( true ) ;
63+ setIndexSuccess ( true )
6664 }
67- } ;
65+ }
6866
6967 const handleDeleteConfirm = async ( ) => {
70- if ( ! selectedImage ) return ;
68+ if ( ! selectedImage ) return
7169
7270 const response = await fetch (
7371 `/deleteImage?imagePath=${ encodeURIComponent ( selectedImage ) } ` ,
74- { method : "DELETE" }
75- ) ;
72+ { method : "DELETE" }
73+ )
7674 if ( response . status === 200 ) {
77- setSelectedImage ( null ) ;
78- await fetchImages ( ) ;
79- toast . success ( "Image deleted successfully" ) ;
75+ setSelectedImage ( null )
76+ await fetchImages ( )
77+ toast . success ( "Image deleted successfully" )
8078 }
81- } ;
79+ }
8280
8381 const handleImageClick = async ( imagePath : string ) => {
84- setSelectedImage ( imagePath ) ;
82+ setSelectedImage ( imagePath )
8583 const response = await fetch (
8684 `/search?imagePath=${ encodeURIComponent ( imagePath ) } `
87- ) ;
88- const matchingImages : SearchResult [ ] = await response . json ( ) ;
89- setSearchResults ( matchingImages ) ;
90- } ;
85+ )
86+ const matchingImages : SearchResult [ ] = await response . json ( )
87+ setSearchResults ( matchingImages )
88+ }
9189
92- console . log ( "searchResults: " , searchResults ) ;
90+ console . log ( "searchResults: " , searchResults )
9391
9492 return (
9593 < div className = "min-h-screen bg-gray-800 text-white w-full" >
@@ -98,7 +96,7 @@ function App() {
9896 < h1 className = "text-4xl" > Image Search</ h1 >
9997 </ div >
10098 < Dropzone onDrop = { handleImageDrop } >
101- { ( { getRootProps, getInputProps } ) => (
99+ { ( { getRootProps, getInputProps} ) => (
102100 < section className = "mx-5 border-dashed rounded-lg border-2 border-white hover:cursor-pointer" >
103101 < div
104102 { ...getRootProps ( ) }
@@ -212,7 +210,8 @@ function App() {
212210 ) ) }
213211 </ div >
214212 </ div >
215- ) ;
213+ )
216214}
215+ console . log ( )
217216
218- export default App ;
217+ export default App
0 commit comments