1
import { notification } from '@/components/AntdStaticMethods';
2
import { BRANDING_NAME } from '@/const/branding';
3
import { CURRENT_CONFIG_VERSION, Migration } from '@/migrations';
10
ConfigFileSingleSession,
13
} from '@/types/exportConfig';
15
export const exportConfigFile = (config: object, fileName?: string) => {
16
const file = `${BRANDING_NAME}-${fileName || '-config'}-v${CURRENT_CONFIG_VERSION}.json`;
19
const blob = new Blob([JSON.stringify(config)], { type: 'application/json' });
22
const url = URL.createObjectURL(blob);
24
// 创建一个 <a> 元素,设置下载链接和文件名
25
const a = document.createElement('a');
29
// 触发 <a> 元素的点击事件,开始下载
30
document.body.append(a);
34
URL.revokeObjectURL(url);
38
export const importConfigFile = async (
40
onConfigImport: (config: ConfigFile) => Promise<void>,
42
const text = await file.text();
45
const config = JSON.parse(text);
46
const { state, version } = Migration.migrate(config);
48
await onConfigImport({ ...config, state, version });
52
description: `出错原因: ${(error as Error).message}`,
58
type CreateConfigFileState<T extends ExportType> = ConfigModelMap[T]['state'];
60
type CreateConfigFile<T extends ExportType> = ConfigModelMap[T]['file'];
62
export const createConfigFile = <T extends ExportType>(
64
state: CreateConfigFileState<T>,
65
): CreateConfigFile<T> => {
71
version: Migration.targetVersion,
72
} as ConfigFileAgents;
77
exportType: 'sessions',
79
version: Migration.targetVersion,
80
} as ConfigFileSessions;
85
exportType: 'settings',
87
version: Migration.targetVersion,
88
} as ConfigFileSettings;
91
case 'singleSession': {
93
exportType: 'sessions',
95
version: Migration.targetVersion,
96
} as ConfigFileSingleSession;
103
version: Migration.targetVersion,
108
throw new Error('缺少正确的导出类型,请检查实现...');