mirror of
https://git.um-react.app/um/um-react.git
synced 2026-12-05 10:24:00 +00:00
feat(#10): add batch download settings and capabilities
- Introduced new batch download settings in the settings slice, allowing users to configure options for directory picking, streaming ZIP downloads, and blob ZIP downloads. - Implemented a new general settings panel for managing batch download preferences. - Added utility functions to handle directory selection and ZIP file creation. - Updated the download logic to support multiple download modes based on user preferences. - Enhanced documentation to reflect changes in batch download functionality.
This commit is contained in:
1 parent
3059f495ab
commit
07d160c547
16 files changed
+660
-55
No files matched your search
@@ -0,0 +1,145 @@
|
||||
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 { 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 [hasRememberedDir, setHasRememberedDir] = useState(false);
|
||||
|
||||
const refreshRemembered = useCallback(() => {
|
||||
hasRememberedDirectoryHandle()
|
||||
.then(setHasRememberedDir)
|
||||
.catch(() => setHasRememberedDir(false));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
refreshRemembered();
|
||||
}, [refreshRemembered]);
|
||||
|
||||
const setBatch = (key: keyof BatchDownloadSettings, value: boolean) => {
|
||||
dispatch(updateBatchDownloadSetting({ key, value }));
|
||||
};
|
||||
|
||||
const handleClearDirectory = async () => {
|
||||
await clearRememberedDirectoryHandle();
|
||||
refreshRemembered();
|
||||
toast.info('已清除记住的输出目录');
|
||||
};
|
||||
|
||||
const idDir = useId();
|
||||
const idRemember = useId();
|
||||
const idStreamZip = useId();
|
||||
const idBlobZip = useId();
|
||||
|
||||
return (
|
||||
<div className="min-h-0 flex-col grow px-1">
|
||||
<h2 className="text-2xl font-bold mb-4">通用设置</h2>
|
||||
|
||||
<h3 className="text-xl font-semibold mb-2">批量下载</h3>
|
||||
<p className="text-sm text-base-content/80 mb-4">
|
||||
点击「下载全部」时,将按下方勾选项的顺序依次尝试。若选中的模式均不可用或全部失败,会弹出提示。
|
||||
</p>
|
||||
|
||||
<div className="flex flex-col gap-3">
|
||||
<BatchOption
|
||||
id={idDir}
|
||||
checked={batch.directoryPicker && canDirectoryPicker}
|
||||
disabled={!canDirectoryPicker}
|
||||
dimmed={!canDirectoryPicker}
|
||||
title="使用选择目录批量下载"
|
||||
description={
|
||||
canDirectoryPicker
|
||||
? '通过 showDirectoryPicker 将解密文件直接写入所选目录(推荐,Chrome / Edge)。'
|
||||
: '当前浏览器不支持 showDirectoryPicker。'
|
||||
}
|
||||
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),内存占用较低。'
|
||||
: '当前浏览器不支持 showSaveFilePicker,无法流式写出 ZIP。'
|
||||
}
|
||||
onChange={(checked) => setBatch('streamingZip', checked)}
|
||||
/>
|
||||
|
||||
<BatchOption
|
||||
id={idBlobZip}
|
||||
checked={batch.blobZip}
|
||||
title="生成单个 ZIP 并触发下载"
|
||||
description={
|
||||
<>
|
||||
在内存中生成 ZIP 后触发一次浏览器下载。兼容性最好;ZIP 需整包驻留内存,建议合计不超过约
|
||||
500MB~1GB(视设备可用内存而定,过大可能失败或卡顿)。
|
||||
</>
|
||||
}
|
||||
onChange={(checked) => setBatch('blobZip', checked)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
type BatchOptionProps = {
|
||||
id: string;
|
||||
checked: boolean;
|
||||
disabled?: boolean;
|
||||
dimmed?: boolean;
|
||||
title: string;
|
||||
description: ReactNode;
|
||||
onChange: (checked: boolean) => void;
|
||||
};
|
||||
|
||||
function BatchOption({ id, checked, disabled, dimmed, title, description, onChange }: BatchOptionProps) {
|
||||
return (
|
||||
<label className={`flex items-start gap-2 ${dimmed ? 'opacity-50' : ''}`}>
|
||||
<input
|
||||
id={id}
|
||||
type="checkbox"
|
||||
className="checkbox checkbox-sm mt-0.5"
|
||||
checked={checked}
|
||||
disabled={disabled}
|
||||
onChange={(e) => onChange(e.target.checked)}
|
||||
/>
|
||||
<span>
|
||||
<span className="font-medium">{title}</span>
|
||||
<span className="block text-sm text-base-content/70">{description}</span>
|
||||
</span>
|
||||
</label>
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,13 @@
|
||||
import { debounce } from 'radash';
|
||||
|
||||
import type { AppStore } from '~/store';
|
||||
import { settingsSlice, setProductionChanges, ProductionSettings } from './settingsSlice';
|
||||
import {
|
||||
settingsSlice,
|
||||
setProductionChanges,
|
||||
ProductionSettings,
|
||||
DEFAULT_BATCH_DOWNLOAD_SETTINGS,
|
||||
BatchDownloadSettings,
|
||||
} from './settingsSlice';
|
||||
import { enumObject } from '~/util/objects';
|
||||
import { getLogger } from '~/util/logUtils';
|
||||
import { parseKwm2ProductionKey } from './keyFormats';
|
||||
@@ -9,8 +15,24 @@ import { deepClone } from '~/util/deepClone';
|
||||
|
||||
const DEFAULT_STORAGE_KEY = 'um-react-settings';
|
||||
|
||||
function mergeBatchDownload(source: Partial<BatchDownloadSettings> | undefined): BatchDownloadSettings {
|
||||
const result = { ...DEFAULT_BATCH_DOWNLOAD_SETTINGS };
|
||||
if (!source) {
|
||||
return result;
|
||||
}
|
||||
for (const key of Object.keys(DEFAULT_BATCH_DOWNLOAD_SETTINGS) as (keyof BatchDownloadSettings)[]) {
|
||||
if (typeof source[key] === 'boolean') {
|
||||
result[key] = source[key];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function mergeSettings(settings: ProductionSettings): ProductionSettings {
|
||||
const draft = deepClone(settingsSlice.getInitialState().production);
|
||||
|
||||
draft.general.batchDownload = mergeBatchDownload(settings?.general?.batchDownload);
|
||||
|
||||
if (settings?.qmc2) {
|
||||
const { allowFuzzyNameSearch, keys } = settings.qmc2;
|
||||
for (const [k, v] of enumObject(keys)) {
|
||||
|
||||
@@ -66,3 +66,6 @@ export const selectKugouKey = (state: RootState, hdr: ParseKugouHeaderResponse):
|
||||
|
||||
export const selectStagingQtfmAndroidKey = (state: RootState) => state.settings.staging.qtfm.android;
|
||||
export const selectQtfmAndroidKey = (state: RootState) => state.settings.production.qtfm.android;
|
||||
|
||||
export const selectStagingBatchDownloadSettings = (state: RootState) => state.settings.staging.general.batchDownload;
|
||||
export const selectFinalBatchDownloadSettings = (state: RootState) => state.settings.production.general.batchDownload;
|
||||
@@ -21,7 +21,17 @@ import {
|
||||
StagingKugouKey,
|
||||
} from './keyFormats';
|
||||
|
||||
export interface BatchDownloadSettings {
|
||||
directoryPicker: boolean;
|
||||
rememberDirectory: boolean;
|
||||
streamingZip: boolean;
|
||||
blobZip: boolean;
|
||||
}
|
||||
|
||||
export interface StagingSettings {
|
||||
general: {
|
||||
batchDownload: BatchDownloadSettings;
|
||||
};
|
||||
qmc2: {
|
||||
keys: StagingQMCv2Key[];
|
||||
allowFuzzyNameSearch: boolean;
|
||||
@@ -38,6 +48,9 @@ export interface StagingSettings {
|
||||
}
|
||||
|
||||
export interface ProductionSettings {
|
||||
general: {
|
||||
batchDownload: BatchDownloadSettings;
|
||||
};
|
||||
qmc2: {
|
||||
keys: ProductionQMCv2Keys; // { [fileName]: ekey }
|
||||
allowFuzzyNameSearch: boolean;
|
||||
@@ -53,6 +66,13 @@ export interface ProductionSettings {
|
||||
};
|
||||
}
|
||||
|
||||
export const DEFAULT_BATCH_DOWNLOAD_SETTINGS: BatchDownloadSettings = {
|
||||
directoryPicker: true,
|
||||
rememberDirectory: true,
|
||||
streamingZip: true,
|
||||
blobZip: true,
|
||||
};
|
||||
|
||||
export interface SettingsState {
|
||||
dirty: boolean;
|
||||
staging: StagingSettings;
|
||||
@@ -62,12 +82,14 @@ export interface SettingsState {
|
||||
const initialState: SettingsState = {
|
||||
dirty: false,
|
||||
staging: {
|
||||
general: { batchDownload: { ...DEFAULT_BATCH_DOWNLOAD_SETTINGS } },
|
||||
qmc2: { allowFuzzyNameSearch: true, keys: [] },
|
||||
kwm2: { keys: [] },
|
||||
qtfm: { android: '' },
|
||||
kugou: { keys: [] },
|
||||
},
|
||||
production: {
|
||||
general: { batchDownload: { ...DEFAULT_BATCH_DOWNLOAD_SETTINGS } },
|
||||
qmc2: { allowFuzzyNameSearch: true, keys: {} },
|
||||
kwm2: { keys: {} },
|
||||
kugou: { keys: {} },
|
||||
@@ -76,6 +98,9 @@ const initialState: SettingsState = {
|
||||
};
|
||||
|
||||
const stagingToProduction = (staging: StagingSettings): ProductionSettings => ({
|
||||
general: {
|
||||
batchDownload: { ...staging.general.batchDownload },
|
||||
},
|
||||
qmc2: {
|
||||
keys: stagingKeyToProduction(staging.qmc2.keys, qmc2StagingToProductionKey, qmc2StagingToProductionValue),
|
||||
allowFuzzyNameSearch: staging.qmc2.allowFuzzyNameSearch,
|
||||
@@ -90,6 +115,9 @@ const stagingToProduction = (staging: StagingSettings): ProductionSettings => ({
|
||||
});
|
||||
|
||||
const productionToStaging = (production: ProductionSettings): StagingSettings => ({
|
||||
general: {
|
||||
batchDownload: { ...production.general.batchDownload },
|
||||
},
|
||||
qmc2: {
|
||||
keys: productionKeyToStaging(production.qmc2.keys, qmc2ProductionToStaging),
|
||||
allowFuzzyNameSearch: production.qmc2.allowFuzzyNameSearch,
|
||||
@@ -208,6 +236,13 @@ export const settingsSlice = createSlice({
|
||||
state.staging.qtfm.android = deviceKey;
|
||||
state.dirty = true;
|
||||
},
|
||||
updateBatchDownloadSetting(
|
||||
state,
|
||||
{ payload: { key, value } }: PayloadAction<{ key: keyof BatchDownloadSettings; value: boolean }>,
|
||||
) {
|
||||
state.staging.general.batchDownload[key] = value;
|
||||
state.dirty = true;
|
||||
},
|
||||
//
|
||||
discardStagingChanges: (state) => {
|
||||
state.dirty = false;
|
||||
@@ -252,6 +287,7 @@ export const {
|
||||
kugouImportKeys,
|
||||
|
||||
qtfmAndroidUpdateKey,
|
||||
updateBatchDownloadSetting,
|
||||
|
||||
commitStagingChange,
|
||||
discardStagingChanges,
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import type { FC } from 'react';
|
||||
import { PanelGeneral } from '~/features/settings/panels/PanelGeneral.tsx';
|
||||
import { PanelQMCv2Key } from '~/features/settings/panels/PanelQMCv2Key.tsx';
|
||||
import { PanelKWMv2Key } from '~/features/settings/panels/PanelKWMv2Key.tsx';
|
||||
import { PanelKGGKey } from '~/features/settings/panels/PanelKGGKey.tsx';
|
||||
import { PanelQingTing } from '~/features/settings/panels/PanelQingTing.tsx';
|
||||
|
||||
export const SETTINGS_TABS: Record<string, { name: string; Tab: FC }> = {
|
||||
general: { name: '通用设置', Tab: PanelGeneral },
|
||||
qmc: { name: 'QMCv2 密钥', Tab: PanelQMCv2Key },
|
||||
kwm: { name: 'KWMv2 密钥', Tab: PanelKWMv2Key },
|
||||
kgg: { name: 'KGG 密钥', Tab: PanelKGGKey },
|
||||
qtfm: { name: '蜻蜓 FM', Tab: PanelQingTing },
|
||||
// misc: { name: '其它/待定', Tab: () => <p>这里空空如也~</p> },
|
||||
} as const;
|
||||
Reference in new issue
Block a user