| 6 | import { i18n } from "~tools" |
| 7 | |
| 8 | export default function DownloadImages({ name }) { |
| 9 | const [isDownloading, setIsDownloading] = useState(false) |
| 10 | const [progress, setProgress] = useState(0) |
| 11 | |
| 12 | const handleDownload = async () => { |
| 13 | if (isDownloading) return |
| 14 | |
| 15 | try { |
| 16 | setIsDownloading(true) |
| 17 | setProgress(0) |
| 18 | |
| 19 | const res = await sendToContentScript({ |
| 20 | name: `${name}-downloadImages`, |
| 21 | body: { |
| 22 | onProgress: (current, total) => { |
| 23 | const percentage = Math.round((current / total) * 100) |
| 24 | setProgress(percentage) |
| 25 | } |
| 26 | } |
| 27 | }) |
| 28 | } catch (error) { |
| 29 | console.error("Failed to download images:", error) |
| 30 | } finally { |
| 31 | setIsDownloading(false) |
| 32 | setProgress(0) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return ( |
| 37 | <div |
| 38 | className={`item download ${isDownloading ? "downloading" : ""}`} |
| 39 | onClick={handleDownload} |
| 40 | style={{ |
| 41 | opacity: isDownloading ? 0.7 : 1, |
| 42 | cursor: isDownloading ? "not-allowed" : "pointer" |
| 43 | }}> |
| 44 | <span> |
| 45 | <StarTwoTone twoToneColor="#eb2f96" style={{ marginRight: "5px" }} /> |
| 46 | {isDownloading |
| 47 | ? `${i18n("downloading")} (${progress}%)` |
| 48 | : i18n("downloadAllImg")} |
| 49 | </span> |
| 50 | <DownloadOutlined |
| 51 | style={{ |
| 52 | color: isDownloading ? "#999" : "#52c41a", |
| 53 | fontSize: "16px" |
| 54 | }} |
| 55 | /> |
| 56 | </div> |
| 57 | ) |
| 58 | } |