@@ -21,46 +21,54 @@ interface PlanToSvgButtonProps {
2121}
2222
2323export function PlanToSvgButton ( { plan, database} : PlanToSvgButtonProps ) {
24- const [ svgData , setSvgData ] = React . useState < string | null > ( null ) ;
2524 const [ error , setError ] = React . useState < string | null > ( null ) ;
25+ const [ blobUrl , setBlobUrl ] = React . useState < string | null > ( null ) ;
2626 const [ checkPlanToSvg , { isLoading, isUninitialized} ] =
2727 planToSvgQueryApi . usePlanToSvgQueryMutation ( ) ;
2828
2929 React . useEffect ( ( ) => {
3030 if ( ! plan ) {
31- return ;
31+ return undefined ;
3232 }
3333
3434 checkPlanToSvg ( { plan, database} )
3535 . unwrap ( )
3636 . then ( ( result ) => {
37- setSvgData ( result ) ;
37+ const blob = new Blob ( [ result ] , { type : 'image/svg+xml' } ) ;
38+ const url = URL . createObjectURL ( blob ) ;
39+ setBlobUrl ( url ) ;
3840 setError ( null ) ;
3941 } )
4042 . catch ( ( err ) => {
4143 setError ( JSON . stringify ( err ) ) ;
4244 } ) ;
43- } , [ checkPlanToSvg , database , plan ] ) ;
45+
46+ return ( ) => {
47+ if ( blobUrl ) {
48+ URL . revokeObjectURL ( blobUrl ) ;
49+ }
50+ } ;
51+ } , [ checkPlanToSvg , database , plan , blobUrl ] ) ;
4452
4553 const handleClick = React . useCallback ( ( ) => {
46- if ( svgData ) {
47- const blob = new Blob ( [ svgData ] , { type : 'image/svg+xml' } ) ;
48- const url = URL . createObjectURL ( blob ) ;
49- window . open ( url , '_blank' ) ;
54+ if ( blobUrl ) {
55+ window . open ( blobUrl , '_blank' ) ;
5056 }
51- } , [ svgData ] ) ;
57+ } , [ blobUrl ] ) ;
5258
5359 if ( isUninitialized ) {
5460 return null ;
5561 }
5662
5763 return (
58- < Tooltip content = { i18n ( 'text_error-plan-svg' , { error} ) || i18n ( 'text_plan-svg' ) } >
64+ < Tooltip
65+ content = { error ? i18n ( 'text_error-plan-svg' , { error} ) : i18n ( 'text_open-plan-svg' ) }
66+ >
5967 < Button
6068 view = { getButtonView ( error , isLoading ) }
6169 loading = { isLoading }
6270 onClick = { handleClick }
63- disabled = { isLoading || ! svgData }
71+ disabled = { isLoading || ! blobUrl }
6472 >
6573 { i18n ( 'text_plan-svg' ) }
6674 < Button . Icon >
0 commit comments