1
- import { DownloadCloud , FileText , Files } from "lucide-react" ;
1
+ import axios from "axios" ;
2
+ import { DownloadCloud , FileText , Files , Trash2 } from "lucide-react" ;
2
3
import { toast } from "react-hot-toast" ;
4
+ import { getFiles } from "../actions/getFiles" ;
5
+ import { useAtom } from "jotai" ;
6
+ import { filesAtom , sizeAtom } from "../store" ;
7
+ import { getSize } from "../actions/getSize" ;
3
8
4
9
type TContentCardProps = {
5
10
file_name ?: string ;
@@ -21,6 +26,23 @@ const ContentCard: React.FC<TContentCardProps> = ({
21
26
const url = `${ window . location . protocol } //${
22
27
window . location . host
23
28
} /api/cdn/download/${ type === "documents" ? "docs" : "images" } /${ file_name } `;
29
+ const [ _ , setFiles ] = useAtom ( filesAtom )
30
+ const [ __ , setSize ] = useAtom ( sizeAtom )
31
+
32
+ const deleteFile = ( ) => {
33
+ toast . loading ( "Deleting file..." ) ;
34
+ axios . delete ( `/api/cdn/delete/${ type === "documents" ? "doc" : "image" } /${ file_name } ` ) . then ( ( res ) => {
35
+ if ( res . status === 200 ) {
36
+ toast . dismiss ( )
37
+ toast . success ( "Deleted file!" )
38
+ getFiles ( type , setFiles )
39
+ getSize ( setSize ) ;
40
+ }
41
+ } ) . catch ( ( err : Error ) => {
42
+ toast . dismiss ( ) ;
43
+ toast . error ( err . message )
44
+ } )
45
+ }
24
46
25
47
return (
26
48
< div className = "border rounded-lg shadow-lg flex flex-col min-h-[264px] w-64 max-w-[256px] justify-center items-center gap-4 p-4" >
@@ -36,29 +58,44 @@ const ContentCard: React.FC<TContentCardProps> = ({
36
58
< FileText size = "128" />
37
59
) }
38
60
< p className = "truncate w-64 px-4" > { file_name } </ p >
39
- < div className = { `flex gap-2 w-full ${ disabled && "sr-only" } ` } >
61
+ < div className = { `flex w-full justify-between ${ disabled && "sr-only" } ` } >
62
+ { /* Non-destructive buttons */ }
63
+ < div className = "flex gap-2" >
64
+ < button
65
+ className = "flex justify-center items-center text-sky-600 tooltip"
66
+ onClick = { ( ) => {
67
+ navigator . clipboard . writeText ( url ) ;
68
+ toast . success ( "clipboard saved" ) ;
69
+ } }
70
+ aria-label = "Copy Link"
71
+ aria-labelledby = "Copy Link"
72
+ >
73
+ < span className = "tooltiptext" > Copy Link</ span >
74
+ < Files className = "inline" size = "24" />
75
+ </ button >
76
+ < a
77
+ className = "flex justify-center items-center text-sky-600 tooltip"
78
+ aria-label = "Download file"
79
+ aria-labelledby = "Download file"
80
+ href = { url }
81
+ download
82
+ >
83
+ < span className = "tooltiptext" > Download file</ span >
84
+ < DownloadCloud className = "inline" size = "24" />
85
+ </ a >
86
+ </ div >
87
+ { /* Destructive buttons */ }
88
+ < div className = "flex gap-2" >
40
89
< button
41
- className = "flex justify-center items-center text-sky-600 tooltip"
42
- onClick = { ( ) => {
43
- navigator . clipboard . writeText ( url ) ;
44
- toast . success ( "clipboard saved" ) ;
45
- } }
46
- aria-label = "Copy Link"
47
- aria-labelledby = "Copy Link"
48
- >
49
- < span className = "tooltiptext" > Copy Link</ span >
50
- < Files className = "inline" size = "24" />
51
- </ button >
52
- < a
53
- className = "flex justify-center items-center text-sky-600 tooltip"
54
- aria-label = "Download file"
55
- aria-labelledby = "Download file"
56
- href = { url }
57
- download
58
- >
59
- < span className = "tooltiptext" > Download file</ span >
60
- < DownloadCloud className = "inline" size = "24" />
61
- </ a >
90
+ className = "flex justify-center items-center text-red-600 tooltip"
91
+ onClick = { ( ) => file_name && deleteFile ( ) }
92
+ aria-label = "Delete file"
93
+ aria-labelledby = "Delete file"
94
+ >
95
+ < span className = "tooltiptext" > Delete file</ span >
96
+ < Trash2 className = "inline" size = "24" />
97
+ </ button >
98
+ </ div >
62
99
</ div >
63
100
</ div >
64
101
) ;
0 commit comments