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
+154 -154

No files matched your search

+135 -141
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,151 +72,145 @@ 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>
<h3 className="text-xl font-semibold mb-2">批量下载</h3> {showBatchDownload && (
<p className="text-sm text-base-content/80 mb-4"> <>
点击「下载全部」时,将按下方勾选项的顺序依次尝试。若选中的模式均不可用或全部失败,会弹出提示。 <h3 className="text-xl font-semibold mb-2">批量下载</h3>
</p> <p className="text-sm text-base-content/80 mb-4">
点击「下载全部」时,将按下方勾选项的顺序依次尝试。若选中的模式均不可用或全部失败,会弹出提示。
</p>
<div className="flex flex-col gap-3"> <div className="flex flex-col gap-3">
<BatchOption {canDirectoryPicker && (
id={idDir}
checked={batch.directoryPicker && canDirectoryPicker}
disabled={!canDirectoryPicker}
dimmed={!canDirectoryPicker}
title="使用选择目录批量下载"
description={
canDirectoryPicker
? '通过 showDirectoryPicker 将解密文件直接写入所选目录(推荐,Chrome / Edge)。'
: '当前环境不支持选择目录批量下载。'
}
onChange={(checked) => setBatch('directoryPicker', checked)}
/>
<div className={`ml-6 flex flex-col gap-2 ${batch.directoryPicker && canDirectoryPicker ? '' : 'opacity-50'}`}>
<BatchOption
id={idRemember}
checked={batch.rememberDirectory}
disabled={!batch.directoryPicker || !canDirectoryPicker}
title="记住输出目录"
description="将目录句柄保存在本机;下次下载时可能仍需确认写入权限。"
onChange={(checked) => setBatch('rememberDirectory', checked)}
/>
<div className="flex items-center gap-2 text-sm">
<span className="text-base-content/70">{hasRememberedDir ? '已记住一个输出目录' : '尚未记住输出目录'}</span>
<button
type="button"
className="btn btn-xs btn-ghost"
disabled={!hasRememberedDir}
onClick={() => void handleClearDirectory()}
>
清除
</button>
</div>
</div>
<BatchOption
id={idStreamZip}
checked={batch.streamingZip && canStreamingZip}
disabled={!canStreamingZip}
dimmed={!canStreamingZip}
title="流式生成 ZIP"
description={
canStreamingZip
? '边打包边写入单个 ZIP 文件(showSaveFilePicker),内存占用较低。'
: '当前环境不支持流式写出 ZIP。'
}
onChange={(checked) => setBatch('streamingZip', checked)}
/>
<BatchOption
id={idBlobZip}
checked={batch.blobZip && canBlobZip}
disabled={!canBlobZip}
dimmed={!canBlobZip}
title="生成单个 ZIP 并触发下载"
description={
canBlobZip ? (
<> <>
在内存中生成 ZIP 后触发一次浏览器下载。兼容性最好;ZIP 需整包驻留内存,建议合计不超过约 <BatchOption
500MB~1GB(视设备可用内存而定,过大可能失败或卡顿)。 id={idDir}
checked={batch.directoryPicker}
title="使用选择目录批量下载"
description="通过 showDirectoryPicker 将解密文件直接写入所选目录(推荐,Chrome / Edge)。"
onChange={(checked) => setBatch('directoryPicker', checked)}
/>
<div className={`ml-6 flex flex-col gap-2 ${batch.directoryPicker ? '' : 'opacity-50'}`}>
<BatchOption
id={idRemember}
checked={batch.rememberDirectory}
disabled={!batch.directoryPicker}
title="记住输出目录"
description="将目录句柄保存在本机;下次下载时可能仍需确认写入权限。"
onChange={(checked) => setBatch('rememberDirectory', checked)}
/>
<div className="flex items-center gap-2 text-sm">
<span className="text-base-content/70">
{hasRememberedDir ? '已记住一个输出目录' : '尚未记住输出目录'}
</span>
<button
type="button"
className="btn btn-xs btn-ghost"
disabled={!hasRememberedDir}
onClick={() => void handleClearDirectory()}
>
清除
</button>
</div>
</div>
</> </>
) : ( )}
'当前环境不支持打包 ZIP 下载。'
)
}
onChange={(checked) => setBatch('blobZip', checked)}
/>
</div>
<h3 className="text-xl font-semibold mt-8 mb-2">解密后自动保存</h3> {canStreamingZip && (
<p className="text-sm text-base-content/80 mb-4"> <BatchOption
{canDirectWrite id={idStreamZip}
? '解密完成后直接保存到本机,无需再手动下载。' checked={batch.streamingZip}
: '当前环境无法直接保存到本机,请使用上方的批量下载。此项设置会保留,在桌面客户端中生效。'} title="流式生成 ZIP"
</p> description="边打包边写入单个 ZIP 文件(showSaveFilePicker),内存占用较低。"
onChange={(checked) => setBatch('streamingZip', checked)}
/>
)}
<div className={`flex flex-col gap-3 ${canDirectWrite ? '' : 'opacity-50'}`}> {canBlobZip && (
<div className="flex flex-col gap-1"> <BatchOption
<label htmlFor={idOutputMode} className="font-medium"> id={idBlobZip}
保存位置 checked={batch.blobZip}
</label> title="生成单个 ZIP 并触发下载"
<select description={
id={idOutputMode} <>
className="select select-bordered select-sm w-full max-w-md" 在内存中生成 ZIP 后触发一次浏览器下载。兼容性最好;ZIP 需整包驻留内存,建议合计不超过约
value={directWrite.outputMode} 500MB~1GB(视设备可用内存而定,过大可能失败或卡顿)。
disabled={!canDirectWrite} </>
onChange={(e) => }
dispatch(updateDirectWriteSetting({ outputMode: e.target.value as DirectWriteOutputMode })) onChange={(checked) => setBatch('blobZip', checked)}
} />
> )}
<option value="besideSource">原文件所在文件夹</option>
<option value="fixedDirectory">指定文件夹</option>
</select>
<span className="text-sm text-base-content/70">
{directWrite.outputMode === 'besideSource'
? '解密后的文件保存在加密文件旁边。'
: '解密后的文件保存到下方指定的文件夹。'}
</span>
</div>
<div className="flex flex-col gap-1">
<label htmlFor={idFixedPath} className="font-medium">
文件夹路径
</label>
<div className="flex flex-wrap items-center gap-2 max-w-xl">
<input
id={idFixedPath}
type="text"
className="input input-bordered input-sm min-w-0 flex-1"
placeholder="/path/to/output"
value={directWrite.fixedDirectoryPath ?? ''}
disabled={!canDirectWrite || directWrite.outputMode !== 'fixedDirectory'}
onChange={(e) =>
dispatch(
updateDirectWriteSetting({ fixedDirectoryPath: e.target.value === '' ? null : e.target.value }),
)
}
/>
<button
type="button"
className="btn btn-sm btn-outline"
disabled={!canDirectWrite || pickingDirectory}
onClick={() => void handlePickDirectory()}
>
{pickingDirectory ? '选择中…' : '选择文件夹'}
</button>
</div> </div>
</div> </>
)}
<BatchOption {canDirectWrite && (
id={idDeleteSource} <>
checked={directWrite.deleteSourceAfterDecrypt} <h3 className={`text-xl font-semibold mb-2 ${showBatchDownload ? 'mt-8' : ''}`}>解密后自动保存</h3>
disabled={!canDirectWrite} <p className="text-sm text-base-content/80 mb-4">解密完成后直接保存到本机,无需再手动下载。</p>
title="保存成功后删除加密文件"
description="仅在写出成功后删除;请先确认已有备份。" <div className="flex flex-col gap-3">
onChange={(checked) => dispatch(updateDirectWriteSetting({ deleteSourceAfterDecrypt: checked }))} <div className="flex flex-col gap-1">
/> <label htmlFor={idOutputMode} className="font-medium">
</div> 保存位置
</label>
<select
id={idOutputMode}
className="select select-bordered select-sm w-full max-w-md"
value={directWrite.outputMode}
onChange={(e) =>
dispatch(updateDirectWriteSetting({ outputMode: e.target.value as DirectWriteOutputMode }))
}
>
<option value="besideSource">原文件所在文件夹</option>
<option value="fixedDirectory">指定文件夹</option>
</select>
<span className="text-sm text-base-content/70">
{directWrite.outputMode === 'besideSource'
? '解密后的文件保存在加密文件旁边。'
: '解密后的文件保存到下方指定的文件夹。'}
</span>
</div>
<div className="flex flex-col gap-1">
<label htmlFor={idFixedPath} className="font-medium">
文件夹路径
</label>
<div className="flex flex-wrap items-center gap-2 max-w-xl">
<input
id={idFixedPath}
type="text"
className="input input-bordered input-sm min-w-0 flex-1"
placeholder="/path/to/output"
value={directWrite.fixedDirectoryPath ?? ''}
disabled={directWrite.outputMode !== 'fixedDirectory'}
onChange={(e) =>
dispatch(
updateDirectWriteSetting({ fixedDirectoryPath: e.target.value === '' ? null : e.target.value }),
)
}
/>
<button
type="button"
className="btn btn-sm btn-outline"
disabled={pickingDirectory}
onClick={() => void handlePickDirectory()}
>
{pickingDirectory ? '选择中…' : '选择文件夹'}
</button>
</div>
</div>
<BatchOption
id={idDeleteSource}
checked={directWrite.deleteSourceAfterDecrypt}
title="保存成功后删除加密文件"
description="仅在写出成功后删除;请先确认已有备份。"
onChange={(checked) => dispatch(updateDirectWriteSetting({ deleteSourceAfterDecrypt: checked }))}
/>
</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],