From 2c461df5fc25a98c3a00927ca7820e5de284b246 Mon Sep 17 00:00:00 2001 From: awalol Date: Tue, 17 Jun 2025 18:40:58 +0800 Subject: [PATCH 1/6] =?UTF-8?q?fix(downloadAll):=20=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E6=9C=AA=E8=A7=A3=E5=AF=86=E6=96=87=E4=BB=B6=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E4=B8=8B=E8=BD=BD=E5=A4=B1=E8=B4=A5=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=9B=AE=E5=BD=95=E9=80=89=E6=8B=A9=E6=A1=86=E7=9A=84?= =?UTF-8?q?=E5=BC=B9=E5=87=BA=20=E4=BC=98=E5=8C=96=E6=9D=83=E9=99=90?= =?UTF-8?q?=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/DownloadAll.tsx | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/components/DownloadAll.tsx b/src/components/DownloadAll.tsx index 5975033..5641f0b 100644 --- a/src/components/DownloadAll.tsx +++ b/src/components/DownloadAll.tsx @@ -1,23 +1,43 @@ -import { DecryptedAudioFile, selectFiles } from '~/features/file-listing/fileListingSlice'; +import { DecryptedAudioFile, ProcessState, selectFiles } from '~/features/file-listing/fileListingSlice'; import { FaDownload } from 'react-icons/fa'; import { useAppSelector } from '~/hooks'; import { toast } from 'react-toastify'; export function DownloadAll() { const files = useAppSelector(selectFiles); - const filesLength = Object.keys(files).length; const onClickDownloadAll = async () => { + if (Object.keys(files).length === 0) { + toast.warning('未添加文件'); + return; + } + + //判断所有文件是否处理完成 + const allComplete = Object.values(files).every((file) => file.state !== ProcessState.PROCESSING); + if (!allComplete) { + toast.warning('请等待所有文件解密完成'); + return; + } + + //过滤处理失败的文件 + const completeFiles = Object.values(files).filter((file) => file.state === ProcessState.COMPLETE); + const filesLength = Object.keys(completeFiles).length; + + //开始下载 let dir: FileSystemDirectoryHandle | undefined; let success = 0; try { - dir = await window.showDirectoryPicker(); + dir = await window.showDirectoryPicker({ mode: 'readwrite' }); } catch (e) { console.error(e); if (e instanceof Error && e.name === 'AbortError') { return; } } - for (const [_, file] of Object.entries(files)) { + toast.warning('开始下载,请稍候'); + for (const [_, file] of Object.entries(completeFiles)) { + if (file.state !== ProcessState.COMPLETE) { + return; + } try { if (dir) { await DownloadNew(dir, file); @@ -33,7 +53,7 @@ export function DownloadAll() { if (success === filesLength) { toast.success(`成功下载: ${success}/${filesLength}首`); } else { - toast.error(`成功下载: ${success}/${filesLength}首`); + toast.warning(`成功下载: ${success}/${filesLength}首`); } }; From 17200150dd7d8bcac2ebf3ba251a4ea6d5a4efa2 Mon Sep 17 00:00:00 2001 From: awalol Date: Sat, 12 Jul 2025 18:44:57 +0800 Subject: [PATCH 2/6] =?UTF-8?q?feat(DownloadAll):=20=E5=B9=B6=E8=A1=8C?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/DownloadAll.tsx | 48 ++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/components/DownloadAll.tsx b/src/components/DownloadAll.tsx index 5641f0b..298516b 100644 --- a/src/components/DownloadAll.tsx +++ b/src/components/DownloadAll.tsx @@ -6,6 +6,7 @@ import { toast } from 'react-toastify'; export function DownloadAll() { const files = useAppSelector(selectFiles); const onClickDownloadAll = async () => { + console.time('DownloadAll'); //开始计时 if (Object.keys(files).length === 0) { toast.warning('未添加文件'); return; @@ -20,11 +21,10 @@ export function DownloadAll() { //过滤处理失败的文件 const completeFiles = Object.values(files).filter((file) => file.state === ProcessState.COMPLETE); - const filesLength = Object.keys(completeFiles).length; + const fileCount = Object.keys(files).length; //开始下载 let dir: FileSystemDirectoryHandle | undefined; - let success = 0; try { dir = await window.showDirectoryPicker({ mode: 'readwrite' }); } catch (e) { @@ -34,27 +34,35 @@ export function DownloadAll() { } } toast.warning('开始下载,请稍候'); + + let success = 0; + const promises: Promise[] = []; for (const [_, file] of Object.entries(completeFiles)) { - if (file.state !== ProcessState.COMPLETE) { - return; - } - try { - if (dir) { - await DownloadNew(dir, file); - } else { - await DownloadOld(file); - } - success++; - } catch (e) { - console.error(`下载失败: ${file.fileName}`, e); - toast.error(`出现错误: ${e}`); - } + const promise = new Promise((resolve, reject) => { + console.log(`开始下载: ${file.fileName}`); + const action = dir ? DownloadNew(dir, file) : DownloadOld(file); + action.then( + () => { + console.log(`成功下载: ${file.fileName}`); + success++; + resolve(); + }, + (e) => { + console.error(`下载失败: ${file.fileName}`, e); + toast.error(`出现错误: ${e}`); + reject(e); + }, + ); + }); + promises.push(promise); } - if (success === filesLength) { - toast.success(`成功下载: ${success}/${filesLength}首`); - } else { - toast.warning(`成功下载: ${success}/${filesLength}首`); + try { + await Promise.allSettled(promises); + toast.success(`成功下载: ${success}/${fileCount}首`); + } catch { + toast.warning(`成功下载: ${success}/${fileCount}首`); } + console.timeEnd('DownloadAll'); //停止计时 }; return ( From 2da766168cea7d44a58ec2e01292d6048ef254d0 Mon Sep 17 00:00:00 2001 From: awalol Date: Mon, 14 Jul 2025 00:56:30 +0800 Subject: [PATCH 3/6] =?UTF-8?q?refactor(DownloadAll):=20=E5=B9=B6=E8=A1=8C?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/DownloadAll.tsx | 54 ++++++++++++++++------------------ 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/src/components/DownloadAll.tsx b/src/components/DownloadAll.tsx index 298516b..bc9abae 100644 --- a/src/components/DownloadAll.tsx +++ b/src/components/DownloadAll.tsx @@ -7,7 +7,8 @@ export function DownloadAll() { const files = useAppSelector(selectFiles); const onClickDownloadAll = async () => { console.time('DownloadAll'); //开始计时 - if (Object.keys(files).length === 0) { + const fileCount = Object.keys(files).length; + if (fileCount === 0) { toast.warning('未添加文件'); return; } @@ -21,7 +22,6 @@ export function DownloadAll() { //过滤处理失败的文件 const completeFiles = Object.values(files).filter((file) => file.state === ProcessState.COMPLETE); - const fileCount = Object.keys(files).length; //开始下载 let dir: FileSystemDirectoryHandle | undefined; @@ -35,33 +35,29 @@ export function DownloadAll() { } toast.warning('开始下载,请稍候'); - let success = 0; - const promises: Promise[] = []; - for (const [_, file] of Object.entries(completeFiles)) { - const promise = new Promise((resolve, reject) => { - console.log(`开始下载: ${file.fileName}`); - const action = dir ? DownloadNew(dir, file) : DownloadOld(file); - action.then( - () => { - console.log(`成功下载: ${file.fileName}`); - success++; - resolve(); - }, - (e) => { - console.error(`下载失败: ${file.fileName}`, e); - toast.error(`出现错误: ${e}`); - reject(e); - }, - ); - }); - promises.push(promise); - } - try { - await Promise.allSettled(promises); - toast.success(`成功下载: ${success}/${fileCount}首`); - } catch { - toast.warning(`成功下载: ${success}/${fileCount}首`); - } + const promises = Object.values(completeFiles).map(async (file) => { + console.log(`开始下载: ${file.fileName}`); + try { + if (dir) { + await DownloadNew(dir, file); + } else { + await DownloadOld(file); + } + console.log(`成功下载: ${file.fileName}`); + } catch (e) { + console.error(`下载失败: ${file.fileName}`, e); + toast.error(`出现错误: ${e}`); + throw e; + } + }); + await Promise.allSettled(promises).then((f) => { + const success = f.filter((result) => result.status === 'fulfilled').length; + if (success === fileCount) { + toast.success(`成功下载: ${success}/${fileCount}首`); + } else { + toast.warning(`成功下载: ${success}/${fileCount}首`); + } + }); console.timeEnd('DownloadAll'); //停止计时 }; From d122eaecf5a11e0f8f58d8aaf1890b1109d37da6 Mon Sep 17 00:00:00 2001 From: awalol Date: Tue, 15 Jul 2025 22:10:45 +0800 Subject: [PATCH 4/6] =?UTF-8?q?refactor(DownloadAll):=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.css | 9 +++++++++ src/components/AppRoot.tsx | 2 -- src/components/DownloadAll.tsx | 7 +------ src/tabs/MainTab.tsx | 2 ++ 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/App.css b/src/App.css index 94a85cc..5c548d8 100644 --- a/src/App.css +++ b/src/App.css @@ -65,3 +65,12 @@ h6 { opacity: 0.75; } } + +#downloadAll { + position: absolute; + right: 0; + bottom: 72px; + width: 48px; + height: 48px; + margin: 10px; +} diff --git a/src/components/AppRoot.tsx b/src/components/AppRoot.tsx index 696c46d..dbee96a 100644 --- a/src/components/AppRoot.tsx +++ b/src/components/AppRoot.tsx @@ -15,7 +15,6 @@ import { Bounce, ToastContainer } from 'react-toastify'; import { SettingsHome } from '~/features/settings/SettingsHome'; import { FAQ_PAGES } from '~/faq/FAQPages'; import { FaqHome } from '~/faq/FaqHome'; -import { DownloadAll } from '~/components/DownloadAll.tsx'; // Private to this file only. const store = setupStore(); @@ -72,7 +71,6 @@ export function AppRoot() { transition={Bounce} /> -