fix: 修复 Tauri 重构导致的配置读取与渲染问题

- 前端:始终绑定 ,避免环境判断失误造成白屏
- 后端: 仅初始化一次,并通过  注入,避免双实例不一致
- 配置: 兼容  回退,提高旧配置兼容性
- 结果:主页面数据正常加载,底部配置路径组件恢复显示
This commit is contained in:
Jason
2025-08-24 23:04:55 +08:00
parent c4791ff523
commit 224d7a8be0
3 changed files with 17 additions and 14 deletions

View File

@@ -12,7 +12,13 @@ pub fn get_claude_config_dir() -> PathBuf {
/// 获取 Claude Code 主配置文件路径 /// 获取 Claude Code 主配置文件路径
pub fn get_claude_settings_path() -> PathBuf { pub fn get_claude_settings_path() -> PathBuf {
get_claude_config_dir().join("settings.json") let dir = get_claude_config_dir();
let settings = dir.join("settings.json");
if settings.exists() {
return settings;
}
// 兼容旧版命名claude.json
dir.join("claude.json")
} }
/// 获取应用配置目录路径 (~/.cc-switch) /// 获取应用配置目录路径 (~/.cc-switch)
@@ -133,4 +139,4 @@ pub fn import_current_config_as_default() -> Result<Value, String> {
log::info!("已导入当前配置为默认供应商"); log::info!("已导入当前配置为默认供应商");
Ok(settings_config) Ok(settings_config)
} }

View File

@@ -4,6 +4,7 @@ mod store;
mod commands; mod commands;
use store::AppState; use store::AppState;
use tauri::Manager;
#[cfg_attr(mobile, tauri::mobile_entry_point)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() { pub fn run() {
@@ -18,7 +19,7 @@ pub fn run() {
)?; )?;
} }
// 初始化应用状态 // 初始化应用状态(仅创建一次,并在本函数末尾注入 manage
let app_state = AppState::new(); let app_state = AppState::new();
// 如果没有供应商且存在 Claude Code 配置,自动导入 // 如果没有供应商且存在 Claude Code 配置,自动导入
@@ -51,9 +52,10 @@ pub fn run() {
} }
} }
// 将同一个实例注入到全局状态,避免重复创建导致的不一致
app.manage(app_state);
Ok(()) Ok(())
}) })
.manage(AppState::new())
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![
commands::get_providers, commands::get_providers,
commands::get_current_provider, commands::get_current_provider,
@@ -69,4 +71,4 @@ pub fn run() {
]) ])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@@ -144,15 +144,10 @@ export const tauriAPI = {
// 创建全局 API 对象,兼容现有代码 // 创建全局 API 对象,兼容现有代码
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
// 检测是否在 Tauri 环境中 // 始终绑定到 window.electronAPI以避免环境判断失误导致未绑定而报错
const isTauri = '__TAURI__' in window; // API 内部已做 try/catch非 Tauri 环境下也会安全返回默认值
(window as any).electronAPI = tauriAPI;
if (isTauri) {
// 在 Tauri 环境中,将 API 绑定到 window.electronAPI
// 保持代码兼容性,无需修改组件代码
(window as any).electronAPI = tauriAPI;
}
// 提供平台信息 // 提供平台信息
(window as any).platform = { (window as any).platform = {
isMac: navigator.platform.toLowerCase().includes('mac') isMac: navigator.platform.toLowerCase().includes('mac')