22
33import { useRef , useState , useTransition } from "react" ;
44import { File as FileIcon , Trash2 , Copy , Pencil } from "lucide-react" ;
5+ import { toast } from "sonner" ;
56import { uploadMediaAction , deleteMediaAction , renameMediaAction } from "../../lib/actions" ;
67import type { Media } from "../../generated/prisma/client" ;
78import { runAction } from "./runAction" ;
89import { ResourceCard , NewResourceCard , ActionButton , formatRelativeTime } from "./ResourceCard" ;
10+ import { useDialogs } from "@/components/ui/dialog-provider" ;
911
1012type MediaWithUrl = Media & { url : string } ;
1113
@@ -80,7 +82,7 @@ function MediaCard({
8082 < ActionButton onClick = { ( ) => onRename ( file ) } disabled = { disabled } title = "Rename" >
8183 < Pencil className = "h-3 w-3" />
8284 </ ActionButton >
83- < ActionButton onClick = { ( ) => navigator . clipboard . writeText ( file . url ) } title = "Copy URL" >
85+ < ActionButton onClick = { ( ) => { navigator . clipboard . writeText ( file . url ) ; toast . success ( "URL copied" ) ; } } title = "Copy URL" >
8486 < Copy className = "h-3 w-3" />
8587 </ ActionButton >
8688 < ActionButton onClick = { ( ) => onDelete ( file ) } disabled = { disabled } title = "Delete" variant = "danger" >
@@ -95,6 +97,7 @@ function MediaCard({
9597export function MediaLibrary ( { files : initialFiles } : { files : MediaWithUrl [ ] } ) {
9698 const [ isPending , startTransition ] = useTransition ( ) ;
9799 const [ files , setFiles ] = useState ( initialFiles ) ;
100+ const { confirm, prompt, alert } = useDialogs ( ) ;
98101
99102 function handleUpload ( file : File ) {
100103 const formData = new FormData ( ) ;
@@ -104,32 +107,32 @@ export function MediaLibrary({ files: initialFiles }: { files: MediaWithUrl[] })
104107 if ( result . success ) {
105108 setFiles ( ( prev ) => [ result . data , ...prev ] ) ;
106109 } else {
107- alert ( result . error ) ;
110+ await alert ( result . error ) ;
108111 }
109112 } ) ;
110113 }
111114
112- function handleRename ( file : MediaWithUrl ) {
113- const newName = window . prompt ( "Rename file" , file . name ) ;
115+ async function handleRename ( file : MediaWithUrl ) {
116+ const newName = await prompt ( { title : "Rename file" , defaultValue : file . name } ) ;
114117 if ( newName === null || newName . trim ( ) === "" || newName . trim ( ) === file . name ) return ;
115118 startTransition ( async ( ) => {
116119 const result = await runAction ( renameMediaAction ( { id : file . id , name : newName . trim ( ) } ) ) ;
117120 if ( result . success ) {
118121 setFiles ( ( prev ) => prev . map ( ( f ) => ( f . id === file . id ? { ...f , name : newName . trim ( ) } : f ) ) ) ;
119122 } else {
120- alert ( result . error ) ;
123+ await alert ( result . error ) ;
121124 }
122125 } ) ;
123126 }
124127
125- function handleDelete ( file : MediaWithUrl ) {
126- if ( ! window . confirm ( `Delete "${ file . name } "?` ) ) return ;
128+ async function handleDelete ( file : MediaWithUrl ) {
129+ if ( ! await confirm ( { message : `Delete "${ file . name } "?` , actionLabel : "Delete" } ) ) return ;
127130 startTransition ( async ( ) => {
128131 const result = await runAction ( deleteMediaAction ( { id : file . id } ) ) ;
129132 if ( result . success ) {
130133 setFiles ( ( prev ) => prev . filter ( ( f ) => f . id !== file . id ) ) ;
131134 } else {
132- alert ( result . error ) ;
135+ await alert ( result . error ) ;
133136 }
134137 } ) ;
135138 }
0 commit comments