Files
cc-switch/src/lib/api/settings.ts
Jason d064cb8555 feat: sync current providers to live files after config import
Core Improvements:
- Add sync_current_providers_live command to synchronize in-memory provider
  settings to corresponding live files (~/.claude/settings.json or ~/.codex/auth.json)
- Introduce partial-success state to distinguish between 'import succeeded
  but sync failed' scenario, providing clear user feedback
- Remove unused skip_live_backfill parameter from switch_provider command
- Separate responsibilities: backend handles import/backup, frontend handles
  sync/error presentation

Technical Details:
- Codex: sync auth.json + config.toml with MCP configuration
- Claude: sync settings.json
- Bidirectional sync: read back after write to update in-memory settings_config
- Full i18n support (English and Chinese)
- Graceful handling when no current provider is active

Affected Files:
- Backend: import_export.rs, commands.rs, lib.rs
- Frontend: useImportExport.ts, ImportExportSection.tsx, settings.ts
- i18n: en.json, zh.json

This ensures SSOT (Single Source of Truth) consistency between config.json
and live configuration files after import operations.
2025-10-27 13:20:59 +08:00

123 lines
3.2 KiB
TypeScript

import { invoke } from "@tauri-apps/api/core";
import type { Settings } from "@/types";
import type { AppType } from "./types";
export interface ConfigTransferResult {
success: boolean;
message: string;
filePath?: string;
backupId?: string;
}
export const settingsApi = {
async get(): Promise<Settings> {
return await invoke("get_settings");
},
async save(settings: Settings): Promise<boolean> {
return await invoke("save_settings", { settings });
},
async restart(): Promise<boolean> {
return await invoke("restart_app");
},
async checkUpdates(): Promise<void> {
await invoke("check_for_updates");
},
async isPortable(): Promise<boolean> {
return await invoke("is_portable_mode");
},
async getConfigDir(appType: AppType): Promise<string> {
return await invoke("get_config_dir", {
app_type: appType,
app: appType,
});
},
async openConfigFolder(appType: AppType): Promise<void> {
await invoke("open_config_folder", { app_type: appType, app: appType });
},
async selectConfigDirectory(defaultPath?: string): Promise<string | null> {
return await invoke("pick_directory", { default_path: defaultPath });
},
async getClaudeCodeConfigPath(): Promise<string> {
return await invoke("get_claude_code_config_path");
},
async getAppConfigPath(): Promise<string> {
return await invoke("get_app_config_path");
},
async openAppConfigFolder(): Promise<void> {
await invoke("open_app_config_folder");
},
async getAppConfigDirOverride(): Promise<string | null> {
return await invoke("get_app_config_dir_override");
},
async setAppConfigDirOverride(path: string | null): Promise<boolean> {
return await invoke("set_app_config_dir_override", { path });
},
async applyClaudePluginConfig(options: {
official: boolean;
}): Promise<boolean> {
const { official } = options;
return await invoke("apply_claude_plugin_config", { official });
},
async saveFileDialog(defaultName: string): Promise<string | null> {
return await invoke("save_file_dialog", {
default_name: defaultName,
defaultName,
});
},
async openFileDialog(): Promise<string | null> {
return await invoke("open_file_dialog");
},
async exportConfigToFile(filePath: string): Promise<ConfigTransferResult> {
return await invoke("export_config_to_file", {
file_path: filePath,
filePath,
});
},
async importConfigFromFile(filePath: string): Promise<ConfigTransferResult> {
return await invoke("import_config_from_file", {
file_path: filePath,
filePath,
});
},
async syncCurrentProvidersLive(): Promise<void> {
const result = (await invoke("sync_current_providers_live")) as {
success?: boolean;
message?: string;
};
if (!result?.success) {
throw new Error(result?.message || "Sync current providers failed");
}
},
async openExternal(url: string): Promise<void> {
try {
const u = new URL(url);
const scheme = u.protocol.replace(":", "").toLowerCase();
if (scheme !== "http" && scheme !== "https") {
throw new Error("Unsupported URL scheme");
}
} catch {
throw new Error("Invalid URL");
}
await invoke("open_external", { url });
},
};