feat: integrate platform capabilities for enhanced download management

- Refactored `DownloadAll` component to utilize platform capabilities for download types, improving flexibility in handling various download methods.
- Introduced `buildEnabledDownloadModes` function to determine available download modes based on user settings and platform capabilities.
- Updated `FileRow` and `PanelGeneral` components to conditionally render download options based on platform capabilities.
- Added tests for new functionalities in `DownloadAll`, `FileRow`, and `PanelGeneral` to ensure correct behavior under different platform conditions.
- Enhanced error handling for unsupported download methods, providing clearer feedback to users.
This commit is contained in:
鲁树人 committed 2026-11-01 11:32:04 +09:00
1 parent b5fdc8ebbe
commit d424837e66
17 files changed
+417 -178

No files matched your search

+20 -10
View File
@@ -2,15 +2,19 @@ import { useCallback, useEffect, useId, useState, type ReactNode } from 'react';
import { useAppDispatch, useAppSelector } from '~/hooks';
import { selectStagingBatchDownloadSettings } from '../settingsSelector';
import { BatchDownloadSettings, updateBatchDownloadSetting } from '../settingsSlice';
import { supportsDirectoryPicker, supportsStreamingZipDownload } from '~/util/batchDownloadCapabilities';
import { BatchDownloadType } from '~/platform/capabilities';
import { usePlatform } from '~/platform/PlatformContext';
import { clearRememberedDirectoryHandle, hasRememberedDirectoryHandle } from '~/util/downloadDirectoryHandle';
import { toast } from 'react-toastify';
export function PanelGeneral() {
const dispatch = useAppDispatch();
const batch = useAppSelector(selectStagingBatchDownloadSettings);
const canDirectoryPicker = supportsDirectoryPicker();
const canStreamingZip = supportsStreamingZipDownload();
const { capabilities } = usePlatform();
const downloadTypes = new Set(capabilities.downloadTypes);
const canDirectoryPicker = downloadTypes.has(BatchDownloadType.SelectDirectory);
const canStreamingZip = downloadTypes.has(BatchDownloadType.StreamZip);
const canBlobZip = downloadTypes.has(BatchDownloadType.PackZip);
const [hasRememberedDir, setHasRememberedDir] = useState(false);
const refreshRemembered = useCallback(() => {
@@ -57,7 +61,7 @@ export function PanelGeneral() {
description={
canDirectoryPicker
? '通过 showDirectoryPicker 将解密文件直接写入所选目录(推荐,Chrome / Edge)。'
: '当前浏览器不支持 showDirectoryPicker。'
: '当前环境不支持选择目录批量下载。'
}
onChange={(checked) => setBatch('directoryPicker', checked)}
/>
@@ -93,20 +97,26 @@ export function PanelGeneral() {
description={
canStreamingZip
? '边打包边写入单个 ZIP 文件(showSaveFilePicker),内存占用较低。'
: '当前浏览器不支持 showSaveFilePicker,无法流式写出 ZIP。'
: '当前环境不支持流式写出 ZIP。'
}
onChange={(checked) => setBatch('streamingZip', checked)}
/>
<BatchOption
id={idBlobZip}
checked={batch.blobZip}
checked={batch.blobZip && canBlobZip}
disabled={!canBlobZip}
dimmed={!canBlobZip}
title="生成单个 ZIP 并触发下载"
description={
<>
在内存中生成 ZIP 后触发一次浏览器下载。兼容性最好;ZIP 需整包驻留内存,建议合计不超过约
500MB~1GB(视设备可用内存而定,过大可能失败或卡顿)。
</>
canBlobZip ? (
<>
在内存中生成 ZIP 后触发一次浏览器下载。兼容性最好;ZIP 需整包驻留内存,建议合计不超过约
500MB~1GB(视设备可用内存而定,过大可能失败或卡顿)。
</>
) : (
'当前环境不支持打包 ZIP 下载。'
)
}
onChange={(checked) => setBatch('blobZip', checked)}
/>
@@ -0,0 +1,35 @@
import { PanelGeneral } from '~/features/settings/panels/PanelGeneral';
import { BatchDownloadType } from '~/platform/capabilities';
import { createBrowserProvider } from '~/platform/browser/BrowserProvider';
import { renderWithProviders, screen } from '~/test-utils/test-helper';
const stubCrypto = {
request: () => Promise.resolve(null as never),
enqueueDecrypt: () => Promise.reject(new Error('unused')),
};
test('disables directory and streaming options when only PackZip is capable', () => {
const platform = createBrowserProvider({
crypto: stubCrypto,
downloadTypes: [BatchDownloadType.PackZip],
});
renderWithProviders(<PanelGeneral />, { platform });
expect(screen.getByLabelText(/使用选择目录批量下载/)).toBeDisabled();
expect(screen.getByLabelText(/流式生成 ZIP/)).toBeDisabled();
expect(screen.getByLabelText(/生成单个 ZIP 并触发下载/)).not.toBeDisabled();
});
test('disables pack-zip when capability is absent', () => {
const platform = createBrowserProvider({
crypto: stubCrypto,
downloadTypes: [],
});
renderWithProviders(<PanelGeneral />, { platform });
expect(screen.getByLabelText(/使用选择目录批量下载/)).toBeDisabled();
expect(screen.getByLabelText(/流式生成 ZIP/)).toBeDisabled();
expect(screen.getByLabelText(/生成单个 ZIP 并触发下载/)).toBeDisabled();
});