feat: enhance batch download options in PanelGeneral component

- Added conditional rendering for batch download options based on available download types.
- Introduced a new state variable to control the visibility of the batch download section.
- Updated tests to verify the correct display of batch download options based on platform capabilities and download types.
This commit is contained in:
鲁树人 committed 2026-11-06 04:34:07 +09:00
1 parent 6571dc5344
commit c8bb7d4430
2 files changed
+53 -53

No files matched your search

+34 -40
View File
@@ -21,6 +21,7 @@ export function PanelGeneral() {
const canDirectoryPicker = downloadTypes.has(BatchDownloadType.SelectDirectory); const canDirectoryPicker = downloadTypes.has(BatchDownloadType.SelectDirectory);
const canStreamingZip = downloadTypes.has(BatchDownloadType.StreamZip); const canStreamingZip = downloadTypes.has(BatchDownloadType.StreamZip);
const canBlobZip = downloadTypes.has(BatchDownloadType.PackZip); const canBlobZip = downloadTypes.has(BatchDownloadType.PackZip);
const showBatchDownload = downloadTypes.size > 0;
const canDirectWrite = capabilities.directFsAccess; const canDirectWrite = capabilities.directFsAccess;
const [hasRememberedDir, setHasRememberedDir] = useState(false); const [hasRememberedDir, setHasRememberedDir] = useState(false);
const [pickingDirectory, setPickingDirectory] = useState(false); const [pickingDirectory, setPickingDirectory] = useState(false);
@@ -71,37 +72,37 @@ export function PanelGeneral() {
<div className="min-h-0 flex-col grow px-1"> <div className="min-h-0 flex-col grow px-1">
<h2 className="text-2xl font-bold mb-4">通用设置</h2> <h2 className="text-2xl font-bold mb-4">通用设置</h2>
{showBatchDownload && (
<>
<h3 className="text-xl font-semibold mb-2">批量下载</h3> <h3 className="text-xl font-semibold mb-2">批量下载</h3>
<p className="text-sm text-base-content/80 mb-4"> <p className="text-sm text-base-content/80 mb-4">
点击「下载全部」时,将按下方勾选项的顺序依次尝试。若选中的模式均不可用或全部失败,会弹出提示。 点击「下载全部」时,将按下方勾选项的顺序依次尝试。若选中的模式均不可用或全部失败,会弹出提示。
</p> </p>
<div className="flex flex-col gap-3"> <div className="flex flex-col gap-3">
{canDirectoryPicker && (
<>
<BatchOption <BatchOption
id={idDir} id={idDir}
checked={batch.directoryPicker && canDirectoryPicker} checked={batch.directoryPicker}
disabled={!canDirectoryPicker}
dimmed={!canDirectoryPicker}
title="使用选择目录批量下载" title="使用选择目录批量下载"
description={ description="通过 showDirectoryPicker 将解密文件直接写入所选目录(推荐,Chrome / Edge)。"
canDirectoryPicker
? '通过 showDirectoryPicker 将解密文件直接写入所选目录(推荐,Chrome / Edge)。'
: '当前环境不支持选择目录批量下载。'
}
onChange={(checked) => setBatch('directoryPicker', checked)} onChange={(checked) => setBatch('directoryPicker', checked)}
/> />
<div className={`ml-6 flex flex-col gap-2 ${batch.directoryPicker && canDirectoryPicker ? '' : 'opacity-50'}`}> <div className={`ml-6 flex flex-col gap-2 ${batch.directoryPicker ? '' : 'opacity-50'}`}>
<BatchOption <BatchOption
id={idRemember} id={idRemember}
checked={batch.rememberDirectory} checked={batch.rememberDirectory}
disabled={!batch.directoryPicker || !canDirectoryPicker} disabled={!batch.directoryPicker}
title="记住输出目录" title="记住输出目录"
description="将目录句柄保存在本机;下次下载时可能仍需确认写入权限。" description="将目录句柄保存在本机;下次下载时可能仍需确认写入权限。"
onChange={(checked) => setBatch('rememberDirectory', checked)} onChange={(checked) => setBatch('rememberDirectory', checked)}
/> />
<div className="flex items-center gap-2 text-sm"> <div className="flex items-center gap-2 text-sm">
<span className="text-base-content/70">{hasRememberedDir ? '已记住一个输出目录' : '尚未记住输出目录'}</span> <span className="text-base-content/70">
{hasRememberedDir ? '已记住一个输出目录' : '尚未记住输出目录'}
</span>
<button <button
type="button" type="button"
className="btn btn-xs btn-ghost" className="btn btn-xs btn-ghost"
@@ -112,49 +113,43 @@ export function PanelGeneral() {
</button> </button>
</div> </div>
</div> </div>
</>
)}
{canStreamingZip && (
<BatchOption <BatchOption
id={idStreamZip} id={idStreamZip}
checked={batch.streamingZip && canStreamingZip} checked={batch.streamingZip}
disabled={!canStreamingZip}
dimmed={!canStreamingZip}
title="流式生成 ZIP" title="流式生成 ZIP"
description={ description="边打包边写入单个 ZIP 文件(showSaveFilePicker),内存占用较低。"
canStreamingZip
? '边打包边写入单个 ZIP 文件(showSaveFilePicker),内存占用较低。'
: '当前环境不支持流式写出 ZIP。'
}
onChange={(checked) => setBatch('streamingZip', checked)} onChange={(checked) => setBatch('streamingZip', checked)}
/> />
)}
{canBlobZip && (
<BatchOption <BatchOption
id={idBlobZip} id={idBlobZip}
checked={batch.blobZip && canBlobZip} checked={batch.blobZip}
disabled={!canBlobZip}
dimmed={!canBlobZip}
title="生成单个 ZIP 并触发下载" title="生成单个 ZIP 并触发下载"
description={ description={
canBlobZip ? (
<> <>
在内存中生成 ZIP 后触发一次浏览器下载。兼容性最好;ZIP 需整包驻留内存,建议合计不超过约 在内存中生成 ZIP 后触发一次浏览器下载。兼容性最好;ZIP 需整包驻留内存,建议合计不超过约
500MB~1GB(视设备可用内存而定,过大可能失败或卡顿)。 500MB~1GB(视设备可用内存而定,过大可能失败或卡顿)。
</> </>
) : (
'当前环境不支持打包 ZIP 下载。'
)
} }
onChange={(checked) => setBatch('blobZip', checked)} onChange={(checked) => setBatch('blobZip', checked)}
/> />
)}
</div> </div>
</>
)}
<h3 className="text-xl font-semibold mt-8 mb-2">解密后自动保存</h3> {canDirectWrite && (
<p className="text-sm text-base-content/80 mb-4"> <>
{canDirectWrite <h3 className={`text-xl font-semibold mb-2 ${showBatchDownload ? 'mt-8' : ''}`}>解密后自动保存</h3>
? '解密完成后直接保存到本机,无需再手动下载。' <p className="text-sm text-base-content/80 mb-4">解密完成后直接保存到本机,无需再手动下载。</p>
: '当前环境无法直接保存到本机,请使用上方的批量下载。此项设置会保留,在桌面客户端中生效。'}
</p>
<div className={`flex flex-col gap-3 ${canDirectWrite ? '' : 'opacity-50'}`}> <div className="flex flex-col gap-3">
<div className="flex flex-col gap-1"> <div className="flex flex-col gap-1">
<label htmlFor={idOutputMode} className="font-medium"> <label htmlFor={idOutputMode} className="font-medium">
保存位置 保存位置
@@ -163,7 +158,6 @@ export function PanelGeneral() {
id={idOutputMode} id={idOutputMode}
className="select select-bordered select-sm w-full max-w-md" className="select select-bordered select-sm w-full max-w-md"
value={directWrite.outputMode} value={directWrite.outputMode}
disabled={!canDirectWrite}
onChange={(e) => onChange={(e) =>
dispatch(updateDirectWriteSetting({ outputMode: e.target.value as DirectWriteOutputMode })) dispatch(updateDirectWriteSetting({ outputMode: e.target.value as DirectWriteOutputMode }))
} }
@@ -189,7 +183,7 @@ export function PanelGeneral() {
className="input input-bordered input-sm min-w-0 flex-1" className="input input-bordered input-sm min-w-0 flex-1"
placeholder="/path/to/output" placeholder="/path/to/output"
value={directWrite.fixedDirectoryPath ?? ''} value={directWrite.fixedDirectoryPath ?? ''}
disabled={!canDirectWrite || directWrite.outputMode !== 'fixedDirectory'} disabled={directWrite.outputMode !== 'fixedDirectory'}
onChange={(e) => onChange={(e) =>
dispatch( dispatch(
updateDirectWriteSetting({ fixedDirectoryPath: e.target.value === '' ? null : e.target.value }), updateDirectWriteSetting({ fixedDirectoryPath: e.target.value === '' ? null : e.target.value }),
@@ -199,7 +193,7 @@ export function PanelGeneral() {
<button <button
type="button" type="button"
className="btn btn-sm btn-outline" className="btn btn-sm btn-outline"
disabled={!canDirectWrite || pickingDirectory} disabled={pickingDirectory}
onClick={() => void handlePickDirectory()} onClick={() => void handlePickDirectory()}
> >
{pickingDirectory ? '选择中…' : '选择文件夹'} {pickingDirectory ? '选择中…' : '选择文件夹'}
@@ -210,12 +204,13 @@ export function PanelGeneral() {
<BatchOption <BatchOption
id={idDeleteSource} id={idDeleteSource}
checked={directWrite.deleteSourceAfterDecrypt} checked={directWrite.deleteSourceAfterDecrypt}
disabled={!canDirectWrite}
title="保存成功后删除加密文件" title="保存成功后删除加密文件"
description="仅在写出成功后删除;请先确认已有备份。" description="仅在写出成功后删除;请先确认已有备份。"
onChange={(checked) => dispatch(updateDirectWriteSetting({ deleteSourceAfterDecrypt: checked }))} onChange={(checked) => dispatch(updateDirectWriteSetting({ deleteSourceAfterDecrypt: checked }))}
/> />
</div> </div>
</>
)}
</div> </div>
); );
} }
@@ -224,15 +219,14 @@ type BatchOptionProps = {
id: string; id: string;
checked: boolean; checked: boolean;
disabled?: boolean; disabled?: boolean;
dimmed?: boolean;
title: string; title: string;
description: ReactNode; description: ReactNode;
onChange: (checked: boolean) => void; onChange: (checked: boolean) => void;
}; };
function BatchOption({ id, checked, disabled, dimmed, title, description, onChange }: BatchOptionProps) { function BatchOption({ id, checked, disabled, title, description, onChange }: BatchOptionProps) {
return ( return (
<label className={`flex items-start gap-2 ${dimmed ? 'opacity-50' : ''}`}> <label className="flex items-start gap-2">
<input <input
id={id} id={id}
type="checkbox" type="checkbox"
@@ -9,7 +9,7 @@ const stubCrypto = {
enqueueDecrypt: () => Promise.reject(new Error('unused')), enqueueDecrypt: () => Promise.reject(new Error('unused')),
}; };
test('disables directory and streaming options when only PackZip is capable', () => { test('shows only capable batch-download options', () => {
const platform = createBrowserProvider({ const platform = createBrowserProvider({
crypto: stubCrypto, crypto: stubCrypto,
downloadTypes: [BatchDownloadType.PackZip], downloadTypes: [BatchDownloadType.PackZip],
@@ -17,25 +17,29 @@ test('disables directory and streaming options when only PackZip is capable', ()
renderWithProviders(<PanelGeneral />, { platform }); renderWithProviders(<PanelGeneral />, { platform });
expect(screen.getByLabelText(/使用选择目录批量下载/)).toBeDisabled(); expect(screen.queryByLabelText(/使用选择目录批量下载/)).not.toBeInTheDocument();
expect(screen.getByLabelText(/流式生成 ZIP/)).toBeDisabled(); expect(screen.queryByLabelText(/流式生成 ZIP/)).not.toBeInTheDocument();
expect(screen.getByLabelText(/生成单个 ZIP 并触发下载/)).toBeInTheDocument();
expect(screen.getByLabelText(/生成单个 ZIP 并触发下载/)).not.toBeDisabled(); expect(screen.getByLabelText(/生成单个 ZIP 并触发下载/)).not.toBeDisabled();
}); });
test('disables pack-zip when capability is absent', () => { test('hides batch-download section when downloadTypes is empty', () => {
const platform = createBrowserProvider({ const platform = createBrowserProvider({
crypto: stubCrypto, crypto: stubCrypto,
downloadTypes: [], downloadTypes: [],
directFsAccess: true,
}); });
renderWithProviders(<PanelGeneral />, { platform }); renderWithProviders(<PanelGeneral />, { platform });
expect(screen.getByLabelText(/使用选择目录批量下载/)).toBeDisabled(); expect(screen.queryByRole('heading', { name: '批量下载' })).not.toBeInTheDocument();
expect(screen.getByLabelText(/流式生成 ZIP/)).toBeDisabled(); expect(screen.queryByLabelText(/使用选择目录批量下载/)).not.toBeInTheDocument();
expect(screen.getByLabelText(/生成单个 ZIP 并触发下载/)).toBeDisabled(); expect(screen.queryByLabelText(/流式生成 ZIP/)).not.toBeInTheDocument();
expect(screen.queryByLabelText(/生成单个 ZIP 并触发下载/)).not.toBeInTheDocument();
expect(screen.getByRole('heading', { name: '解密后自动保存' })).toBeInTheDocument();
}); });
test('disables DirectWrite controls when directFsAccess is false', () => { test('hides DirectWrite section when directFsAccess is false', () => {
const platform = createBrowserProvider({ const platform = createBrowserProvider({
crypto: stubCrypto, crypto: stubCrypto,
downloadTypes: [BatchDownloadType.PackZip], downloadTypes: [BatchDownloadType.PackZip],
@@ -44,13 +48,15 @@ test('disables DirectWrite controls when directFsAccess is false', () => {
renderWithProviders(<PanelGeneral />, { platform }); renderWithProviders(<PanelGeneral />, { platform });
expect(screen.getByLabelText('保存位置')).toBeDisabled(); expect(screen.queryByRole('heading', { name: '解密后自动保存' })).not.toBeInTheDocument();
expect(screen.getByLabelText('文件夹路径')).toBeDisabled(); expect(screen.queryByLabelText('保存位置')).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: '选择文件夹' })).toBeDisabled(); expect(screen.queryByLabelText('文件夹路径')).not.toBeInTheDocument();
expect(screen.getByLabelText(/保存成功后删除加密文件/)).toBeDisabled(); expect(screen.queryByRole('button', { name: '选择文件夹' })).not.toBeInTheDocument();
expect(screen.queryByLabelText(/保存成功后删除加密文件/)).not.toBeInTheDocument();
expect(screen.getByRole('heading', { name: '批量下载' })).toBeInTheDocument();
}); });
test('enables DirectWrite controls when directFsAccess is true', () => { test('shows DirectWrite controls when directFsAccess is true', () => {
const platform = createBrowserProvider({ const platform = createBrowserProvider({
crypto: stubCrypto, crypto: stubCrypto,
downloadTypes: [BatchDownloadType.PackZip], downloadTypes: [BatchDownloadType.PackZip],