1+ import { Download } from "@mui/icons-material" ;
2+ import { Button , CircularProgress , IconButton , Tooltip } from "@mui/material" ;
3+ import React from "react" ;
4+ import useMapAsset from "../../hooks/assets/useMapAsset" ;
5+ import GUID from "../../types/common/GUID" ;
6+ import { useTranslation } from "react-i18next" ;
7+ import useDownloadMapAssetAsPNG from "../../hooks/assets/useDownloadMapAssetAsPNG" ;
8+
9+ export interface SpriteDownloadPNGButtonProps {
10+ assetID : GUID | undefined ;
11+ small ?: boolean ;
12+ }
13+
14+ export default function SpriteDownloadPNGButton ( props : SpriteDownloadPNGButtonProps ) {
15+ const { t} = useTranslation ( ) ;
16+ const asset = useMapAsset ( props . assetID ) ;
17+ const downloadPNG = useDownloadMapAssetAsPNG ( ) ;
18+ const [ isDownloadingPNG , setIsDownloadingPNG ] = React . useState ( false ) ;
19+
20+ const onClick = React . useCallback ( ( ) => {
21+ if ( isDownloadingPNG )
22+ return ;
23+
24+ setIsDownloadingPNG ( true ) ;
25+ downloadPNG ( { id : props . assetID } ) . finally ( ( ) => setIsDownloadingPNG ( false ) ) ;
26+ } , [ isDownloadingPNG , downloadPNG , asset ] ) ;
27+
28+ const isDDS = asset ?. blob . type === "image/dds" ;
29+
30+ if ( ! asset || ! isDDS )
31+ return null ;
32+
33+ if ( props . small )
34+ return (
35+ < Tooltip title = { t ( "sprite.downloadAsPNG" ) } >
36+ < IconButton
37+ color = { "secondary" }
38+ size = { "small" }
39+ disabled = { isDownloadingPNG }
40+ onClick = { onClick }
41+ >
42+ { isDownloadingPNG ? (
43+ < CircularProgress
44+ size = { 16 }
45+ color = { "inherit" }
46+ />
47+ ) : (
48+ < Download
49+ fontSize = { "small" }
50+ />
51+ ) }
52+ </ IconButton >
53+ </ Tooltip >
54+ ) ;
55+
56+ return (
57+ < Button
58+ variant = { "outlined" }
59+ color = { "secondary" }
60+ size = { "small" }
61+ fullWidth
62+ disabled = { isDownloadingPNG }
63+ onClick = { onClick }
64+ >
65+ { isDownloadingPNG && (
66+ < CircularProgress
67+ sx = { { marginRight : 0.5 } }
68+ size = { 16 }
69+ color = { "inherit" }
70+ />
71+ ) }
72+ { ! isDownloadingPNG && (
73+ < Download
74+ sx = { { marginRight : 0.5 } }
75+ fontSize = { "small" }
76+ />
77+ ) }
78+ { t ( "sprite.downloadAsPNG" ) }
79+ </ Button >
80+ ) ;
81+ }
0 commit comments