@@ -71,31 +71,44 @@ export const DesktopAdPreviewNavigation: React.FC<
7171 ( type === "community" && ! isPublished ) ) ) ||
7272 ( type === "community" && ! isPublished ) ;
7373
74+
7475 const downloadImage = async ( format : "png" | "jpg" ) => {
76+ setIsDownloading ( true ) ;
7577 try {
7678 const element = document . getElementById ( "containerRef" ) ;
79+ // console.log("Element found:", element);
80+
7781 if ( element ) {
78- html2canvas ( element , { useCORS : true , allowTaint : true } )
79- . then ( ( canvas ) => {
80- const link = document . createElement ( "a" ) ;
81- link . download = `${ imageName } .${ format } ` ;
82- link . href = canvas . toDataURL ( `image/${ format } ` ) ;
83- link . click ( ) ;
84- } )
85- . catch ( ( error ) => {
86- console . error ( "Error generating canvas:" , error ) ;
87- toast . error ( "Failed to generate image for download" ) ;
88- } ) ;
82+ const canvas = await html2canvas ( element , {
83+ useCORS : true ,
84+ allowTaint : true ,
85+ logging : true ,
86+ } ) ;
87+
88+ // console.log("Canvas created:", canvas);
89+
90+ const link = document . createElement ( "a" ) ;
91+ const fileName = `${ imageName } .${ format } ` ;
92+ link . download = fileName ;
93+ link . href = canvas . toDataURL ( `image/${ format } ` ) ;
94+ // console.log("Data URL created:", link.href.substring(0, 50) + "...");
95+
96+ document . body . appendChild ( link ) ;
97+ link . click ( ) ;
98+ document . body . removeChild ( link ) ;
99+
100+ } else {
101+ // console.error("Element with ID 'containerRef' not found");
102+ toast . error ( "Could not find the ad content to download" ) ;
89103 }
90104 } catch ( error ) {
91- console . error ( "Error downloading image :" , error ) ;
105+ console . error ( "Error in downloadImage :" , error ) ;
92106 toast . error ( "Failed to download image" ) ;
93107 } finally {
94108 setIsDownloading ( false ) ;
95109 setIsExportDropdownOpen ( false ) ;
96110 }
97111 } ;
98-
99112 React . useEffect ( ( ) => {
100113 const handleClickOutside = ( event : MouseEvent ) => {
101114 if (
@@ -250,7 +263,7 @@ export const DesktopAdPreviewNavigation: React.FC<
250263 </ button >
251264
252265 < div className = "flex gap-4" >
253- { ( showSaveButton || queryType == "community" ) && (
266+ { ( showSaveButton || queryType ! == "community" ) && (
254267 < div className = "relative" ref = { saveDropdownRef } >
255268 < button
256269 onClick = { ( ) => setIsSaveDropdownOpen ( ! isSaveDropdownOpen ) }
@@ -294,7 +307,7 @@ export const DesktopAdPreviewNavigation: React.FC<
294307 </ div >
295308 ) }
296309
297- { type === "image-form" && status === "completed" && (
310+ { status === "completed" && (
298311 < ShareModal
299312 adUrl = { `https://genz.ad/stand-alone/${ imageId } ` }
300313 imageUrl = { generatedImageUrl }
@@ -309,8 +322,7 @@ export const DesktopAdPreviewNavigation: React.FC<
309322 </ ShareModal >
310323 ) }
311324
312- { ( type === "demo" ||
313- ( type === "image-form" && status === "completed" ) ) && (
325+ { queryType !== "community" && (
314326 < div className = "relative" ref = { exportDropdownRef } >
315327 < button
316328 disabled = { isDownloading || isLoading }
@@ -332,7 +344,7 @@ export const DesktopAdPreviewNavigation: React.FC<
332344 >
333345 < div className = "py-1" >
334346 < button
335- // onClick={() => downloadImage("png ")}
347+ // onClick={() => console.log("Download as PNG ")}
336348 onClick = { ( ) => downloadImage ( "png" ) }
337349 disabled = { isDownloading || isLoading }
338350 className = "block w-full text-left px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 cursor-pointer"
0 commit comments