优化保存方式

This commit is contained in:
limil 2026-02-18 15:36:36 +08:00
parent 4370189e81
commit 5985971041
2 changed files with 43 additions and 8 deletions

View File

@ -17,6 +17,5 @@
---
- [ ] 使用Reducer 和 Immer重构代码
- [ ] 整理、重构代码
- [ ] 添加测试用例
- [ ] 体验调优:生成配置完成后弹出预览窗口可以下载或者复制

View File

@ -168,12 +168,48 @@ function FlowContent(): ReactNode {
const json = instanceToPlain(saveConfig);
const blob = new Blob([JSON.stringify(json, null, 2)], { type: 'application/json' });
const lastNameKey = 'wg-last-filename';
const suggestedName = (localStorage.getItem(lastNameKey) || 'wg-config.json');
// Use File System Access API when available to show save file picker
if ((window as any).showSaveFilePicker) {
try {
const handle = await (window as any).showSaveFilePicker({
suggestedName: suggestedName,
types: [{
description: 'WireGuard Config',
accept: { 'application/json': ['.json'] }
}]
});
const writable = await handle.createWritable();
await writable.write(blob);
await writable.close();
// remember chosen name if available
try {
const name = handle.name || suggestedName;
localStorage.setItem(lastNameKey, name);
} catch (e) {
localStorage.setItem(lastNameKey, suggestedName);
}
} catch (e) {
// If user cancels or error, fallback to anchor download
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'wg-config.json';
a.download = suggestedName;
a.click();
URL.revokeObjectURL(url);
}
} else {
// Fallback: create an anchor and trigger download, using last used filename
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = suggestedName;
a.click();
URL.revokeObjectURL(url);
localStorage.setItem(lastNameKey, suggestedName);
}
} catch (e) {
toast.error('保存失败: ' + e);
}