@@ -30,6 +30,7 @@ import {
30
30
IconFolderOff ,
31
31
IconCheck ,
32
32
IconX ,
33
+ IconTrash ,
33
34
} from "@tabler/icons-react" ;
34
35
35
36
interface Thread {
@@ -384,6 +385,66 @@ export default function InboxPage() {
384
385
} ) ;
385
386
} ;
386
387
388
+ const deleteThread = ( ) => {
389
+ notifications . show ( {
390
+ id : "loading" ,
391
+ title : "Loading" ,
392
+ color : "red" ,
393
+ message : "Deleting thread..." ,
394
+ loading : true ,
395
+ autoClose : false ,
396
+ } ) ;
397
+ const formData = new FormData ( ) ;
398
+ formData . append ( "id" , active . toString ( ) ) ;
399
+ fetch ( "/api/emails/delete" , {
400
+ method : "POST" ,
401
+ body : formData ,
402
+ } )
403
+ . then ( ( res ) => {
404
+ if ( res . ok ) return res . json ( ) ;
405
+ notifications . update ( {
406
+ id : "loading" ,
407
+ title : "Error!" ,
408
+ color : "red" ,
409
+ loading : false ,
410
+ message : "Something went wrong!" ,
411
+ } ) ;
412
+ } )
413
+ . then ( ( ) => {
414
+ setThreads ( ( oldThreads ) => {
415
+ const updatedThreads = oldThreads . filter (
416
+ ( thread ) => thread . id !== active ,
417
+ ) ;
418
+
419
+ let newActive = - 1 ;
420
+
421
+ //setting to next email hopefully
422
+ if ( updatedThreads . length > 0 ) {
423
+ const currentIndex = oldThreads . findIndex (
424
+ ( thread ) => thread . id === active ,
425
+ ) ;
426
+ if ( currentIndex >= 0 && currentIndex < updatedThreads . length ) {
427
+ newActive = updatedThreads [ currentIndex ] . id ;
428
+ } else if ( currentIndex >= updatedThreads . length ) {
429
+ newActive = updatedThreads [ updatedThreads . length - 1 ] . id ;
430
+ }
431
+ }
432
+ setActive ( newActive ) ;
433
+ return updatedThreads ;
434
+ } ) ;
435
+ notifications . update ( {
436
+ id : "loading" ,
437
+ title : "Success!" ,
438
+ color : "green" ,
439
+ message : "Deleted thread" ,
440
+ icon : < IconCheck /> ,
441
+ autoClose : 2000 ,
442
+ withCloseButton : false ,
443
+ loading : false ,
444
+ } ) ;
445
+ } ) ;
446
+ } ;
447
+
387
448
useEffect ( ( ) => {
388
449
if ( activeThread && activeThread . emailList . length > threadSize ) {
389
450
if ( viewport && viewport . current )
@@ -698,6 +759,16 @@ export default function InboxPage() {
698
759
Unresolve
699
760
</ Button >
700
761
) }
762
+
763
+ { ! activeThread . resolved && (
764
+ < Button
765
+ leftSection = { < IconTrash /> }
766
+ color = "red"
767
+ onClick = { ( ) => deleteThread ( ) }
768
+ >
769
+ Delete
770
+ </ Button >
771
+ ) }
701
772
</ Group >
702
773
</ Stack >
703
774
</ Box >
0 commit comments