@@ -337,31 +337,52 @@ const BatchDepictionView = () => {
337337 } ;
338338
339339 // Download a single depiction image
340- const downloadSingleDepiction = ( depiction ) => {
341- // Check if depictService and the method exist before proceeding
342- if ( ! depictService || typeof depictService . get2DDepictionUrl !== 'function' ) {
343- console . error ( "depictService.get2DDepictionUrl is not available for single download." ) ;
344- setError ( "Depiction service is not configured correctly." ) ;
345- return ;
340+ const downloadSingleDepiction = async ( depiction ) => {
341+ try {
342+ // Check if depictService and the method exist before proceeding
343+ if ( ! depictService || typeof depictService . get2DDepictionUrl !== 'function' ) {
344+ console . error ( "depictService.get2DDepictionUrl is not available for single download." ) ;
345+ setError ( "Depiction service is not configured correctly." ) ;
346+ return ;
347+ }
348+
349+ setLoading ( true ) ; // Show loading indicator while downloading
350+ const rotation = rotations [ depiction . id ] || 0 ;
351+
352+ // Generate URL with current options and selected download format
353+ const options = {
354+ toolkit, width, height, rotate : rotation ,
355+ CIP : toolkit === 'cdk' ? showCIP : undefined ,
356+ unicolor : useUnicolor , highlight : highlight || undefined ,
357+ format : downloadFormat // Use selected format for single download too
358+ } ;
359+
360+ // Get the URL for fetching the image
361+ const url = depictService . get2DDepictionUrl ( depiction . smiles , options ) ;
362+
363+ // Fetch the image data as a blob
364+ const response = await fetch ( url ) ;
365+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
366+
367+ const blob = await response . blob ( ) ;
368+ const downloadUrl = URL . createObjectURL ( blob ) ;
369+
370+ // Trigger download using an anchor tag
371+ const a = document . createElement ( 'a' ) ;
372+ a . href = downloadUrl ;
373+ a . download = `${ depiction . title . replace ( / [ ^ a - z 0 - 9 ] / gi, '_' ) . toLowerCase ( ) || 'molecule' } .${ downloadFormat } ` ;
374+ document . body . appendChild ( a ) ;
375+ a . click ( ) ;
376+ document . body . removeChild ( a ) ;
377+
378+ // Clean up the blob URL
379+ URL . revokeObjectURL ( downloadUrl ) ;
380+ } catch ( err ) {
381+ console . error ( "Error downloading single depiction:" , err ) ;
382+ setError ( `Error downloading depiction: ${ err . message || 'Unknown error' } ` ) ;
383+ } finally {
384+ setLoading ( false ) ; // Hide loading indicator
346385 }
347- const rotation = rotations [ depiction . id ] || 0 ;
348- // Generate URL with current options and selected download format
349- const options = {
350- toolkit, width, height, rotate : rotation ,
351- CIP : toolkit === 'cdk' ? showCIP : undefined ,
352- unicolor : useUnicolor , highlight : highlight || undefined ,
353- format : downloadFormat // Use selected format for single download too
354- } ;
355- // Use a potentially different URL for download if format differs
356- const url = depictService . get2DDepictionUrl ( depiction . smiles , options ) ;
357-
358- // Trigger download using an anchor tag
359- const a = document . createElement ( 'a' ) ;
360- a . href = url ;
361- a . download = `${ depiction . title . replace ( / [ ^ a - z 0 - 9 ] / gi, '_' ) . toLowerCase ( ) || 'molecule' } .${ downloadFormat } ` ;
362- document . body . appendChild ( a ) ;
363- a . click ( ) ;
364- document . body . removeChild ( a ) ;
365386 } ;
366387
367388
@@ -722,6 +743,7 @@ const BatchDepictionView = () => {
722743 className = "p-1.5 rounded-md text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-white hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors focus:outline-none focus:ring-1 focus:ring-blue-500"
723744 title = { `Download ${ downloadFormat . toUpperCase ( ) } ` }
724745 aria-label = { `Download ${ depiction . title } as ${ downloadFormat . toUpperCase ( ) } ` }
746+ disabled = { loading }
725747 >
726748 < HiOutlineDownload className = "h-5 w-5" />
727749 </ button >
0 commit comments